Azure Automation account runbook unable to write to Azure fileshare

Apurva Pathak 320 Reputation points
2024-05-16T10:53:49.6866667+00:00

Hi folks,

I am using Azure Automation account runbook in PowerShell to write to a file in Azure file share, but it is failing with error: "The parameter is incorrect"

Code:

User's image

Error:

User's image

However, same piece of code works when I run it on either my local machine or in the Hybrid Group. To add on, the storage account is open to public networks which is why it works from my local machine.

I am struggling to find the exact cause of this issue; any help will be highly appreciated!

Thanks in advance!

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,200 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,158 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anand Prakash Yadav 7,465 Reputation points Microsoft Vendor
    2024-05-20T09:45:06.33+00:00

    Hello Apurva Pathak,

    Thank you for posting your query here!

    It seems you’re encountering an issue with your Azure Automation runbook in PowerShell.  Here’s an updated approach to address the issue, please try it and let us know if it helps:

    · Instead of using cmd.exe and cmdkey, use Azure PowerShell cmdlets to handle the authentication and file operations.

    · Use a service principal or managed identity for authentication within the runbook.

    Prerequisites:

    · Install the Azure PowerShell module in your Azure Automation account.

    · Assign necessary roles (e.g., Storage Blob Data Contributor) to the managed identity or service principal.

    # Login to Azure using a Run As account or Managed Identity
    # For Run As account
    $connectionName = "AzureRunAsConnection"
    $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
    Connect-AzAccount -ServicePrincipal -Tenant $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
    
    # Define the storage account and share details
    $storageAccountName = "<YourStorageAccountName>"
    $storageAccountKey = "<YourStorageAccountKey>"
    $shareName = "<YourFileShareName>"
    $filePath = "Log.txt"
    
    # Get the storage context
    $context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
    
    # Create or update the file content
    $content = "Testing in Azure DNS Stg"
    Set-Content -Path $filePath -Value $content
    
    # Upload the file to Azure File Share
    $cloudFile = Get-AzStorageFile -Context $context -ShareName $shareName -Path $filePath
    if ($null -ne $cloudFile) {
        # File exists, update content
        $cloudFile | Set-AzStorageFileContent -Context $context -Content $filePath
    } else {
        # File does not exist, create and upload
        Set-AzStorageFileContent -Context $context -ShareName $shareName -Source $filePath -Path $filePath
    }
    
    # Clean up local file if needed
    Remove-Item -Path $filePath -Force
    `
    

    Do let us know if you have any further queries. I’m happy to assist you further.

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.