从 wmic.exe 调用中提取兴趣线
Extract line of interest from a wmic.exe call
我在 powershell 上执行以下命令:
$objWMI = wmic product where "name like 'app' " get PackageCache
echo $objWMI
输出:
PackageCache
C:\Windows\Installer\file.msi
我需要转换这个输出只是为了显示这个目录。
一旦命令成功运行,您示例中的变量将转换为数组,路径始终显示在第 3 行,因此您只需调用 $objWMI[2] 即可为您提供仅目录(我们使用数字 2,因为如果我们想要第一行,我们将使用 [0])。
总而言之,它可能像下面这样简单。
$objWMI = wmic product where "name like 'app' " get PackageCache
$directory=$objWMI[2]
echo $directory
提供了一个有效的解决方案 ,尽管有两个 很好的理由使用 不同的方法:
wmic.exe
CLI 是 deprecated(PowerShell 的 *-Wmi*
cmdlet 也是 Get-WmiObject
)
作为一个外部程序,它只能将文本传送到PowerShell,然后需要解析该文本以获取感兴趣的信息,这既麻烦又脆弱。
使用 Get-CimInstance
cmdlet 避免了这两个问题:CIM cmdlet 是与 WMI 交互的推荐方式,并且作为本机 PowerShell cmdlet,Get-CimInstance
returns objects 而不是文本,其 properties 您可以以稳健的方式访问 :
# Get the Win32_Product instance of interest and output its
# .PackageCache propertly value.
(Get-CimInstance Win32_Product -Filter 'Name like "app"').PackageCache
上面直接输出一个字符串如C:\Windows\Installer\file.msi
;只需在前面加上 $packagePath =
之类的内容即可将其捕获到变量中。
警告:
应该避免Win32_Product
CIM/WMIclass,如果可能的话,因为its use is excessively slow and can have side effects(重点加了):
Win32_product Class is not query-optimized. Queries such as “select * from Win32_Product where (name like 'Sniffer%')” require WMI to use the MSI provider to enumerate all of the installed products and then parse the full list sequentially to handle the “where” clause. This process also initiates a consistency check of packages installed, verifying and repairing the install. With an account with only user privileges, as the user account may not have access to quite a few locations, may cause delay in application launch and an event 11708 stating an installation failure.
我在 powershell 上执行以下命令:
$objWMI = wmic product where "name like 'app' " get PackageCache
echo $objWMI
输出:
PackageCache
C:\Windows\Installer\file.msi
我需要转换这个输出只是为了显示这个目录。
一旦命令成功运行,您示例中的变量将转换为数组,路径始终显示在第 3 行,因此您只需调用 $objWMI[2] 即可为您提供仅目录(我们使用数字 2,因为如果我们想要第一行,我们将使用 [0])。
总而言之,它可能像下面这样简单。
$objWMI = wmic product where "name like 'app' " get PackageCache
$directory=$objWMI[2]
echo $directory
wmic.exe
CLI 是 deprecated(PowerShell 的*-Wmi*
cmdlet 也是Get-WmiObject
)作为一个外部程序,它只能将文本传送到PowerShell,然后需要解析该文本以获取感兴趣的信息,这既麻烦又脆弱。
使用 Get-CimInstance
cmdlet 避免了这两个问题:CIM cmdlet 是与 WMI 交互的推荐方式,并且作为本机 PowerShell cmdlet,Get-CimInstance
returns objects 而不是文本,其 properties 您可以以稳健的方式访问 :
# Get the Win32_Product instance of interest and output its
# .PackageCache propertly value.
(Get-CimInstance Win32_Product -Filter 'Name like "app"').PackageCache
上面直接输出一个字符串如C:\Windows\Installer\file.msi
;只需在前面加上 $packagePath =
之类的内容即可将其捕获到变量中。
警告:
应该避免Win32_Product
CIM/WMIclass,如果可能的话,因为its use is excessively slow and can have side effects(重点加了):
Win32_product Class is not query-optimized. Queries such as “select * from Win32_Product where (name like 'Sniffer%')” require WMI to use the MSI provider to enumerate all of the installed products and then parse the full list sequentially to handle the “where” clause. This process also initiates a consistency check of packages installed, verifying and repairing the install. With an account with only user privileges, as the user account may not have access to quite a few locations, may cause delay in application launch and an event 11708 stating an installation failure.