MSHのデバッグ手法(2)

エラー内容を分析する

エラー発生時のオブジェクトを格納する「$error」。
「$error」を調べることでエラー内容を分析することができます。


試しに、意図的に「start-service」でエラーを発生させてみます。

MSH C:\> start-service $servicename
start-service : Cannot bind argument to parameter 'ServiceName' because it is null.
At line:1 char:14
+ start-service  <<<< $servicename

$errorにはエラー内容が配列として格納されています。

MSH C:\> $error.gettype().fullname
System.Collections.ArrayList
MSH C:\> $error.count
1

今回発生させたエラーについて調べてみましょう。

MSH C:\> $thisError = $error[0]
MSH C:\> $thisError
start-service : Cannot bind argument to parameter 'ServiceName' because it is null.
At line:1 char:14
+ start-service  <<<< $servicename

$errorの要素は「ErrorRecord」オブジェクトです。

MSH C:\> $thisError.gettype().fullname
System.Management.Automation.ErrorRecord

エラーの詳細は「CategoryInfo」を見ると分かります。

MSH C:\> $thisError.CategoryInfo


Category   : InvalidData
Activity   : start-service
Reason     : ParameterBindingValidationException
TargetName :
TargetType :

Categoryは以下の種類があります。

Category Description
CloseError For errors that result when closing.
DeadlockDetected For errors that result when a deadlock is detected.
DeviceError For errors that result when a device reported an error.
FromStdErr For errors that result when a non-MSH command reported an error to its STDERR pipe. The engine uses this category when it executes a native console application and captures the errors reported by the native application. Avoid using FromStdErr in other circumstances.
InvalidArgument For errors that result when an invalid argument is specified.
InvalidData For errors that result when invalid data is specified.
InvalidOperation For errors that result when an invalid operation is requested.
InvalidResult For errors that result when an invalid result is returned.
InvalidType For errors that result when an invalid Type is specified.
MetadataError For errors that result when the metadata contains an error.
NotImplemented For errors that result when a referenced API is not implemented.
NotInstalled For errors that result when an item is not installed.
NotSpecified For errors detected by MSH that result because an exception did not specify an error category or the specified error category is invalid.
ObjectNotFound For errors that result when an object cannot be found (such as file, directory, computer, and system resource objects).
OpenError For errors that result when opening.
OperationStopped For errors that result when an operation has stopped.
OperationTimeout For errors that result when an operation has exceeded its timeout limit.
ParserError For errors that result when a parser encounters an error.
PermissionDenied For errors that result when an operation is not permitted.
ReadError For errors that result when reading.
ResourceBusy For errors that result when a resource is busy.
ResourceExists For errors that result when a resource already exists.
ResourceUnavailable For errors that result when a resource is unavailable.
SecurityError
SyntaxError For errors that result when MSH detects an error in the MSH language syntax.
WriteError For errors that result when attempting to write to the pipeline.

参照:ErrorCategory Enumeration