簡體   English   中英

無法運行PowerShell卸載腳本

[英]Unable to run PowerShell uninstall script

我正在嘗試啟動進程並等待退出代碼。 該過程使用參數列表啟動msiexec。 當我運行我的腳本時,它會出現參數幫助向導,但是如果我直接在CMD中運行命令生成的:

write-host $command

它按預期工作。 這是我的完整腳本:

# Get uninstall strings from registry, looking for the msiexec option
$applist = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
    Get-ItemProperty |
        Where-Object {$_.DisplayName -match "Microsoft Visio Standard 2013" -and $_.UninstallString -match "msiexec"} |
            Select-Object -Property DisplayName, UninstallString

# Check for any aplications requiring uninstall and output their names
if ($applist) {
    foreach ($app in $applist) {
        Write-host "$($app.DisplayName) has been detected for uninstall"
    }

    Write-host "Attempting to uninstall application(s)"

    # Uninstall each application that has been identified
    foreach ($app in $applist) {
        try {
             $uninst = $app.UninstallString
             $pos = $uninst.IndexOf(" ")
             $leftPart = $uninst.Substring(0, $pos)
             $rightPart = $uninst.Substring($pos+1)
             $command = """$rightPart /qn /L*V ""C:\UninstallVisio.txt"""""
             write-host $command
            $uninstall = (Start-Process "msiexec.exe" -ArgumentList $command -Wait -Passthru).ExitCode

            if($uninstall.ExitCode -ne 0) {
                write-host "attempting XML config uninstall"
                #**still to be worked on**
            }
        } catch {
            write-host "Unable to uninstall $_.Name Please view logs" 
            Continue
        }
    }
}
Exit
# Exit script as no apps to uninstall
else {
    write-host "No application(s) detected for uninstall"
    Exit
}

而不是這個

$command = "$uninst /qn /L*V ""C:\UninstallVisio.txt"""

嘗試

$command = @(
    $uninst
    "/qn"
    "/L*V"
    '"C:\UninstallVisio.txt"'
     )

資料來源:見最后一個例子https://kevinmarquette.github.io/2016-10-21-powershell-installing-msi-files/

#Get uninstall strings from registry, looking for the msiexec option
$applist = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
    Get-ItemProperty |
        Where-Object {$_.DisplayName -match "Microsoft Visio Standard 2013" -and $_.UninstallString -match "msiexec"} |
            Select-Object -Property DisplayName, UninstallString

#Check for any aplications requiring uninstall and output their names
if ($applist){
foreach ($app in $applist){
Write-host "$($app.DisplayName) has been detected for uninstall"
}


Write-host "Attempting to uninstall application(s)"


#Uninstall each application that has been identified
foreach ($app in $applist){
try
{
        $uninst = $app.UninstallString
        $pos = $uninst.IndexOf(" ")
        $leftPart = $uninst.Substring(0, $pos)
        $rightPart = $uninst.Substring($pos+1)
        $command = @(
        $rightPart
        "/qn"
        "/L*V"
        '"C:\UninstallVisio.txt"'
         )
        write-host $command
        $uninstall = (Start-Process "msiexec.exe" -ArgumentList $command -Wait -Passthru).ExitCode
        If($uninstall.ExitCode -ne 0){
        write-host "attempting XML config uninstall"
        }
       }

catch{
write-host "Unable to uninstall $_.Name Please view logs" 
Continue
    }
Exit
}
}
#Exit script as no apps to uninstall
else {
write-host "No application(s) detected for uninstall"
Exit
}

看起來您正在嘗試使用ArgumentList“msiexec.exe / x {ProductCode}”運行msiexec.exe。

Powershell嘗試以“msiexec.exe msiexec.exe / x {ProductCode}”運行命令

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM