简体   繁体   English

如何从 python 连续调用 powershell 命令

[英]How to call powershell commands in a row from python

I am trying to run this ps command within a python script:我正在尝试在 python 脚本中运行此 ps 命令:

subprocess.call('powershell.exe $username = "admin@example.com";$password = Get-Content "C:/Scripts/user.txt" | ConvertTo-SecureString;$cred = new-object -typename System.Management.Automation.PSCredential ` -argumentlist $username, $password;Connect-MsolService -Credential $cred;Get-MsolUser -UserPrincipalName user@example, shell=TRUE) subprocess.call('powershell.exe $username = "admin@example.com";$password = Get-Content "C:/Scripts/user.txt" | ConvertTo-SecureString;$cred = new-object -typename System. Management.Automation.PSCredential ` -argumentlist $username, $password;Connect-MsolService -Credential $cred;Get-MsolUser -UserPrincipalName user@example, shell=TRUE)

So basically as you can understand I call this command within the script and autologin in azure and get the user.所以基本上你可以理解我在 azure 的脚本和自动登录中调用这个命令并获取用户。 My problem is that I am getting this error:我的问题是我收到此错误:

'ConvertTo-SecureString' is not recognized as an internal or external command, operable program or batch file. 'ConvertTo-SecureString' 不是内部或外部命令、可运行程序或批处理文件。

Any ideas?有任何想法吗? Thank you so much in advance everybody.非常感谢大家。

You should run this as it was a normal CMD line.您应该运行它,因为它是一条普通的 CMD 行。 Use CMD's escape symbol ^ before special chars.在特殊字符之前使用 CMD 的转义符号^ Example:例子:

powershell.exe -Command Get-ChildItem ^| ConvertTo-Json

In difficult cases it's easier to use powershell.exe -EncodedCommand ENCCMD where ENCCMD is Base64-encoded text.困难的情况下,使用powershell.exe -EncodedCommand ENCCMD更容易,其中ENCCMD是 Base64 编码的文本。


Avoid using MSOL Powershell CmdLets, use Microsoft Graph Api /users natively in python.避免使用 MSOL Powershell CmdLets,在 python 中使用Microsoft Graph Api /users本机。

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

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