2015-06-16 19 views
1

我在這裏有一個腳本,它讀取計算機列表並更改管理員密碼。當腳本運行時,它會有一個日誌文件,說明任務是成功還是失敗。但是我也想在失敗時將實際的錯誤記錄到日誌中。我該如何做到這一點?腳本失敗時記錄實際錯誤

[cmdletbinding()] 
param (
[parameter(mandatory = $true)] 
$InputFile, 
$OutputDirectory 
) 

if(!$outputdirectory) { 
    $outputdirectory = (Get-Item $InputFile).directoryname 
} 
$failedcomputers = Join-Path $outputdirectory "failed-computers.txt" 
$stream = [System.IO.StreamWriter] $failedcomputers 
$stream.writeline("ComputerName `t IsOnline `t PasswordChangeStatus") 
$stream.writeline("____________ `t ________ `t ____________________") 

$password = Read-Host "Enter the password" -AsSecureString 
$confirmpassword = Read-Host "Confirm the password" -AsSecureString 

$pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)) 
$pwd2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($confirmpassword)) 

if($pwd1_text -ne $pwd2_text) { 
    Write-Error "Entered passwords are not same. Script is exiting" 
    exit 
} 

if(!(Test-Path $InputFile)) { 
    Write-Error "File ($InputFile) not found. Script is exiting" 
    exit 
} 

$Computers = Get-Content -Path $InputFile 

foreach ($Computer in $Computers) { 
    $Computer = $Computer.toupper() 
    $Isonline = "OFFLINE" 
    $Status  = "SUCCESS" 
    Write-Verbose "Working on $Computer" 
    if((Test-Connection -ComputerName $Computer -count 1 -ErrorAction 0)) { 
     $Isonline = "ONLINE" 
     Write-Verbose "`t$Computer is Online" 
    } else { Write-Verbose "`t$Computer is OFFLINE" } 

    try { 
     $account = [ADSI]("WinNT://$Computer/lchuaadmin,user") 
     $account.psbase.invoke("setpassword",$pwd1_text) 
     Write-Verbose "`tPassword Change completed successfully" 
    } 
    catch { 
     $status = "FAILED" 
     Write-Verbose "`tFailed to Change the administrator password. Error: $_" 
    } 

    $obj = New-Object -TypeName PSObject -Property @{ 
     ComputerName = $Computer 
     IsOnline = $Isonline 
     PasswordChangeStatus = $Status 
    } 

    $obj | Select ComputerName, IsOnline, PasswordChangeStatus 

    if($Status -eq "FAILED" -or $Isonline -eq "OFFLINE") { 
     $stream.writeline("$Computer `t $isonline `t $status") 
    } 

} 
$stream.close() 
Write-Host "`n`nFailed computers list is saved to $failedcomputers" 

回答

0

更改此:

catch { 
    $status = "FAILED" 
    Write-Verbose "`tFailed to Change the administrator password. Error: $_" 
} 

這樣:

catch { 
    $status = "FAILED" 
    Write-Verbose "`tFailed to Change the administrator password. Error: $_" 
    $errmsg = $_.Exception.Message 
}

保留錯誤消息(一個或多個)。而改變這種:

if($Status -eq "FAILED" -or $Isonline -eq "OFFLINE") { 
    $stream.writeline("$Computer `t $isonline `t $status") 
} 

這樣:

if($Status -eq "FAILED" -or $Isonline -eq "OFFLINE") { 
    $stream.writeline("$Computer `t $isonline `t $status `t $errmsg") 
}

包括日誌中的錯誤消息。


如果要展開內部異常,以及你可以使用這個:

$e = $_.Exception 
$errmsg = $e.Message 
while ($e.InnerException) { 
    $e = $e.InnerException 
    $errmsg = "`n" + $e.Message 
} 

,而不是僅僅

$errmsg = $_.Exception.Message 
+0

嗨@Ansgar Wiechers,謝謝您的回答!如果假設我希望記錄每個失敗和成功,那麼如果($ Status -eq「FAILED」 - 或「SUCCESS」 - 或$ Isonline -eq「OFFLINE」 - 或「ONLINE」 「){ – jayteezer

+1

您不希望爲SUCCESS記錄錯誤消息,因此您應該使用'if'或'switch'控制結構來區分日誌消息。 –

+0

謝謝隊友!現在完美運作。 :) – jayteezer