0

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: $_"
}

Venkatesan
  • 3,748
  • 1
  • 3
  • 15
Olgaraa
  • 65
  • 1
  • 9
  • I don't see the [`-Destination`](https://learn.microsoft.com/en-us/powershell/module/az.storage/get-azstoragefilecontent?view=azps-10.2.0#-destination) parameter in your code. – Gaurav Mantri Aug 23 '23 at 12:26
  • Is it needed? Can't I write the Output to a variable and then simply display it? or read the content directly from Storage? – Olgaraa Aug 23 '23 at 12:45
  • I don't think so. This Cmdlet only downloads the file. – Gaurav Mantri Aug 23 '23 at 14:23

1 Answers1

1

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:

enter image description here

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:

enter image description here

Venkatesan
  • 3,748
  • 1
  • 3
  • 15