简体   繁体   English

使用绝对路径包含空格执行Powershell文件

[英]Execute a powershell file with an absolute path that contains spaces

I'm trying to execute a ps1 file in my python script. 我正在尝试在python脚本中执行ps1文件。

proc = subprocess.Popen(["powershell.exe", 'F:/FOLDER/FOLDER OK/myfile.ps1'])

It stopped at F:/FOLDER/FOLDER so I added additional quotes like this: 它在F:/FOLDER/FOLDER处停止,因此我添加了如下附加引号:

proc = subprocess.Popen(["powershell.exe", '"F:/FOLDER/FOLDER OK/myfile.ps1"'])

No error now, but it didn't execute the script either... Any idea? 现在没有错误,但是它也没有执行脚本...知道吗?

powershell.exe does not assume that a parameter is automatically a file to execute; powershell.exe不假定参数自动是要执行的文件; you need to identify all parameters. 您需要确定所有参数。 Use the -File switch 使用-File开关

proc = subprocess.Popen(["powershell.exe", "-File", r"F:\FOLDER\FOLDER OK\myfile.ps1"])

The r in r"..." (for raw ) turns off escape-sequence processing so that \\ can be used without escaping. rr"..." (为原料 )关闭转义序列处理,使得\\可以在不脱离被使用。

See PowerShell Command Line Help at Microsoft Docs. 请参阅Microsoft Docs中的PowerShell命令行帮助

PowerShell[.exe]
       [-Command { - | <script-block> [-args <arg-array>]
                     | <string> [<CommandParameters>] } ]
       [-EncodedCommand <Base64EncodedCommand>]
       [-ExecutionPolicy <ExecutionPolicy>]
       [-File <FilePath> [<Args>]]
       [-InputFormat {Text | XML}] 
       [-Mta]
       [-NoExit]
       [-NoLogo]
       [-NonInteractive] 
       [-NoProfile] 
       [-OutputFormat {Text | XML}] 
       [-PSConsoleFile <FilePath> | -Version <PowerShell version>]
       [-Sta]
       [-WindowStyle <style>]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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