【问题标题】:How can I copy the contents of a .csv file placed in a azure file storage, to a powershell variable?如何将放置在 azure 文件存储中的 .csv 文件的内容复制到 powershell 变量?
【发布时间】:2019-01-20 13:42:47
【问题描述】:
  • 我正在创建一个用于自动化单调数据库任务的运行手册。
  • 我的 master.csv 文件每小时更新一次,其中包含我们基础架构中所有资源的详细信息,并放置在 Azure 文件存储系统中。
  • 我正在尝试将资源 (DB) 的名称作为用户的输入,并通过检查主清单文件来验证它是否存在于我的 Azure 基础架构中。

我主要关心的是是否能够在变量中获取此 CSV(

我试过下面的代码:

文件位于 {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 {随机驱动器盘符,如 C:,D:,E:}/a/b/{filename}.csv(不要认为这些甚至存在于 azure 文件存储中)

所有导致文件未找到错误。

我还查看了 Get-AzureStorageFileContent 命令,但这似乎将整个文件下载到目的地(没有达到目的)。

【问题讨论】:

标签: azure powershell azure-runbook


【解决方案1】:

由于.csv文件存储在文件共享存储中,您可以使用Get-AzureStorageFileContent将.csv文件下载到powershell runbook中的$evn:temp文件夹中,然后您可以根据需要对.csv文件进行操作。

这是一个例子:

假设 azure 文件存储中的文件路径为:https://xx.file.core.windows.net/a/b/test.csv(在此示例中,文件共享名称为“a”)。

在 powershell 运行手册中:

$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)

如果还有问题请告诉我。

【讨论】:

  • 非常感谢sn-ps的代码,所以我认为我无法直接将存储容器中的内容复制到我的变量中?
  • 如果文件存储在 azure 文件共享中,我们应该遵循这个答案。但如果它存储在 azure blob 存储中,您可以查看此 cmdlet Get-AzureStorageBlobContent
  • 这很好,但它没有回答问题。 OP(现在是我)需要知道如何将内容获取到变量中。这里,变量是对本地文件的引用。我怀疑将需要“获取内容”,但不确定中间是否需要临时暂存文件 - 我需要避免这种情况。
  • 嗨@AndrewJennings,没有用于直接获取文件存储内容的内置cmdlet。所以我先下载了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-07
  • 2021-03-09
  • 2021-02-22
  • 2019-09-13
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
相关资源
最近更新 更多