简体   繁体   中英

Python calling Powershell commands

I'm building an API that's calling Powershell to get the data.

Everything works fine except '*' when I'm running the command using subprocess.check_output

powershell_call = json.loads(subprocess.check_output([
    'powershell.exe',
     f'{cmdlet_name} -Identity "{identity}" -Server {server} -Properties * | ConvertTo-Json -Depth {jsonDepth}'
]))

Error message:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa2 in position 8110: invalid start byte

I tried to change the calling method to this:

powershell_call = subprocess.Popen(
    [
        "powershell.exe",
        f'{cmdlet_name} -Identity "{identity}" -Server {server} -Properties * | ConvertTo-Json -Depth {jsonDepth}'
    ],
    shell=True,
    stdout = subprocess.PIPE,
    encoding = 'utf-8',
    universal_newlines = True
)

But it gives the same error message.

Is there any way to pass the asterisk character while calling PowerShell via subprocess?

--- EDIT ---

Turned out it I just needed to change subprocess.check_output to subprocess.getoutput

原来,我只是需要改变subprocess.check_outputsubprocess.getoutput

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM