2

我在 Powershell 中有这样的功能。当用户为任何参数传递空字符串时,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 回答