选择要删除的Netweaver版本

时间:2016-06-30 13:11:52

标签: powershell sap

我已经做到这一点,但它删除了所有版本的SAP我只想删除某些版本。我已经尝试更改我正在搜索的内容,但它仍会删除所有版本的SAP

$y = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
            Get-ItemProperty |
                Where-Object {$_.DisplayName -match 'SAP Netweaver Business Client 3.5' } |
                    Select-Object -Property DisplayName, UninstallString, PSPath

        foreach ($x in $y) 
        {
            if ($x.UninstallString) 
            {
                $uninst = "C:\Program Files (x86)\SAP\SapSetup\Setup\nwsapsetup.exe" 
                Start-Process $uninst -ArgumentList "/uninstall /nodlg /force"
            } 
         }

1 个答案:

答案 0 :(得分:0)

最终剧本

这将删除所有版本的Netweaver。自从我的同事创造了这个以来,我无法承担功劳。

$y = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
    Get-ItemProperty |
        Where-Object {$_.DisplayName -match "SAP Netweaver Business Client" -and $_.Publisher -match "SAP AG" } |
            Select-Object -Property DisplayName, UninstallString, PSPath

foreach ($x in $y) {
if ($x.UninstallString -like "MsiExec.exe*") {
   $x.UninstallString = $x.UninstallString -replace "/i","/x" #replace /i with /x to ensure uninstall command is executed
   $x.UninstallString = $x.UninstallString -replace "MsiExec.exe ","" #remove MsiExec.exe so that only arguements are left
   $x.UninstallString = "$($x.UninstallString) /qn" #append the /qn for silent uninstall
    Start-Process -filepath "MsiExec.exe" -ArgumentList "$($x.UninstallString)" -Wait -WindowStyle Hidden
} else {
   #check to see if the UninstallString contains quotes
    $x.UninstallString = "$($x.UninstallString) /nodlg" 
    Start-Process cmd.exe -ArgumentList "/c `"$($x.UninstallString)`"" -WindowStyle Hidden -Wait
    write-host "$($x.displayname) uninstalled"
}
}