首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法运行PowerShell卸载脚本

无法运行PowerShell卸载脚本
EN

Stack Overflow用户
提问于 2017-10-10 10:01:51
回答 3查看 2.3K关注 0票数 3

我正在尝试启动一个进程并等待退出代码。该进程使用参数列表启动msiexec。当我运行我的脚本时,它会产生参数帮助向导,但是如果我在CMD中直接运行由:

代码语言:javascript
复制
write-host $command

它如预期的那样工作。这是我的完整剧本:

代码语言:javascript
复制
# 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
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-10-10 10:23:50

而不是这个

代码语言:javascript
复制
$command = "$uninst /qn /L*V ""C:\UninstallVisio.txt"""

试一试

代码语言:javascript
复制
$command = @(
    $uninst
    "/qn"
    "/L*V"
    '"C:\UninstallVisio.txt"'
     )

来源:参见最后一个示例https://kevinmarquette.github.io/2016-10-21-powershell-installing-msi-files/

票数 2
EN

Stack Overflow用户

发布于 2017-10-10 10:33:35

代码语言:javascript
复制
#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
}
票数 1
EN

Stack Overflow用户

发布于 2017-10-10 10:08:45

看起来您正在尝试使用ArgumentList "msiexec.exe /x {ProductCode}“运行msiexec.exe。

Powershell正在尝试以"msiexec.exe msiexec.exe /x {ProductCode}“的形式运行命令。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46663935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档