I want to execute powershell commands in a remote windows server using python. I am using pypsrp for making connection to remote windows server, then executed client.execute_ps(f'Set-ExecutionPolicy RemoteSigned')
got error - 'PSDataStreams' object has no attribute 'decode'
I am able to connect and copy files from local machine to remote window server - client.copy(source_path, dest_path)
I am using pypsrp module to connect to a remote windows server -
try:
with Client(server=self.ip, username=self.username, password=psw, auth='ntlm') as client:
stdout, stderr, rc = client.execute_ps(f'Set-ExecutionPolicy RemoteSigned')
error = stderr.decode('utf-8').strip("\n")
output = stdout.decode('utf-8').strip("\n")
except Exception as e:
print(e)
Got error - 'PSDataStreams' object has no attribute 'decode'
When I print strout, its empty. Stderr is - stderr ---- <pypsrp.powershell.PSDataStreams object at 0x0000014170402AF0>
How to decode the value of stderr, or what is the correct code to execute powershell commands at remote window server.
execute_ps
doesn't support traditional strerr use execute_cmd instead. This is written in execute_ps comment .
Because this runs in a runspace, traditional concepts like stdout, stderr, rc's are no longer relevant. Instead there is a output, error/verbose/debug streams, and a boolean that indicates if the script execution came across an error. If you want the traditional stdout/stderr/rc, use execute_cmd instead.
So, you can get traditional strerr like this.
stdout, stderr, rc = client.execute_cmd('powershell Set-ExecutionPolicy RemoteSigned')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.