简体   繁体   中英

How to call powershell commands in a row from python

I am trying to run this ps command within a python script:

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. 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.

Any ideas? Thank you so much in advance everybody.

You should run this as it was a normal CMD line. Use CMD's escape symbol ^ before special chars. 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.


Avoid using MSOL Powershell CmdLets, use Microsoft Graph Api /users natively in python.

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