[英]How to create wmic powershell script
我对Powershell命令非常陌生,我正努力朝着能够创建简单脚本的方向发展。 例如,我正在研究一个脚本,该脚本将按顺序运行以下命令:
命令1:WMIC
命令2:name =“ Cloud Workspace Client”调用的产品卸载/ nointeractive
第二条命令取决于首先运行的第一条命令。 但是,我不确定如何实现成功执行此操作的脚本。 我只知道单个命令,但不知道如何将它们串在一起。
任何帮助,建议或资源链接将不胜感激!
正如Ansgar所提到的,在PowerShell中有处理WMI类的本地方法。 因此,使用wmic.exe
被认为是不好的做法。 有趣的是,撰写导致PowerShell的Monad宣言的Jeffrey Snover也致力于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 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.