2012-01-29 6 views
1

Microsoft Online PowerShellコマンドレットで間違ったユーザー名とパスワードを使用すると(ASP.NET Webサイト内から)、例外エラーを処理しようとしています。ここでは、コードのスニップです:私は、try/catchブロックを削除した場合PowerShellで未処理のエラーをトラップする方法

PowerShell.Runspace = myRunSpace 

Dim connect As New Command("Get-MSOnlineUser") 
connect.Parameters.Add("Enabled") 

Dim secureString As New System.Security.SecureString() 
Dim myPassword As String = "Password" 

For Each c As Char In myPassword 
     secureString.AppendChar(c) 
Next 

connect.Parameters.Add("Credential", New PSCredential("[email protected]", secureString)) 
PowerShell.Commands.AddCommand(connect) 

Dim results As Collection(Of PSObject) = Nothing 
Dim errors As Collection(Of ErrorRecord) = Nothing 

results = PowerShell.Invoke() 

Try 
     results = PowerShell.Invoke() 
Catch ex As Exception 

End Try 

errors = PowerShell.Streams.[Error].ReadAll() 

、私は未処理の例外エラーを取得:資格情報が有効ではありません。ユーザー名とパスワードを確認して、もう一度お試しください。ただし、Try/Catchブロックでは、結果コレクションまたはエラーコレクションには何も含まれていません。

このエラーを適切にトラップして、ユーザーに「無効なユーザー名またはパスワード」を提示する方法を教えてください。あなたが探しているものを

答えて

4

は次のとおりです。

$_.Exception.Message 

この変数は、最新の例外エラーを保持しています。 Arcass

try 
{ 
    #Some cmdlet likely to throw exception 
} 
catch [system.exception] 
{ 
    Write-host "Exception String:"+$($_.Exception.Message)" 
    #do some error handling 
} 

よろしく : だから純粋PowerShellの私は、次のように取り扱い、私の誤りを行います

関連する問題