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: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.

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?

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 :)

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.

I use Hyper-V, the virtual machine is accessible to me because I do several operations until I get to this point. (I create it, I copy the files on it, etc.)
I'll try to add what you said above, and I'll come back with the output.

I found the problem, but I don't know exactly how I could solve it. When I send the arguments to the PowerShell script, from python, some arguments also have white spaces, and when I take the arguments it stops at the first white space.(it works now)

In conclusion, if I run the hardcoded command directly in the PowerShell script, everything goes ok on the virtual machine. But if I give the command as a parameter to the script, and it's put in a variable, it doesn't work. So how could I send the command as a script parameter for this to work ok. I want to mention that I do not have much experience with Powershell.

works:
$cmd = { Start-Process -FilePath installer.exe -ArgumentList "arguments string" -verb runas}
Invoke-Command -Session $s -Command $cmd

doesn't work:
If the command is received as the argument of the PowerShell script.
$cmd = $args[0]
Invoke-Command -Session $s -Command {$cmd}

That's how i wrote the code.

Note that in your first example you're using a script block not a string.

In your "doesn't work" example you're passing a string. A script block and a string are very different types of objects!

Try using this example:

$cmd = $args[0]
Invoke-Command -Command {Invoke-Expression -Command $cmd}

. . . and be forewarned that executing arbitrary commands passed a parameters without validating the data is, as they say, "not a good thing"

Well, it "remains hanging" because it receives no input from STDIN. The way that sort of thing was handled when running from a CMD.exe shell was to pipe "Y" to the executable! If it never asked for confirmation the STDIN was never read, so no harm was done.

echo "y" | c:\dir\executable.exe

Assuming your installer is expecting a simple "Y" answer that should handle it. If the installer opens a message-box to request a response I don't remember of that worked . . . too many years have passed since I've had to do such a thing.

Of course, to do that you wouldn't be using "Start-Process" to run the installer. Why not simply use the cmd.exe shell?