azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

Not able get Powershell script output using run_command

Open mvitor opened this issue 6 years ago • 3 comments

Package Name: azure.mgmt.compute ComputeManagementClient Package Version: 2020_06_01 Operating System: Azure Automation Runbook Python Version: 2

Trying to run a Powershell script targeting a Windows 2016 VM using Python Runbook on Azure Automation . I'm not able to receive the command using any of the documented steps. I see similar issue described here: https://stackoverflow.com/questions/62961460/python-azure-sdk-virtual-machines-run-command

I'm able to execute and parse the same script using Powershell.

Many many different approaches were tried for days. If anyone could recommended any which works we would really appreciate.

To Reproduce

Run Powershell script in Windows VM using Azure Automation Python runbook.

    script = """
add-type @`"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
`"@

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
`$output = Invoke-WebRequest -Uri 'https://some_rest_api_call/?api_token=some_api_token`&scope=global' -UseBasicParsing -Headers @{    Authorization="Bearer some_api_token" } 
Write-output (`"(`$output).content`")
"""
    script_file = open("script.ps1", "w")
    script_file.write(script)
    script_file.close()
    run_command_parameters = {
        'command_id': 'RunPowerShellScript', 
         'ScriptPath ': 'script.ps1',
        "parameters":[]
    }
    
    poller = client.virtual_machines.run_command(
            resource_group_name,
            vm_name,
            run_command_parameters
    )
    while  not poller.done() : 
        print ("waiting for poller")
        time.sleep(5)
    result = poller.result()  # Blocking till executed
    print("Command output 4: "+result.value[0].message)  # stdout/stderr
`

Expected behavior An output like the Powershell script output.

Screenshots

run_command_parameters = {
    'command_id': 'RunPowerShellScript', 
    'script': ['& `"C:/Packages/Plugins/Microsoft.CPlat.Core.RunCommandWindows/1.1.5/Downloads/Invoke-WebRequestTest.ps1`"']   
}
poller = client.virtual_machines.run_command(
        resource_group_name,
        vm_name,
        run_command_parameters
)
while  not poller.done() : 
    print ("waiting for poller")
    time.sleep(5)
result = poller.result()  # Blocking till executed
print("Command output 1: "+str(result))
print("Command output 2: "+str(result.value))
print("Command output 3: "+str(result.value[0]))
print("Command output 4: "+result.value[0].message)  # stdout/stderr

waiting for poller waiting for poller Command output 1: {'additional_properties': {}, 'value': [<azure.mgmt.compute.v2020_06_01.models._models.InstanceViewStatus object at 0x0000000588086358>, <azure.mgmt.compute.v2020_06_01.models._models.InstanceViewStatus object at 0x00000005880866D8>]} Command output 2: [<azure.mgmt.compute.v2020_06_01.models._models.InstanceViewStatus object at 0x0000000588086358>, <azure.mgmt.compute.v2020_06_01.models._models.InstanceViewStatus object at 0x00000005880866D8>] Command output 3: {'code': u'ComponentStatus/StdOut/succeeded', 'level': <StatusLevelTypes.info: 'Info'>, 'display_status': u'Provisioning succeeded', 'additional_properties': {}, 'time': None, 'message': u''} Command output 4: waiting for poller waiting for poller waiting for poller waiting for poller waiting for poller waiting for poller waiting for poller Command output 1: {'additional_properties': {}, 'value': [<azure.mgmt.compute.v2020_06_01.models._models.InstanceViewStatus object at 0x0000000588092208>, <azure.mgmt.compute.v2020_06_01.models._models.InstanceViewStatus object at 0x00000005880923C8>]} Command output 2: [<azure.mgmt.compute.v2020_06_01.models._models.InstanceViewStatus object at 0x0000000588092208>, <azure.mgmt.compute.v2020_06_01.models._models.InstanceViewStatus object at 0x00000005880923C8>] Command output 3: {'code': u'ComponentStatus/StdOut/succeeded', 'level': <StatusLevelTypes.info: 'Info'>, 'display_status': u'Provisioning succeeded', 'additional_properties': {}, 'time': None, 'message': u''} Command output 4:

Additional context Azure Automation Python 2 runbook running Powershell script in Azure VM.

mvitor avatar Aug 31 '20 01:08 mvitor

Code updated with final version.

mvitor avatar Sep 01 '20 19:09 mvitor

Hi @mvitor. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text “/unresolve” to remove the “issue-addressed” label and continue the conversation.

ghost avatar Dec 15 '21 08:12 ghost

Hi @mvitor, since you haven’t asked that we “/unresolve” the issue, we’ll close this out. If you believe further discussion is needed, please add a comment “/unresolve” to reopen the issue.

ghost avatar Dec 22 '21 10:12 ghost