Powershell 中的错误级别不反映所采取的措施

Powershell 中的错误级别不反映所采取的措施

我使用以下 powershell 脚本静默安装 Chrome。但是成功安装 Chrome 后显示的错误消息并不反映事实。它显示错误消息“发现错误,更改为手动安装”,并将安装文件复制到目标而不是“Chrome 已安装”。请检查我的脚本出了什么问题。谢谢。

$uri = "https://dl.google.com/chrome/install/latest/chrome_installer.exe"
$Path = $env:TEMP; $Installer = "chrome_installer.exe"; Invoke-WebRequest $uri -OutFile 
$Path$Installer; Start-Process -FilePath $Path$Installer -Args "/silent /install" -Verb RunAs 
-Wait; Remove-Item $Path$Installer
if( $LASTEXITCODE -eq 0 ) {
    Write-Output "Chrome Installed"
    Start-Sleep -Seconds 3
} else {
Write-Output "Error found, Change to Manual Installation"
    Start-Sleep -Seconds 5
    Copy-Item -Path "${PSScriptRoot}\Software\Browsers\ChromeStandaloneSetup64.exe" - 
    Destination "$($env:USERPROFILE)\downloads"
    Start $env:userprofile\downloads
}
exit

答案1

根据我的评论。有点像这样...(当然,你不需要所有这些。我只是让它变得嘈杂起来以便看东西)

# Chrome Silent installation

$VerbosePreference = 'Continue'

Clear-Host
Try
{
    $uri  = 'https://dl.google.com/chrome/install/latest/chrome_installer.exe'
    $Path = $env:TEMP

    $Installer = 'chrome_installer.exe'
    Invoke-WebRequest $uri -OutFile $Path$Installer

    Get-ChildItem -Path $Path$Installer

    Start-Process -FilePath $Path$Installer -Args "/silent /install" -Verb RunAs -Wait
    Write-Host 'Chrome Installed' -ForegroundColor Green

    Remove-Item -Path $Path$Installer -WhatIf
    Remove-Item -Path $Path$Installer -Verbose

    Get-ChildItem -Path $Path$Installer
}
Catch 
{
    Write-Error -Message 'Error found, Change to Manual Installation'
    $PSItem[0].Exception.Message
    $Error.Clear()
}

$VerbosePreference = 'SilentlyContinue'

# Results
<#
VERBOSE: GET https://dl.google.com/chrome/install/latest/chrome_installer.exe with 0-byte payload
VERBOSE: received 1427176-byte response of content type application/octet-stream


    Directory: C:\Users\WDAGUtilityAccount\AppData\Local


Mode                 LastWriteTime         Length Name                                                                                                      
----                 -------------         ------ ----                                                                                                      
-a----         8/29/2022  12:20 PM        1427176 Tempchrome_installer.exe                                                                                  
Chrome Installed
What if: Performing the operation "Remove File" on target "C:\Users\WDAGUtilityAccount\AppData\Local\Tempchrome_installer.exe".
VERBOSE: Performing the operation "Remove File" on target "C:\Users\WDAGUtilityAccount\AppData\Local\Tempchrome_installer.exe".
...
#>


# Chrome Silent installation

$VerbosePreference = 'Continue'

Clear-Host
Try
{
    $uri  = 'https://dl.google.com/chrome/install/latest/NOT_chrome_installer.exe'
    $Path = $env:TEMP

    $Installer = 'chrome_installer.exe'
    Invoke-WebRequest $uri -OutFile $Path$Installer

    Get-ChildItem -Path $Path$Installer

    Start-Process -FilePath $Path$Installer -Args "/silent /install" -Verb RunAs -Wait
    Write-Host 'Chrome Installed' -ForegroundColor Green

    Remove-Item -Path $Path$Installer -WhatIf
    Remove-Item -Path $Path$Installer -Verbose

    Get-ChildItem -Path $Path$Installer
}
Catch 
{
    Write-Error -Message 'Error found, Change to Manual Installation'
    $PSItem[0].Exception.Message
    $Error.Clear()
}

$VerbosePreference = 'SilentlyContinue'

# Results
<#
VERBOSE: GET https://dl.google.com/chrome/install/latest/NOT_chrome_installer.exe with 0-byte payload
...
 
The remote server returned an error: (404) Not Found.
#>

相关内容