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
  How to run shell command, capture the output, then write it into textfile? tatahuft 4 977 Dec-20-2024, 02:13 PM
Last Post: Axel_Erfurt
  Show powershell errors in flask-wtf web form robertkwild 14 2,625 Jun-30-2024, 03:15 PM
Last Post: robertkwild
  Cant get powershell script to run with subprocess robertkwild 2 1,432 Jun-27-2024, 01:23 PM
Last Post: robertkwild
  Running powershell command in flask wtf form robertkwild 10 2,345 Jun-27-2024, 09:49 AM
Last Post: robertkwild
  Passing web form to powershell robertkwild 1 974 Jun-14-2024, 10:16 AM
Last Post: Akshiya
  using PowerShell from Python script for mounting shares tester_V 8 2,376 Mar-12-2024, 06:26 PM
Last Post: tester_V
  How Write Part of a Binary Array? Assembler 1 1,014 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Calling functions by making part of their name with variable crouzilles 4 1,846 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  PowerShell & Python deep_logic 2 1,763 Jun-06-2023, 06:34 AM
Last Post: buran
  Os command output in variable shows wrong value paulo79 2 2,314 Apr-09-2022, 03:48 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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