删除所有SAP版本

时间:2016-06-29 13:51:11

标签: powershell sap

我正在编写一个脚本来移除所有版本的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

2 个答案:

答案 0 :(得分:1)

我想你想尝试

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确保您只获得UninstallString中列出的$uninstall值。

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

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

答案 1 :(得分:1)

我认为你需要这样做:

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

问题是UninstallString通常包含带参数的可执行文件的路径。既不是Start-Process也不是Invoke-Expression,也不是调用运算符(&)。他们想要可执行文件的路径,然后将参数放在另一个列表中。

比较

Start-Process 'msiexec.exe /?';

使用:

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

另一个选择是尝试解析UninstallString并将参数拆分,但这非常严重。