首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Get-WMIObject卸载vs Get-CIMInstance卸载

Get-WMIObject卸载vs Get-CIMInstance卸载
EN

Stack Overflow用户
提问于 2021-04-07 00:21:28
回答 2查看 2.9K关注 0票数 2

也许是个愚蠢的问题但我只是好奇。

Get-CIMInstanceGet-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卸载多个应用程序所做的工作

代码语言:javascript
复制
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 /卸载

代码语言:javascript
复制
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)
#>

恕我直言,只是一个好奇的想法。

如果不允许,请删除/关闭。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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`返回的对象。

代码语言:javascript
复制
$instance = Get-CimInstance win32_process -Filter "Name = 'powershell_ise.exe'"
$instance | Invoke-CimMethod -MethodName 'Terminate'

您可以使用Get-CimClass发现方法。

代码语言:javascript
复制
(Get-CimClass win32_process ).CimClassMethods

如果需要给定方法的参数,可以使用哈希表作为参数通过-Arguments参数传递它们。您可以在帮助文件或这里中找到示例。

您也可以直接使用Invoke-WMIMethod:

代码语言:javascript
复制
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已知的性能问题。

票数 6
EN

Stack Overflow用户

发布于 2021-04-07 03:36:39

或者使用get-package,假设它是msi安装:

代码语言:javascript
复制
get-package *visual* | uninstall-package

众所周知,win32_product类速度慢,因为它在使用时会验证所有的msi。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66978090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档