Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write a part of powershell command as a variable in python?

I have a powershell command to run it with winrm.

result  = session.run_ps('get-wmiobject -namespace root\SecurityCenter2 -computername localhost -Query "Select * from AntiVirusProduct"')

I want to give this part of command as a variable :

"Select * from AntiVirusProduct"

like :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

query = "Select * from AntiVirusProduct"
result  = session.run_ps('get-wmiobject -namespace root\SecurityCenter2 -computername localhost -Query query')

How can i do it in python?

>Solution :

A simple f-string should do the trick here:

query = "Select * from AntiVirusProduct"
result  = session.run_ps(f'get-wmiobject -namespace root\\SecurityCenter2 -computername localhost -Query "{query}"')

Let me know if that works for you as I don’t have a windows machine handy to test it on.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading