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.