Azure Automation running Power Shell scripts in runbooks with erros

AzeemK 516 Reputation points
2021-08-25T05:15:34.4+00:00

I am running the power shell script below in Azure Automation runbook , the script goes to on prem file share and rename file names replace strings. Script runs fine in Powershell IDE , I wanted to run from ADF or Azure Automation ,I am new to Azure Automation what are the things I have to do to make this run succesfully in Azure automation see screen shot below

126205-image.png

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
9,644 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,132 questions
{count} votes

Accepted answer
  1. tbgangav-MSFT 10,386 Reputation points
    2021-08-26T15:51:49.977+00:00

    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:

    1. 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.
    2. 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

    1. 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:

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful