在 Azure 自动化中使用 Powershell 从 Azure 文件共享读取文件时清空文件输出

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

我正在使用 Azure 自动化中的 Powershell 连接到我的 Azure 文件共享,这工作正常,我可以列出所有文件和目录。不幸的是,当我尝试读取任何文件的内容时,我得到的输出始终为空。我尝试读取 csv 文件、json 和文本,但在所有情况下输出始终为空。我的代码有问题吗?

#Credentials for File Storage
$credentials = Get-AutomationPSCredential -Name "access_key"

# Storage account information
$storageAccountName = "xxxxx"
$storageAccountKey = $credentials.GetNetworkCredential().Password

# File share information
$shareName = "archive"
$filePath = 'test.txt'

try {
    # Construct the storage context
    $context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

    # List all files directly under the root of the share
    $files = Get-AzStorageFile -ShareName $shareName -Context $context

    foreach ($file in $files) {
        Write-Output "File: $($file.Name)"
    }

  $fileContent = Get-AzStorageFileContent -ShareName $shareName -Path $filePath -Context $context


Write-Output "File Content:"
Write-Output "$fileContent"
}
catch {
    Write-Output "An error occurred: $_"
}

azure powershell azure-storage azure-automation azure-file-share
1个回答
0
投票

在 Azure 自动化中使用 Powershell 从 Azure 文件共享读取文件时清空文件输出

要从

Azure-automation
中读取 Azure 文件共享的内容,您可以使用以下 PowerShell 代码。

我的文件内容:

enter image description here

代码:

$Appid = "your-application-id"
$PWord = ConvertTo-SecureString -String "your-clinet-secret" -AsPlainText -Force
$tenant = "<your-tenant-id>"
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Appid,$PWord
Connect-AzAccount -Credential $Credential -Tenant $tenant -ServicePrincipal -Subscription "<Your-subscription-id>"


$storageAccountName = "venkat123"
$storageAccountKey = "<your-storage-account-key>"

# File share information
$shareName = "fileshare1"
$filePath = 'sample326.txt'

try {
    # Construct the storage context
    $context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

    # List all files directly under the root of the share
    $files = Get-AzStorageFile -ShareName $shareName -Context $context

    foreach ($file in $files) {
        Write-Output "File: $($file.Name)"
    }
    $tempPath = Join-Path $env:TEMP "tempfile.txt"
    Get-AzStorageFileContent -ShareName $shareName -Path $filePath -Destination $tempPath -Context $context

    $fileContent = Get-Content -Path $tempPath

    Remove-Item -Path $tempPath -Force

    Write-Output "File Content:"
    Write-Output $fileContent
}
catch {
    Write-Output "An error occurred: $_"
}

输出:

enter image description here

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