前回「エラーをファイルにリダイレクトする(PowerShell 入門) 」で2>&1演算子を使用してエラーの内容を変数に保存しました。
このようにしてエラーを格納すると、その変数はErrorRecordオブジェクトとなります。
ではErrorRecordオブジェクトにはどのようなメンバーが備わっているかを確認したいと思います。
まずは、前回同様下記のようにしてErrorRecord型の変数を作成しましょう。
$err = mkdir HIRO*2.txt 2>&1
次に、Get-Memberコマンドレットでどのようなメンバーがあるかを確認します。
PS C:\Work> $err | Get-Member TypeName: System.Management.Automation.ErrorRecord Name MemberType Definition ---- ---------- ---------- Equals Method System.Boolean Equals(Object obj) GetHashCode Method System.Int32 GetHashCode() GetObjectData Method System.Void GetObjectData(SerializationInfo info, StreamingContext context) GetType Method System.Type GetType() get_CategoryInfo Method System.Management.Automation.ErrorCategoryInfo get_CategoryInfo() get_ErrorDetails Method System.Management.Automation.ErrorDetails get_ErrorDetails() get_Exception Method System.Exception get_Exception() get_FullyQualifiedErrorId Method System.String get_FullyQualifiedErrorId() get_InvocationInfo Method System.Management.Automation.InvocationInfo get_InvocationInfo() get_TargetObject Method System.Object get_TargetObject() set_ErrorDetails Method System.Void set_ErrorDetails(ErrorDetails value) ToString Method System.String ToString() writeErrorStream NoteProperty System.Boolean writeErrorStream=True CategoryInfo Property System.Management.Automation.ErrorCategoryInfo CategoryInfo {get;} ErrorDetails Property System.Management.Automation.ErrorDetails ErrorDetails {get;set;} Exception Property System.Exception Exception {get;} FullyQualifiedErrorId Property System.String FullyQualifiedErrorId {get;} InvocationInfo Property System.Management.Automation.InvocationInfo InvocationInfo {get;} TargetObject Property System.Object TargetObject {get;}
これらの中から、主要なメンバーについて説明したいと思います。
CategoryInfoプロパティ
このプロパティは文字通りエラーの種類についての情報が格納されています。
PS C:\Work> $err.CategoryInfo Category : InvalidArgument Activity : New-Item Reason : ArgumentException TargetName : C:\Work\HIRO*2.txt TargetType : String
CategoryでInvalidArgumentとなっていることがわかります。これはdirコマンドに対して、無効な文字が含まれたパスを指定しているためです。
さらにReasonをみるとこのArgumentExceptionという例外が発生したことがわかります。
Exceptionプロパティ
Exceptionプロパティ自体も様々なメンバーを持っていますが、規定のプロパティはMessageとなっており、エラーの内容を確認することができます。
PS C:\Work> $err.Exception パスに無効な文字が含まれています。
InvocationInfoプロパティ
このプロパティはエラーの発生場所に関する情報が格納されます。
PS C:\Work> $err.InvocationInfo MyCommand : New-Item ScriptLineNumber : 1 OffsetInLine : -2147483648 ScriptName : Line : param([string[]]$paths); New-Item -type directory -path $paths PositionMessage : 発生場所 行:1 文字:34 + param([string[]]$paths); New-Item <<<< -type directory -path $paths InvocationName : New-Item PipelineLength : 1 PipelinePosition : 1
TargetObjectプロパティ
このプロパティにはエラー発生時の操作対象オブジェクトが格納されます。ただし、すべてのエラーがこのフィールドを設定する訳ではないため、Nullの場合もあります。
PS C:\Work> $err.TargetObject C:\Work\HIRO*2.txt
このようにErrorRecordオブジェクトにはエラーの原因を解明する上で役立つ情報が格納されています。
ぜひ、ErrorRecordオブジェクトの利用方法を理解し、スクリプトのデバッグに活用してください。
Theme design by Jelle Druyts
Pick a theme: BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2 BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2009, HIRO
E-mail