1

こんにちは、dism からのエラーを cat.. で処理しようとしましたが、うまくいきませんでした。だから私の質問は、どうすればそれを改善できるかです。

function dotnet35 () {
    WriteLogNewScirpt "dotnet35"
    WriteLogInstruction "installing .Net 3.5 online"
    $Errorccured=$false
    $Error.Clear()
    try {
    $ErrorActionPreference = 'stop'
        Start-Process -FilePath powershell.exe -ArgumentList {DISM /Online /Enable-Feature /FeatureName:NetFx3 /All} -verb RunAs -WindowStyle Hidden | Out-Default
    } catch {
        WriteLogError ".Net 3.5 could not be installed"
        WriteLogError "$Error"
        $Errorccured=$true  
    }
    if(!$Errorccured) {
        WriteLogPosisitive ".Net 3.5 installed"       
    } else {
        dotnet35offline
    }
}

function dotnet35offline () {
    WriteLogInstruction "installing .Net 3.5 offline"
    $Erroroccured=$false
    $Error.Clear()
    try {
        $ErrorActionPreference = 'stop'
        Start-Process -FilePath powershell.exe -ArgumentList {DISM /Online /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:c:\scripts\sources\features\sxs} -verb RunAs | Out-Default
    } catch {
        WriteLogError ".Net 3.5 could not be installed"
        WriteLogError "$Error"
        $Erroroccured=$true
    }
    if(!$Erroroccured) {
        WriteLogPosisitive ".Net 3.5 konnte offline installiert werden"
    }
} 

私の翻訳で申し訳ありませんが、静かに速く翻訳しました^^

前もって感謝します

4

1 に答える 1

0

終了エラーは、PowerShell 関数と .NET ライブラリでスローされた例外にのみ影響します。外部コマンド(実行可能ファイルなど)の結果を確認する場合は、変数を確認する必要があります。これは、バッチ スクリプト$LASTEXITCODEの変数と同じ目的を果たします。%ERRORLEVEL%例えば:

Start-Process -FilePath dism.exe -ArgumentList '/Online', '/Enable-Feature', '/FeatureName:NetFx3', '/All' -Verb RunAs -WindowStyle Hidden
if( $LASTEXITCODE -ne 0 ){
  # Handle the error here
  # For example, throw your own error
  throw "dism.exe failed with exit code ${LASTEXITCODE}"
}

Out-Defaultまた、コードを呼び出さないでください。この用途向けには設計されていません。

于 2020-01-15T23:52:05.423 に答える