Home > Net >  powershell get-wmiobject wildcards - Batch File
powershell get-wmiobject wildcards - Batch File

Time:11-03

powershell get-wmiobject wildcards - Batch File

I´m struggling with on batch file here.. The idea is to get the version of the applications installed on a remote pc, but instead of writing the full application name ( that can be a bit different from client to client) i want to use a Wildcard for it..

Unfortunantly GET-WmiObject operator, does not work the the parameter ( -like) as SQL..

The powershell command below works like i want , but when i add it the the batch file, it does not work...

Power Shell command that work :

[tag:Get-WmiObject -ComputerName ##PCNAMEHERE## -Query "Select * From Win32_Product Where name Like 'McAfee%'" | select Name, Version]

Line on the batch program

\[tag:get specific app  with Wildcards !!!!!  Need corrections !!!

Powershell.exe Get-WmiObject -ComputerName %CN% -Query Select \* From Win32_Product Where name Like ''McAfee%'' | select Name, Version \>\>%LOG%\]

Error that i get on the batch execution... Already try to play with " and ' but without success ...

Get-WmiObject : A positional parameter cannot be found that accepts argument '*'. At line:1 char:1

  • Get-WmiObject -ComputerName W1865C28 -Query Select * From Win32_Produ ...
  •     CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
        FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand
    

CodePudding user response:

If you want to run this command from the cmd you should use the parameter -command and also query only the needed attributes, e.g.:

Powershell.exe -command "Get-WmiObject -ComputerName %CN% -Query ""Select Name,Version From Win32_Product Where name Like 'McAfee%'"""

But in principle as you are running a batch file/work with the cmd you could also use wmic:

wmic product where "name like 'mcafee%'" get name,version

https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmic

If you want to stay with PowerShell you should use the CIM cmdlets which replaced the get-wmiobject cmdlet, e.g.:

Powershell.exe -command "get-ciminstance -ComputerName %CN% -Query ""Select Name,Version From Win32_Product Where name Like 'McAfee%'"""

But note that the CIM cmdlets relay on WinRM for remoting.

CodePudding user response:

This way would avoid a lot of quoting issues:

Powershell.exe "Get-WmiObject -ComputerName %CN% Win32_Product | Where name -Like *mcafee* | select Name, Version >> %LOG%"

Even better, since win32_product is so slow, and >> might mix encodings, but uses remote powershell:

powershell "invoke-command %cn% { get-package *mcafee* } | select name, version | add-content %log%"
  • Related