question

AzeemK-2483 avatar image
0 Votes"
AzeemK-2483 asked tbgangav-MSFT answered

Azure Automation running Power Shell scripts in runbooks with erros

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-factoryazure-automation
image.png (402.6 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @AzeemK-2483,

Can you share the complete error that you are receiving?
Also, can you share the complete script that you are trying so I can try to reproduce the issue and help fix the error?

0 Votes 0 ·

126442-image.png



0 Votes 0 ·
image.png (380.9 KiB)

Above is the complete error , complete script is in first post , basically I am trying to delete manipulate bunch on prem files using power shell ,
I wan to run that power shell script in Azure Automation runbook

0 Votes 0 ·

1 Answer

tbgangav-MSFT avatar image
0 Votes"
tbgangav-MSFT answered

Hi @AzeemK-2483,

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

  3. 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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.