question

MyHi-4613 avatar image
0 Votes"
MyHi-4613 asked RichMatheisen-8856 commented

Invoke-Command with subprocess.popen doesn't work

I have a PowerShell script, in which I have the commands:

$Username = 'user'
$Password = 'pass'
$SecureString = ConvertTo-SecureString -AsPlainText $Password -Force
$MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $SecureString

$name= $args[0]
$cmd= $args[1]

$s= New-PSSession -VMName $name-Credential $MySecureCreds
Invoke-Command -Session $s -Command {$cmd}
Remove-PSSession -Session $s

And I want to run it from python with open using these commands:
```
sp = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
(out, err) = sp.communicate()

My PowerShell command that I created in python is this

powershell_cmd = Start-Process -NoNewWindow -FilePath path_to_installer.exe -ArgumentList /S, --accept_license_agreement, --license_validation_file=path_to_license.bat
```
but when I try to run it, nothing happens on the virtual machine.

Can you help me with a hint, or what other technology can I use with python to run this command on the virtual machine?

windows-server-powershell
· 8
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.

Have you verified that the @args array holds the correct values?

I'm assuming that $cmd is supposed to hold the "powershell_cmd" string to start the installer on the remote machine?

Have you tried running the PowerShell script from a PowerShell command line to verify that it works as expected?

1 Vote 1 ·

Have you tried running the PowerShell script from a PowerShell command line to verify that it works as expected?
R: Yes, I tried to run the command directly from the PowerShell on the virtual machine, and it works perfectly.

I'm assuming that $cmd is supposed to hold the "powershell_cmd" string to start the installer on the remote machine?
R: Yes

Have you verified that the @args array holds the correct values?
R: Yes, I checked, all the arguments are perfectly given

Thank you for the answer :)

0 Votes 0 ·

I meant have you tried running the PowerShell script from the machine on which you're running the Python script. :-)

The Invoke-Command cmdlet returns the stdout stream, but not (IIRC) the stderr stream. You might want to add a "2>&1" as the last parameter in the -ArgumentList. Then check the data returned by the Invoke-Command (save it to a local file to make things easy).

Are you running Hyper-V or vSphere? From the PowerShell script, check the VM name with the appropriate Get-VM cmdlet to verify it's accessible to you.

1 Vote 1 ·
Show more comments

0 Answers