Run powershell command with quote marks via cmd - powershell

I need to run a powershell command via CMD
my command is
(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "ACPI Thermal Zone"')
[1].GetDeviceProperties().DeviceProperties |
Select-Object -Property keyName, data
and i get successful result:
However, when i try to run the same command with cmd, I getting exception in the query execution.
it looks like something with the quote marks in the command.
Cmd command
powershell -command "(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "ACPI Thermal
Zone"')"
Cmd error
Please help, what do i miss?
Edit
I tried double quotes and escape char with no luck

You might resolve this at a CMD level by escaping the double quotes with a backslash (\"):
PowerShell -command "Get-WmiObject -Class win32_pnpEntity -Filter 'Name like \"ACPI Thermal Zone\"'"
... or as WMI queries also accepts single quotes for names in filters.
You might also simply avoid the double quotes in the WMI query and resolve this with (escaped) single quotes ('') at a PowerShell level:
PowerShell -command "(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like ''ACPI Thermal Zone''')"

Related

batch file with inline powershell script that contains variables [duplicate]

I need to run a powershell command via CMD
my command is
(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "ACPI Thermal Zone"')
[1].GetDeviceProperties().DeviceProperties |
Select-Object -Property keyName, data
and i get successful result:
However, when i try to run the same command with cmd, I getting exception in the query execution.
it looks like something with the quote marks in the command.
Cmd command
powershell -command "(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like "ACPI Thermal
Zone"')"
Cmd error
Please help, what do i miss?
Edit
I tried double quotes and escape char with no luck
You might resolve this at a CMD level by escaping the double quotes with a backslash (\"):
PowerShell -command "Get-WmiObject -Class win32_pnpEntity -Filter 'Name like \"ACPI Thermal Zone\"'"
... or as WMI queries also accepts single quotes for names in filters.
You might also simply avoid the double quotes in the WMI query and resolve this with (escaped) single quotes ('') at a PowerShell level:
PowerShell -command "(Get-WmiObject -Class win32_pnpEntity -Filter 'Name like ''ACPI Thermal Zone''')"

Batch script to taskkill search by "Command Line" arguments

I need a batch script to taskkill by "Command Line" arguments ("Command Line" from Windows Task Manager). To clarify - these processes are dotnet core applications. They are started via:
dotnet MyDotnetCoreApp.dll xxx yyy
If you examine under Task Managers,
Name = dotnet.exe
Image path name = C:\Program Files\dotnet\dotnet.exe
Command line = dotnet MyDotnetCoreApp.dll xxx yyy
I need a batch script to kill these tasks, probably with taskkill
OPTION 1 is Taskkill by PID but how my script search "Command Line" arguments for MyDotnetCoreApp?
OPTION 2 is taskkill by Image Name? This is no go as my server has many dotnet core applications, if kill my Image Name, all dotnet core processes be killed
I been researching:
https://superuser.com/questions/415360/how-do-i-find-out-command-line-arguments-of-a-running-program
https://www.itprotoday.com/powershell/powershell-contains
I can't get this to work, not good at PowerShell:
Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | Select-Object Handle
Here'd get a list of PIDs with which to kill.
Two challenges:
First Challenge, my WHERE clause dont work:
Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | where {$_.CommandLine -like '*MyDotnetCoreApp*'} | Select-Object Handle
I checked further, found out these "CommandLine" was NOT populated for these WmiObjects (omg!):
Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | Select-Object ProcessId, Name, CSName, Caption, CommandLine, ExecutablePath
I later then found out "CommandLine" would have been populated IF you run Powershell as Administrator!?! (Powershell so cryptic!)
In the end - First challenged was resolved:
Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | where {$_.CommandLine -like '*MyDotnetApp*'} | Select-Object ProcessId, Name, CSName, Caption, CommandLine, ExecutablePath
Second Challenge: How to kill it? Found it!!
(Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | where {$_.CommandLine -like '*MyDotnetCoreApp*'}).Terminate()
So this is actually resolved!
Run Powershell as Administrator! Download psexec from https://learn.microsoft.com/en-us/sysinternals/downloads/psexec
psexec -u Administrator -p SomeSecret powershell
Then from Powershell:
(Get-WmiObject Win32_Process -Filter "name = 'dotnet.exe'" | where {$_.CommandLine -like '*MyDotnetCoreApp*'}).Terminate()
Now as separate question, can you do this one line? Below wont work because -Filter has quotes in it!
psexec -u Administrator -p SomeSecret powershell -Command "(Get-WmiObject Win32_Process -Filter ""name = 'dotnet.exe'"" | where {$_.CommandLine -like '*MyDotnetCoreApp*'}).Terminate() "
As hacky work around, I removed -Filter clause (How unfortunate, not sure how to escape quotes):
psexec -u Administrator -p SomeSecret powershell -Command "(Get-WmiObject Win32_Process | where {$_.CommandLine -like '*MyDotnetCoreApp*'}).Terminate() "
Works for me as a regular user, unless the process is running as administrator?. Unfortunately, the filter syntax is like sql here, where '%' is the wildcard. Piping to where-object would probably work just as well.
get-wmiobject win32_process -filter "commandline like '%dotnet.exe%MyDotnetCoreApp%'" |
remove-wmiobject
get-wmiobject win32_process | where commandline -like '*dotnet.exe*MyDotnetCoreApp*' |
remove-wmiobject

PowerShell: Adding filter or query to Get-WmiObject

I'm trying to add a filter or query to a line of PowerShell code, but I am getting weird results. I can replicate this problem using any WMI query. Here is an example:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "Get-WmiObject -class Win32_Processor -Namespace root\cimv2 -filter 'Status = `"OK`"'"
That gives me this error: Get-WmiObject : Invalid query "select * from Win32_Processor where Status = OK". But if I run it in a PowerShell window that is already open, like this:
Get-WmiObject -class Win32_Processor -Namespace root\cimv2 -filter 'Status = "OK"'
That works fine. So I am not understanding this behavior. I tried every manner of single quotes, double quotes, and escape characters, but nothing seems to work.
Your problem is a misunderstanding of quoting with the cmd parser (as that's what is used when calling powershell.exe):
powershell.exe -Command ""
Everything between the double-quotes will be passed to -Command. In your case, the easiest route to go will be escaping single-quotes (by doubling the count) since cmd handles double-quotes in a special way (usually, by removing them):
powershell.exe -Command "Get-WmiObject -Class Win32_Processor -Filter 'Status = ''OK'''"
As a footnote, root/cimv2 is the default namespace for Get-WmiObject

Disable NetworkAdapter CMD

How can I run this powershell script in cmd?
$adaptor = Get-WmiObject -Class Win32_NetworkAdapter | Where-Object {$_.Name -like "*Wireless*"}
$adaptor.Disable()
Make sure CMD is elevated. You could use WMIC directly from CMD:
wmic nic where "NetConnectionID like '%wireless%'" call disable
you can pass commands to the powershell executeable via the -Command switch and prefixing the script block with &.
powershell -Command "& { $adaptor = Get-WmiObject -Class Win32_NetworkAdapter | Where-Object {$_.Name -like "*Wireless*"}; $adaptor.Disable() }"
You can read more by running the command powershell -?

One-liner PowerShell script

I've created this basic one-liner PowerShell script which executes if I run the ad cmdlet for AD and then the rest of the query. But trying to run them together in line it only seems to load the cmdlet and doesn't execute the rest of the cmd.
powershell.exe -command "&{Import-Module ActiveDirectory; Get-AdGroup -Server DC.Mydomain.com -filter 'name -eq "xxxx"'| set-Adgroup -Replace #{wWWHomePage='10.24.218.194'}}"
Why doesn't it run all together like this?
The answer was to escape the double quotes:
powershell.exe -noprofile -command "&Import-Module ActiveDirectory; Get-AdGroup -Server server.mydomain.com -filter 'name -eq *\"xxxx\"*'| set-Adgroup -Replace #{wWWHomePage='10.10.10.10'}"
Basically, I'm running this from SQL to update an ActiveDirectory attribute that isn't accessible with DSADD.
# Start-Run, type:
powershell.exe -noprofile -command "[guid]::newguid()|Set-Clipboard"
It looks like a quoting issue. Try to replace the surrounding filter quotes with braces:
-filter {name -eq "xxxx"}
To avoid these kind of situations, when you have long list commands to execute, I suggest you put the commands in a script file and pass its path to the -File parameter.
If in doubt with complex commands you can try encoding them in Base64 and using -EncodedCommand. See powershell /? for an example.
Does the line work as intended when you enter it directly in PowerShell?