也许是个愚蠢的问题但我只是好奇。
Get-CIMInstance和Get-WMIObject在为Win32_Product类下的应用程序调用卸载时有什么区别吗?我问的唯一原因是:
Get-CIMInstance卸载应用程序,将用某些程序重新启动我的计算机。Get-WMIObject卸载应用程序,无需重新启动即可运行。而且,将Get-Member输送到任何Get-CIMInstance产品,并不能给我卸载的方法,但它确实使用了Get-WMIObject。开发人员就是这样写的吗?尽管如此,Invoke-CIMMethod -Name Uninstall仍然可以工作。
Get-CIMInstance /卸载
以下是我使用Get-CIMInstance/Invoke-CIMMethod -Name Uninstall卸载多个应用程序所做的工作
Get-CimInstance -ClassName win32_product | Where-Object Name -Match "Visual" |
ForEach-Object -Process {
Invoke-CimMethod -InputObject $_ -Name Uninstall
}
#Methods Returned
<#
Get-CimInstance -ClassName win32_product | Where-Object Name -Match "Visual" | Get-Member -MemberType Method
TypeName: Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_Product
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object ICloneable.Clone()
Dispose Method void Dispose(), void IDisposable.Dispose()
Equals Method bool Equals(System.Object obj)
GetCimSessionComputerName Method string GetCimSessionComputerName()
GetCimSessionInstanceId Method guid GetCimSessionInstanceId()
GetHashCode Method int GetHashCode()
GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Ser...
GetType Method type GetType()
ToString Method string ToString()
#>Get-WMIObject /卸载
Get-WMIObject -ClassName win32_product | Where-Object Name -Match "Visual" |
ForEach-Object -Process {
Invoke-WMIMethod -InputObject $_ -Name Uninstall
}
#Methods Returned
<#
Get-WMIObject -Class win32_product | Where-Object Name -Match "Visual" | Get-Member -MemberType Method
TypeName: System.Management.ManagementObject#root\cimv2\Win32_Product
Name MemberType Definition
---- ---------- ----------
Configure Method System.Management.ManagementBaseObject Configure(System.UInt16 InstallState, System.UInt16 InstallLevel, S...
Reinstall Method System.Management.ManagementBaseObject Reinstall(System.UInt16 ReinstallMode)
Uninstall Method System.Management.ManagementBaseObject Uninstall()
Upgrade Method System.Management.ManagementBaseObject Upgrade(System.String PackageLocation, System.String Options)
#>恕我直言,只是一个好奇的想法。
如果不允许,请删除/关闭。
发布于 2021-04-07 00:41:51
在使用WMI和新的cmdlets之间有很多不同。Get-WMIObject在PowerShell中是不推荐的,并且已经从PowerShell核心中删除了,所以一般的建议是使用PowerShell。但是,这些方法的行为不应该不同,所以我无法解释您提到的重新启动行为。
Get-CimInstance don't have the methods, but you can pass them to Invoke-CimMethod`返回的对象。
$instance = Get-CimInstance win32_process -Filter "Name = 'powershell_ise.exe'"
$instance | Invoke-CimMethod -MethodName 'Terminate'您可以使用Get-CimClass发现方法。
(Get-CimClass win32_process ).CimClassMethods如果需要给定方法的参数,可以使用哈希表作为参数通过-Arguments参数传递它们。您可以在帮助文件或这里中找到示例。
您也可以直接使用Invoke-WMIMethod:
Invoke-CimMethod -Query "SELECT * FROM Win32_Process WHERE Name = 'powershell_ise.exe'" -MethodName Terminate我通常不会这样做,因为-Filter与-CLassName一起使用稍微少了一点,而Invoke-CimMethod中缺少的-Filter是不可用的,但是,这些只是个人偏好。
我建议你也读CIM Cmdlet简介
而且,Win32_Product的声誉也很差。如果你谷歌它,你可以得到更多的信息,但这里有一篇文章,我很快找到了通过这样的问题:产品是坏消息
通常情况下,您应该在命令中向左移动过滤。使用-Query或-Filter参数,而不是获取所有实例,然后使用Where{}。特别是考虑到Win32_Product已知的性能问题。
发布于 2021-04-07 03:36:39
或者使用get-package,假设它是msi安装:
get-package *visual* | uninstall-package众所周知,win32_product类速度慢,因为它在使用时会验证所有的msi。
https://stackoverflow.com/questions/66978090
复制相似问题