powershell获取wmiobject通配符-批处理文件



powershell获取wmiobject通配符-批处理文件

我正在处理这里的批处理文件。。这个想法是在远程电脑上安装应用程序的版本,但我不想写完整的应用程序名称(客户端之间可能有点不同(,而是想使用通配符。

不幸的是,GET WmiObject运算符不将参数(-like(用作SQL。。

下面的powershell命令可以像我想要的那样工作,但当我将其添加到批处理文件时,它不起作用。。。

PowerShell命令有效:

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

批处理程序上的行

[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%]

我在批处理执行中出错。。。已经尝试玩";"但没有成功…"。。。

Get-WmiObject:找不到接受参数"*"的位置参数。在行:1个字符:1个

  • 从Win32_Produ获取WmiObject-ComputerName W1865C28-查询选择*
  • + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetWmiObjectCommand
    

如果您想从cmd运行此命令,您应该使用参数-command,并且只查询所需的属性,例如:

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

但原则上,当你运行批处理文件/使用cmd时,你也可以使用wmic:

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

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

如果你想继续使用PowerShell,你应该使用CIM cmdlet,它取代了get-wmiobject cmdlet,例如:

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

但请注意,CIM cmdlet在WinRM上中继以进行远程处理。

这样可以避免很多报价问题:

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

更好的是,由于win32_product非常慢,并且>gt;可能混合编码,但使用远程powershell:

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

最新更新