如何创建 wmic powershell 脚本

Ty *_*Flo 1 powershell wmic

我对 powershell 命令很陌生,我正试图朝着能够创建简单脚本的方向发展。例如,我正在编写一个脚本,它将按顺序运行以下命令:

命令 1:wmic

命令 2:product where name="Cloud Workspace Client" call uninstall /nointeractive

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

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

Ben*_*enH 5

正如 Ansgar 所提到的,在 PowerShell 中有处理 WMI 类的本机方法。因此,使用wmic.exe. 有趣的是,编写了导致 PowerShell 的 Monad 宣言的 Jeffrey Snover 也致力于wmic.exe.

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

(Get-WMIObject Win32_Product -Filter 'name="Cloud Workspace Client"').Uninstall()
Run Code Online (Sandbox Code Playgroud)

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

#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 { & $_ }
Run Code Online (Sandbox Code Playgroud)

更进一步,如果您有 PowerShell 5+,现在还有 PackageManagement cmdlet。

Get-Package 'Cloud Workspace Client' | Uninstall-Package
Run Code Online (Sandbox Code Playgroud)