Search code examples
pythonazurepowershellazure-runbookazure-vm

Azure VM Run PowerShell Script doesn't Output


I'm attempting to run a python script inside of my Windows 10 Azure VM. I've managed to connect to the VM and run the script from an Automation runbook but nothing from the powershell script seems to be outputted after the runbook completes.

My python script stored at C:\Users\username\Desktop\test.py:

print("Hello World.")

My powershell script stored at C:\Users\username\Desktop\test.ps1:

Write-Output "Starting Script..."
C:\Users\username\Python\python.exe C:\Users\username\Desktop\test.py
Write-Output "Shutting Down..."

My Azure runbook named VMRunTest:

$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
        throw $_.Exception
    }
}

$rgname ="ParcelTracking"
$vmname ="ParcelTracking-Scraper"
$ScriptToRun = "C:\Users\username\Desktop\test.ps1"
Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1 
Invoke-AzureRmVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
Remove-Item -Path ScriptToRun.ps1

Per the documentation it also requires that I open the output port on the VM to allow the 443 port with the AzureCloud tag. In the following image you what my setting are for that.

When I execute the Azure Runbook, I get no errors, warnings, and no exceptions. This is the output that follows:

Logging in to Azure...


Environments                                                                                                            
------------                                                                                                            
{[AzureChinaCloud, AzureChinaCloud], [AzureCloud, AzureCloud], [AzureGermanCloud, AzureGermanCloud], [AzureUSGovernme...


Value     : {Microsoft.Azure.Management.Compute.Models.InstanceViewStatus, 
            Microsoft.Azure.Management.Compute.Models.InstanceViewStatus}
Name      : 
StartTime : 
EndTime   : 
Status    : Succeeded
Error     : 
Output    : 
Capacity  : 0
Count     : 0
Item      : 

So, it appears to have been successful, however I don't see any mention of the Hello World. statement or either output statements from the powershell script. So, I can only assume that the python script is not executing. I also know this from trying this process on a python script that should take roughly ~15minutes to run and it comes back as completed within 1 minute.

I think I'm close, just missing a few minor details somewhere. Any help would be greatly appreciated.


Solution

  • I can reproduce your issue, actually it works.

    Change the line

    Invoke-AzureRmVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
    

    to

    $run = Invoke-AzureRmVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1 
    Write-Output $run.Value[0]
    

    Then you will be able to see the output(in my vm, I didn't install python, for a quick test, I just use powershell, in your case, it should also work).

    enter image description here