如何解决Powershell 循环遍历每个用户配置文件以获取软件版本号,然后创建卸载字符串并运行命令
希望有人能给我一个关于如何处理剩余脚本的想法。 该脚本是从该构建中获取已安装 Chrome 的版本号 为卸载构建一个字符串,如下所示。
我被困在第二部分,获取版本号很好。
- 接下来的逻辑是什么,然后遍历每个用户配置文件以从 C:\Users[username]\appdata\Local\Google\Chrome\Application\90.0.4430.72\Installer 运行 setup.exe。我得到的错误是 { & $unin} 上无法识别的 cmdlet
谢谢
#UserHives - Find all the user profiles in the registry
$UserHives = Get-ChildItem Registry::HKEY_USERS\ |Where-Object {$_.Name -match '^HKEY_USERS\\S-1-5-21-[\d\-]+$'}
$UserProfile = $Env:USERPROFILE
#
foreach($user in $UserHives)
{
#1.Get Version Of chrome
#1. PATH TO SEARCH FOR
$Path = Join-Path $user.PSPath "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome"
If (Test-Path $Path){
$GetVersion = Get-ItemProperty -Path $Path | Select-Object -Property Version
$VersionInstalled = $GetVersion.Version
#create uninstallstring
$UninString = "\Google\Chrome\Application\$VersionInstalled\Installer\setup.exe --uninstall --Channel --chrome --force-uninstall"
$unin = $UserProfile + "" + $UninString
If($VersionInstalled){ & $unin}
}
}
解决方法
引自docs:
调用运算符不解析字符串。这意味着你不能 使用调用运算符时,请在字符串中使用命令参数。
单独传递参数:
$uninArgs = "--uninstall","--Channel","--chrome","--force-uninstall"
$uninExe = "$UserProfile\Google\Chrome\Application\$VersionInstalled\Installer\setup.exe"
if ($VersionInstalled) {
& $uninExe $uninArgs
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。