首页 > 解决方案 > 通过 Powershell 卸载程序

问题描述

我正在尝试通过 Powershell 卸载一些软件,并遇到了一些问题。作为一个完整的菜鸟,我正在通过反复试验将其拼凑起来。

我正在运行的脚本

 ##################
    $appName = 'Automox'
    ##################

    # Get all entries that match our criteria. DisplayName matches $appname, DisplayVersion less than current
    $installed = @(Get-ChildItem $uninstReg -ErrorAction SilentlyContinue | Get-ItemProperty | Where-Object { ($_.DisplayName -match $appName) })
    
    # Initialize an array to store the uninstalled app information
    $uninstalled = @()

    # Start a loop in-case you get more than one match, uninstall each.
    foreach ($version in $installed) {
        #For every version found, run the uninstall string
        $uninstString = $version.UninstallString
        #If exe run as written + silent argument, if msiexec run as msi using the name of the reg key as the msi guid.
        if ($uninstString -match 'msiexec') {
            $process = Start-Process msiexec.exe -ArgumentList "/x $($version.PSChildName) /qn REBOOT=ReallySuppress" -Wait -PassThru
        } else {
            $process = Start-Process $uninstString -ArgumentList '/S' -Wait -PassThru
        }
        
        # Check exit code for success/fail
        # Using 3 "-eq" statements becuase older PowerShell doesn't support "-in"
        # If unsuccessful, don't add to uninstalled list.
        if ( ($process.ExitCode -eq '0') -or ($process.ExitCode -eq '1641') -or ($process.ExitCode -eq '3010') ) {
            $uninstalled += $version.PSPath
        }
    }
    
    return $uninstalled
}


$uninstalledApps = & "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptblock

# Use Write-Output so you can see a result in the Activity Log
Write-Output "$uninstalledApps"

if ($uninstalledApps) {
    Exit 0
} else { Exit 1 }

这是我得到的错误

At line:31 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

标签: powershelluninstallation

解决方案


推荐阅读