How can I copy the contents of a .csv file placed in a azure file storage, to a powershell variable?

﹥>﹥吖頭↗ 提交于 2021-02-04 21:30:31

问题


  • I am creating a runbook for automating a monotonous DB task.
  • My master.csv file gets updated every hour with the details of all the resources in our Infrastructure and is placed in an Azure file storage system.
  • I am trying to take the name of a resource(DB) as an input from a user and verify if it exists on my Azure infrastructure by checking against a master inventory file.

My main concern is if I will be able to get the contents of this CSV(<100KB) in a variable so that I use it for comparison in the subsequent step?

I have tried the below code:

The file is present at {StorageContainer}/a/b/{filename}.csv

  1. $inventory = import-csv {storageaccntname}/a/b/{filename}.csv

  2. $inventory = import-csv https://{storagecontainername}/a/b/{filename}.csv

  3. $inventory = import-csv {random drive letters like C:,D:,E:}/a/b/{filename}.csv (Don't think these even exist for an azure file storage)

All resulting in file not found error.

I also had a look at the Get-AzureStorageFileContent command however this seems to download the whole file at a destination (doesn't serve the purpose).


回答1:


Since the .csv file is stored in the file share storage, you can use Get-AzureStorageFileContent to download the .csv to the $evn:temp folder in powershell runbook, then you can operate the .csv file as per your need.

Here is an example:

Assume the file path in azure file storage is: https://xx.file.core.windows.net/a/b/test.csv (in this example, the file share name is "a").

In powershell runbook:

$storageAccountName ="xx"
$storageAccountKey ="xxxxxx"
$context = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey 

# download the test.csv file to the temp folder in runbook
Get-AzureStorageFileContent -ShareName a -Path "b/test.csv" -Destination $env:temp -Context $context

# Then you can do anything to the test.csv file(Note: the file path now is $env:temp\test.csv)

Please let me know if any more issues.



来源:https://stackoverflow.com/questions/54277036/how-can-i-copy-the-contents-of-a-csv-file-placed-in-a-azure-file-storage-to-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!