I am connecting to my Azure File Share using Powershell in Azure Automation and this works fine, I can list all the files and directories. Unfortunately when I try to read the content of any of the files, the output that I get is always empty. I tried to read a csv file, a json and a text, but in all cases the Output is always empty. Is there something wrong with my code?
#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: $_"
}
Empty File Output while reading a file from Azure File Share using Powershell in Azure Automation
For reading the content from Azure file share in Azure-automation
, you can use the below PowerShell code.
Content in my file:
Code:
$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: $_"
}
Output: