如何创建wmic powershell脚本

问题描述 投票:1回答:1

我是powershell命令的新手,我正在努力创建简单的脚本。例如,我正在编写一个脚本,它将按顺序运行以下命令:

命令1:wmic

命令2:product其中name =“Cloud Workspace Client”调用uninstall / nointeractive

第二个命令取决于首先运行的第一个命令。但是,我不确定如何实现成功执行此操作的脚本。我只知道各个命令,但不知道如何将它们串在一起。

任何帮助,建议或资源链接将不胜感激!

powershell wmic
1个回答
2
投票

正如Ansgar所提到的,有一些本机方法可以在PowerShell中处理WMI类。所以使用wmic.exe被认为是不好的做法。有趣的是Jeffrey Snover撰写了导致PowerShell的Monad宣言,他也参与了wmic.exe

用于使用WMI的PowerShell cmdlet是WMI cmdlet,但在PowerShell 3.0及更高版本中,有更好的CIM cmdlet。这是您可以在WMI查询返回的对象上调用Uninstall方法的一种方法。

(Get-WMIObject Win32_Product -Filter 'name="Cloud Workspace Client"').Uninstall()

但是...... Win32_Product类是臭名昭着的,因为每次调用它时,都会强制对所有msi安装程序进行一致性检查。因此,最佳做法是查看注册表中的Uninstall密钥并使用其中的信息。这是更多的工作,但不会导致一致性检查。

#Uninstall Key locations
$UninstallKey = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\"
$Uninstall32Key = "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"

#Find all of the uninstall keys
$AllUninstallRegistryKeys = @($(Get-ChildItem $uninstallkey),$(Get-ChildItem $uninstall32key -ErrorAction SilentlyContinue))

#Get the properties of each key, filter for specific application, store Uninstall property
$UninstallStrings = $AllUninstallRegistryKeys | ForEach-Object {
    Get-ItemProperty $_.pspath | Where-Object {$_.DisplayName -eq 'Cloud Workspace Client'}
} | Select-Object -ExpandProperty UninstallString
#Run each uninstall string
$UninstallStrings | ForEach-Object { & $_ }

如果您有PowerShell 5+,现在还有PackageManagement cmdlet。

Get-Package 'Cloud Workspace Client' | Uninstall-Package
© www.soinside.com 2019 - 2024. All rights reserved.