如何使用powershell读取存储在azure存储中的csv文件的内容?

问题描述 投票:0回答:1

我想从存储在 azure 存储中的 csv 文件中获取文件名(列 [TransferDate,DirectoryName,FileName,CreationDate] ),但我不想将其存储在本地。我正在自动化 Runbook 中运行脚本,其中我将 csv 中的文件名与存储在其中一个存储容器中的文件名进行比较。

我尝试这样做,但我不知道如何解析文本以获取文件名。还有其他办法吗?

$client = $csvcontainer.CloudBlobContainer.GetBlockBlobReference("Filename")
$file = $client.DownloadText() 

这是我的 powershell 脚本,当我在自动化帐户中运行它时,出现如下所示的错误

$storageAccountName = "xyz"
$containerName = "archive"
$context = New-AzStorageContext -StorageAccountName $storageAccountName -UseConnectedAccount
$blobs = Get-AzStorageBlob -Container $containerName -Context $context

$fileblobs = Get-AzStorageBlobContent -Container "test" -Context $context -blob "abc"
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.workbooks.Open($fileblobs).Sheets.Item(1)
$ws = $workbook.Sheets.Item(1)
$rowMax = $ws.UsedRange.Rows.Count

powershell azure-blob-storage azure-powershell
1个回答
0
投票

我想从存储在 Azure 存储中的 CSV 文件中获取文件名(列:TransferDate、DirectoryName、FileName、CreationDate),但我不想将其存储在本地。

我同意麦克莱顿的评论。如果文件是 CSV 文件,您可以使用

ConvertFrom-Csv
cmdlet 解析内存中的字符串,而无需先将其存储在文件中。

在我的环境中,我已将 CSV 文件存储在 Azure Blob 存储中,详细信息如下。

传送门:

enter image description here

现在,使用下面的脚本,我可以获取文件名。

脚本:

$storageAccountName = "venkat123"
$containerName = "test"
$blobname="test.csv"
$context = New-AzStorageContext -StorageAccountName $storageAccountName -UseConnectedAccount

$csvBlob = Get-AzStorageBlob -Container $containerName -Blob $blobname -Context $context
$csvString = $csvBlob.ICloudBlob.DownloadText()

# Parse the CSV string into objects
$csvObjects = $csvString | ConvertFrom-Csv
$fileNames = $csvObjects.FileName
$fileNames 

输出:

file1.txt
file2.txt
file3.txt
file4.txt

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.