1 votes

Le niveau d'erreur dans Powershell ne reflète pas l'action entreprise

J'utilise le powershell script ci-dessous pour installer Chrome silencieusement. Cependant, le message d'erreur affiché ne reflète pas la vérité après l'installation réussie de Chrome. Il a affiché le message d'erreur "Error found, Change to Manual Installation" et a copié le fichier d'installation vers la destination au lieu de "Chrome Installed". Veuillez examiner ce qui n'a pas fonctionné avec mon script. Je vous remercie.

$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

1voto

postanote Points 3762

Comme je l'ai dit dans mon commentaire. Un peu comme ceci... (bien sûr, vous n'avez pas besoin de tout cela, je l'ai juste rendu plus facile à voir)

# 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.
#>

SistemesEz.com

SystemesEZ est une communauté de sysadmins où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres sysadmins, poser vos propres questions ou résoudre celles des autres.

Powered by:

X