Python Forum

Full Version: How to write a part of powershell command as a variable?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 :
query = "Select * from AntiVirusProduct"
result  = session.run_ps('get-wmiobject -namespace root\SecurityCenter2 -computername localhost -Query[b] query[/b]')
How can i do it?
This is untested, but in theory in should work:

query = '"Select * from AntiVirusProduct"'
result = session.run_ps(f'get-wmiobject -namespace root\SecurityCenter2 -computername localhost -Query {query}')
(Jul-26-2022, 11:27 AM)rob101 Wrote: [ -> ]This is untested, but in theory in should work:

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

It works thank you. But you missed " ".
It works now:

 result = session.run_ps(f'Get-WmiObject -Query "{query}"')