Why won't my Azure Runbook execute my Python script via Powershell

356 views Asked by At

When I remote into my VM to excute this Powershell script, it all works fine.

However, if I try to run the script within my Azure Runbook it will execute, but the Python script will not trigger and I don't know why :( It just seems to skip over it. Am I missing something?

This is the code in my Runbook

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection 
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        "are we here"
        throw $_.Exception

    }
}
$rgname ="MyResourceName"
$vmname ="MyVirtualMachine"
$ScriptToRun = "c:\mypath\myscript.ps1"
Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1
$run = Invoke-AzureRmVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
Write-Output $run.Value[0]
Remove-Item -Path ScriptToRun.ps1

Here is myscript.ps1 (the Path to Python is set the Environment Variables)

Write-Output "Script Started."
Python C:\myscripts\pythonscript.py
Write-Output "Script Ended." 

What I will then see is this output

Script Started.
Script Ended.

The basic output from the py script should be in the middle, but I get nothing. Again, if I run it locally, it works fine.

Thank you for any assistance

1

There are 1 answers

0
Joy Wang On

Try to migrate your script to the new Az module, make sure you have installed the Az.Accounts, Az.Compute in your automation account.

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection 
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
    "Logging in to Azure..."
    Connect-AzAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        "are we here"
        throw $_.Exception

    }
}
$rgname ="MyResourceName"
$vmname ="MyVirtualMachine"
$ScriptToRun = "c:\mypath\myscript.ps1"
Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1
$run = Invoke-AzVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
Write-Output $run.Value[0]
Remove-Item -Path ScriptToRun.ps1