2017-10-10 124 views
3

我試圖啓動一個進程並等待退出代碼。該過程使用參數列表啓動msiexec。當我運行我的腳本談到了爭論的幫助嚮導,但是如果我運行所產生的在CMD命令直接:無法運行PowerShell卸載腳本

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 
} 

回答

0

它看起來像你試圖用參數列表「在msiexec.exe/x {產品代碼}」運行msiexec.exe的。

PowerShell是試圖爲 「msiexec.exe的在msiexec.exe/x {產品代碼}」 運行命令

+0

好點!將嘗試和修改,看看我現在怎麼樣! –

1
#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 
}