0

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

subprocess.call('powershell.exe $username = "[email protected]";$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.

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.