Powershell 循环遍历每个用户配置文件以获取软件版本号,然后创建卸载字符串和 运行 命令

Powershell to Loop through each user profile to get Version number of software and then create uninstall string and run the command

希望有人能给我一个关于如何处理剩余脚本的想法。 该脚本是从该构建中获取已安装 Chrome 的版本号构建一个用于卸载的字符串,如下所示。

我卡在第二部分了,获取版本号没问题。

  1. 接下来的逻辑是什么,然后从 C:\Users[username]\appdata\Local\Google\Chrome\Application.0.4430.[=23= 遍历每个用户配置文件到 运行 setup.exe ].我收到的错误是 { & $unin}
  2. 上无法识别的 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

The call operator does not parse strings. This means that you cannot use command parameters within a string when you use the call operator.

单独传递参数:

$uninArgs = "--uninstall", "--Channel", "--chrome", "--force-uninstall"
$uninExe = "$UserProfile\Google\Chrome\Application$VersionInstalled\Installer\setup.exe"
if ($VersionInstalled) {
    & $uninExe $uninArgs
}