1
  • 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).

Netnetter
  • 41
  • 1
  • 9
  • 1
    I think you may want to look at [Get-AzureStorageBlobContent](https://learn.microsoft.com/en-us/powershell/module/azure.storage/get-azurestorageblobcontent?view=azurermps-6.13.0) – Theo Jan 20 '19 at 14:56

1 Answers1

2

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.

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
  • Thanks a lot for the code snippets, so I assume there's no way I can directly copy the contents from the storage containers to my variable? – Netnetter Jan 21 '19 at 10:08
  • If the file is stored in a azure file share, we should follow this answer. But if it's stored in an azure blob storage, you can take a look at this cmdlet [Get-AzureStorageBlobContent](https://learn.microsoft.com/en-us/powershell/module/azure.storage/get-azurestorageblobcontent?view=azurermps-6.13.0). – Ivan Glasenberg Jan 21 '19 at 14:49
  • This is nice, but it doesn't answer the question. OP (and now me) need to know how to get the contents to a variable. Here, the variable is a reference to a local file. I suspect the "Get-Content" will be needed, but, not sure if there requires to be an interim scratch file in the middle - I need to avoid this. – Andrew Jay Sep 24 '19 at 12:23
  • 2
    Hi @AndrewJennings, there is no built-in cmdlet for fetching the content of file storage directly. So I downloaded it first. – Ivan Glasenberg Sep 25 '19 at 06:51