Invoking a PowerShell script from Python

后端 未结 2 689
遥遥无期
遥遥无期 2020-12-09 10:28

I\'m trying to start a PowerShell script from python like this:

psxmlgen = subprocess.Popen([r\'C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.ex         


        
相关标签:
2条回答
  • 2020-12-09 10:34

    First, Set-ExecutionPolicy Unrestriced is on a per user basis, and a per bitness basis (32-bit is different than 64-bit).

    Second, you can override the execution policy from the command line.

    psxmlgen = subprocess.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
                                 '-ExecutionPolicy',
                                 'Unrestricted',
                                 './buildxml.ps1',
                                 arg1, arg2, arg3], cwd=os.getcwd())
    result = psxmlgen.wait()
    

    Apparently you can access the 64-bit PowerShell from 32-bit PowerShell with this path (thanks to @eryksun in comments):

    powershell64 = os.path.join(os.environ['SystemRoot'], 
        'SysNative' if platform.architecture()[0] == '32bit' else 'System32',
        'WindowsPowerShell', 'v1.0', 'powershell.exe')
    
    0 讨论(0)
  • 2020-12-09 10:42

    For those of us who wanted to know how to display the values of arg1, arg2 and arg3 after it was passed to powershell, all you need to do is:

    Write-Host $args[0]
    Write-Host $args[1]
    Write-Host $args[2]
    
    0 讨论(0)
提交回复
热议问题