2

私はPowershellでこのような機能を持っています。ユーザーがいずれかのパラメーターにnull文字列を渡すと、PowerShellは検証して例外を発生させます。このような例外をどのように処理しますか?

function CheckADUser()
{
    param(
            [ValidateLength(1,256)]
            [string]$domainName, 
            [ValidateLength(1,256)]
            [string]$username, 
            [ValidateLength(1,256)]
            [string]$password)
    Process{
    $fullyQualifiedUser = $domainName+"\"+$username
    $domain = New-Object DirectoryServices.DirectoryEntry("", $fullyQualifiedUser, $password)
    return $domain.name
    }
}
4

1 に答える 1

2

try/catch ブロックを使用して例外を処理します。

try {
  checkaduser $null $null $null
}
catch [System.Management.Automation.ValidationMetadataException] {
  # exception handling code
}
于 2012-04-19T17:05:21.123 に答える