Python Forum
How to write a part of powershell command as a variable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to write a part of powershell command as a variable?
#1
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?
Reply
#2
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}')
ibreeden likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
(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}"')
rob101 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using PowerShell from Python script for mounting shares tester_V 8 548 Mar-12-2024, 06:26 PM
Last Post: tester_V
  How Write Part of a Binary Array? Assembler 1 357 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Calling functions by making part of their name with variable crouzilles 4 849 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  PowerShell & Python deep_logic 2 715 Jun-06-2023, 06:34 AM
Last Post: buran
  Os command output in variable shows wrong value paulo79 2 1,522 Apr-09-2022, 03:48 PM
Last Post: ndc85430
  How to use a variable in linux command in python code? ilknurg 2 1,610 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  How to include input as part of variable name Mark17 4 2,524 Oct-01-2021, 06:45 PM
Last Post: Mark17
  Command output to Variable ironclaw 1 1,792 Aug-26-2021, 06:55 PM
Last Post: bowlofred
  How to use a variable in Python (2.x) to define decimal part? MDRI 4 2,344 May-07-2021, 12:39 AM
Last Post: MDRI
Photo Windows 10 PowerShell doesn't work Amy 3 3,930 Apr-27-2021, 01:33 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020