Az Powershell - 无法读取 CSV 文件内容

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

我正在尝试获取备份的 CSV 用户文件的内容并将其与当前设置进行比较并想要分配(未分配的用户)。我正在从文件存储访问此 CSV 文件 - 尽管该文件已列出,但无法使用 Import-Csv 读取内容。请参阅下面的代码并提供帮助。从昨天开始就一直在为此苦苦挣扎!

 # Connect to Azure with system-assigned managed identity (automation account)
Connect-AzAccount -Identity

# Set the subscription
Set-AzContext -SubscriptionId "xxxxxxxxxxxxxxxxxxxxxx"

# Define the session host details
$storageaccountname = "xxxxxxxx"
$StorageAccountKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$filesharename = "userassignment"
$resourceGroupName = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Set up Azure Storage context
$ctx = New-AzStorageContext -StorageAccountName $storageaccountname -StorageAccountKey $StorageAccountKey
Write-Output "ctx: $ctx"

$subdirectoryname =  "wp_20240501"  
$subdirectorypath = $($subdirectoryname)
$csvFileName = "wp_20240501.csv"

# List files within the directory of the Azure File Share
$files = Get-AzStorageFile -ShareName $filesharename -Context $ctx -Path $subdirectorypath -ErrorAction SilentlyContinue | Get-AzStorageFile
Write-Output "files: $($files.Name)"

# Get the content of the CSV file
$csvpath =  $filesharename + "/" + $subdirectorypath + "/" + $csvFileName
Write-Output "csvpath: $csvpath"
$csvdata = Import-Csv -Path $csvpath
Write-Output "csvdata: $csvdata"

列出 hte 目录中的文件工作正常并显示 CSV 文件,但内容无法显示。 显示的错误消息是 -

Import-Csv:找不到路径“C: pp\userassignment\wvdpool-nonprod-pp-p_20240501\wvdpool-nonprod-pp-p_20240501.csv”的一部分。在行:37 字符:12 + $csvdata = Import-Csv -Path $csvpath -Delimiter "," + ~~~~~~~~~~~~~~~ + CategoryInfo : OpenError: (:) [Import-Csv ],DirectoryNotFoundException + FullQualifiedErrorId:FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand

azure powershell csv
1个回答
0
投票

我首先列出文件共享存储帐户中的文件,然后使用 Import-csv 下载文件内容。

我同意mclayton的评论,

Get-AzstorageFile
用于“列出路径的目录和文件”;你需要使用
Get-AzStorageFileContent
。此命令会将文件下载到本地并读取您的 CSV 文件。

您可以使用下面的脚本列出特定目录中的文件并读取 CSV 文件。

脚本:

Connect-AzAccount -Identity

# Set the subscription
Set-AzContext -SubscriptionId "xxxxxxxxxxxxxxxxxxxxxx"

# Define the session host details
$storageaccountname = "venkat123"
$StorageAccountKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$filesharename = "share1"
$resourceGroupName = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"

$ctx = New-AzStorageContext -StorageAccountName $storageaccountname -StorageAccountKey $StorageAccountKey
Write-Output "ctx: $ctx"

$subdirectoryname = "wp_20240501"  
$subdirectorypath = $subdirectoryname
$csvFileName = "titanic.csv"

# List files within the directory of the Azure File Share
$files = Get-AzStorageFile -ShareName $filesharename -Context $ctx -Path $subdirectorypath -ErrorAction SilentlyContinue | Get-AzStorageFile

# Output the list of files
if ($files) {
    Write-Output "List of files in '$subdirectorypath':"
    foreach ($file in $files) {
        Write-Output $file.Name
    }

    # Check if the CSV file exists
    $csvFile = $files | Where-Object { $_.Name -eq $csvFileName }

    if ($csvFile) {
        # Define the local directory where the CSV file will be downloaded
        $localDestination = "<You destination path>" #c:\users\app\

        # Download the CSV file to the local destination
        $csvFilePath = Join-Path -Path $localDestination -ChildPath $csvFileName
        Get-AzStorageFileContent -ShareName $filesharename -Context $ctx -Path "$subdirectorypath/$csvFileName" -Destination $csvFilePath -Force

        # Check if the file was downloaded successfully
        if (Test-Path $csvFilePath -PathType Leaf) {
            # Read the content of the downloaded CSV file using Import-Csv
            $csvData = Import-Csv -Path $csvFilePath

            # Output the CSV data
            Write-Output $csvData
        } else {
            Write-Output "Failed to download the CSV file to the local destination."
        }
    } else {
        Write-Output "CSV file '$csvFileName' not found in the specified directory."
    }
} else {
    Write-Output "No files found in the specified directory."
}

输出:

ctx: Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext
List of files in 'wp_20240501':
03-04-2024 (1).html
04-04-2024.html
April-04-2024 (1).html
response_1712214210668.html
titanic.csv


PassengerId : 1
Survived    : 0
Pclass      : 3
Name        : Braund, Mr. Owen Harris
Sex         : male
Age         : 22
SibSp       : 1
Parch       : 0
Ticket      : A/5 21171
Fare        : 7.25
Cabin       : 
Embarked    : S

enter image description here

参考:

获取 AzureStorageFileContent (Azure.Storage) |微软学习

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