搜索过滤器无法识别

时间:2019-04-18 11:58:45

标签: powershell

我正在尝试使用const form_data = new FormData(); form_data.append("file", fs.createReadStream(file.path)); const request_config = { headers: { "Authorization": "Bearer " + access_token, "Content-Type": "multipart/form-data" }, data: form_data }; return axios .post(url, form_data, request_config); 验证用户输入 我希望程序一直循环直到给出有效输入为止,这在用户给出任何输入而不管它是什么时都有效,但是当值为Do-until为空时,我陷入了一个无限循环,给我这个错误

""

这是我的代码:

The user does exist in AD:
samaccountname: 'the username'
Full Name: 'the fullname'  

get-aduser : The search filter cannot be recognized
At line:6 char:9
+ $user = get-aduser -Filter "samaccountname -eq '$SiteOwner' -OR name  -eq '$SiteO ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

我在做什么错了?

2 个答案:

答案 0 :(得分:1)

问题在于使用""作为输入时,它将引发终止错误。您可以使用try-catch来解决。在其中,您可以添加break语句以脱离第二个do-until循环。但这可能会设计为不同的。

Do{

  $SiteOwner = Read-Host 'OwnerAlias'

  Do {
    try {
      $user = get-aduser -Filter "samaccountname -eq '$SiteOwner' -OR name -eq '$SiteOwner'"
    }
    catch {
      "The user does not exist in AD"
      break
    }
    If (!$user) {
      "The user does not exist in AD"
    } 
    Else {
      "The user does exist in AD:`nsamaccountname: $($user.samaccountname)`nFull Name: $($user.name)"
    }

  }While (($null -eq $SiteOwner) -or ( "" -eq $SiteOwner))
}Until ($user)

答案 1 :(得分:1)

这是您的代码的有效版本:

# Ensure variables are null - may not be needed, depending on the rest of your code (if any)
$user = $null
$SiteOwner = $null

# Assuming $user is null we should do a lookup
while(-not $user) {
    $SiteOwner = Read-Host 'OwnerAlias'

    # As long as the user entered some text, query AD - otherwise, skip this and ask again
    if (-not [string]::IsNullOrWhiteSpace($SiteOwner)) {
        $user = Get-AdUser -Filter "samaccountname -eq '$SiteOwner' -OR name -eq '$SiteOwner'"

        if (-not $user) {
          # Found the user - we should exit the while
          "The user does not exist in AD"
        } 
        else {
          # Didn't find the user - ask for the name again
          "The user does exist in AD:`nsamaccountname: $($user.samaccountname)`nFull Name: $($user.name)"
        }
    }
}