Hi @AzeemK ,
Firstly, the script that's in your first post is slightly trimmed so I couldn't see the complete script. If possible, please post the complete script again.
Secondly, as per the error shared, the reason for it in general is, some expression in the script is null (i.e., some line in the script didn't return any output) and then you have tried to use that expression to call a method of it. Typically it might be due to one of the below reasons.
- expression or variable being defined outside the script block or scope
- empty or null string value
Lastly, as you have mentioned that script runs fine in PowerShell IDE so I assume it meant that script executed successfully from a local machine. If that's the case, then I would recommend to double check if file share is accessible from Azure Automation level or not (by default it would not).
Also, in general to remotely connect to an on prem machine and execute a script with the help of Azure Automation, you can follow one of the below 3 approaches:
- Hybrid runbook worker i.e., Runbooks in Azure Automation might not have access to resources in other clouds or in your on-premises environment because they run on the Azure cloud platform. You can use the Hybrid Runbook Worker feature of Azure Automation to run runbooks directly on the machine that's hosting the role and against resources in the environment to manage those local resources. Runbooks are stored and managed in Azure Automation and then delivered to one or more assigned machines.
- Create an Azure storage account, container and upload a blob i.e., script (say rename_filenames_on_onprem_file_share.ps1 file with the content of your script) and then have below code in Azure Automation PowerShell runbook.
$connectionName = "AzureRunAsConnection"
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
$ConnectToAzAccount = Add-AzAccount -ServicePrincipal -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint$StorageAccountName = "xxxxxxxxxxxxx"
$StorageAccountKey = "xxxxxxxxxxxxxx=="
$ContainerName = "xxxxxxxxxxxxxxx"
$BlobName_Windows = "rename_filenames_on_onprem_file_share.ps1"
$RG_VM = "xxxxxxxxxxxxxxxxxx"
$VM_Name_Windows = "xxxxxxxxx"
$InvokeCmd_Id_Windows = "RunPowerShellScript"$AzStorage = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$AzStorageContext = $AzStorage.Context$GetBlobContent_Windows = Get-AzStorageBlobContent -Container $ContainerName -Blob $BlobName_Windows -Destination ($Env:temp+"/rename_filenames_on_onprem_file_share.ps1") -Context $AzStorageContext -Force
$InvokeRunCmdOutput_Windows = Invoke-AzVMRunCommand -ResourceGroupName $RG_VM -VMName $VM_Name_Windows -CommandId $InvokeCmd_Id_Windows -ScriptPath ($Env:temp+"/rename_filenames_on_onprem_file_share.ps1")
$TomcatServiceStart_Output_Windows = $InvokeRunCmdOutput_Windows.Value[0].Message
Write-Output $TomcatServiceStart_Output_Windows
- Save a script (say C:\test\rename_filenames_on_onprem_file_share.ps1) in your local machine (from where it executed without any issues) and have your script as content in that file and then have below code in Azure Automation PowerShell runbook.
$ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'
Add-AzAccount -ServicePrincipal -TenantId $ServicePrincipalConnection.TenantId -ApplicationId $ServicePrincipalConnection.ApplicationId -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint
$rgname ="rrrrrrrrrrrrrr"
$vmname ="vvvvvvvvvvvvvv"
$ScriptToRun = "C:\test\rename_filenames_on_onprem_file_share.ps1"
Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1
Invoke-AzVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
Remove-Item -Path ScriptToRun.ps1
Source of the above information:
- https://learn.microsoft.com/en-us/answers/questions/453041/index.html
- https://learn.microsoft.com/en-us/answers/questions/293294/index.html
- https://learn.microsoft.com/en-us/answers/questions/313671/index.html
As you have mentioned that you are new to Azure Automation so if you have further queries w.r.t any of the above information or needs clarification then feel free to revert back. Thanks.