从python调用某些powershell命令时将不运行

2024-04-25 18:02:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我在成功运行作为python脚本一部分的powershell脚本时遇到了一些困难。问题不是脚本不运行,即使我尝试导入,也无法识别来自已安装的powershell模块的命令。具体的命令是Connect MSOLService和同一模块中的其他命令。在

我写了一个小测试来尝试我们不同的东西,但似乎没有任何效果。在测试中,我尝试了以下方法:

subprocess.call('powershell ./script.ps1"')

或者

p = subprocess.Popen(['powershell.exe', '-NoProfile', '-Command', '"&{' + pscommand + '}"'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate()

或者

subprocess.call('powershell.exe ./script.ps1"')

除MSOL命令外,所有这些都成功运行脚本,始终返回this错误:

Connect-MsolService : The term 'Connect-MsolService' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Users\user\desktop\scripts\script.ps1:20 char:9 + Connect-MsolService -Credential $LiveCred + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Connect-MsolService:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

当从python脚本外部运行时,执行策略设置为unrestricted,问题只在我使用时发生subprocess.call()在Python中。如有任何建议/帮助,我们将不胜感激。在

谢谢, 卢


Tags: 模块ofthename命令脚本isconnect
1条回答
网友
1楼 · 发布于 2024-04-25 18:02:38
subprocess.call('C:\\Windows\\sysnative\\windowspowershell\\v1.0\\powershell.exe -executionpolicy bypass ./script.ps1')

在尝试了这个并得到了同样的错误。我发现Python正在调用32位版本的PowerShell,但未能导入64位模块。上面的代码能够从32位Python调用64位版本的PowerShell,然后运行包含 [连接MsolService]。在

相关问题 更多 >