删除所有 SAP 版本

Remove all SAP versions

我正在编写一个脚本来删除所有版本的 SAP,这就是我所拥有的

$uninstall=Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object displayname -like "*SAP*" | select uninstallstring
foreach ($app in $uninstall)
{
    Start-Process "$uninstall" -verb runas -Wait

}


Start-Process : This command cannot be run due to the error: The system cannot find the file specified.
At line:5 char:9
+         Start-Process "$uninstall" -verb runas -Wait
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

Start-Process : This command cannot be run due to the error: The system cannot find the file specified.
At line:5 char:9
+         Start-Process "$uninstall" -verb runas -Wait
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Start-Process],   InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

我猜你想试试

Start-Process "$app" -Verb RunAs -Wait

$uninstall代表全集,$app为单品。

此外,$uninstall 不符合您的预期,您应该这样尝试:

regPath = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"

$uninstall = Get-ItemProperty $regPath |
    Where-Object DisplayName -like "*SAP*" |
    Select-Object -ExpandProperty UninstallString -ErrorAction SilentlyContinue

foreach ($app in $uninstall) {
    #do something with $app
}

-ExpandProperty 确保您只获得 $uninstall.

中列出的 UninstallString

-ErrorAction SilentlyContinue 删除因缺少 UninstallString 值而导致的错误。

最后但同样重要的是,我尝试 运行 使用 Start-Process 命令但失败了,您将不得不使用另一种方法来执行卸载命令。

我认为你需要这样做:

foreach ($app in $uninstall) {
    Start-Process -FilePath 'C:\Windows\System32\cmd.exe' -ArgumentList '/C',$app -Verb RunAs;
}

问题是 UninstallString 通常包含带有参数 的可执行文件 的路径。 Start-ProcessInvoke-Expression 和调用运算符 (&) 都不是那样的。他们想要可执行文件的路径,然后参数在另一个列表中。

比较:

Start-Process 'msiexec.exe /?';

有:

Start-Process 'C:\Windows\System32\cmd.exe' -ArgumentList '/C','msiexec.exe /?';

另一种选择是尝试解析 UninstallString 并将参数分开,但这很糟糕。