Powershell 搜索其他服务器时Active Directory搜索筛选器失败

Powershell 搜索其他服务器时Active Directory搜索筛选器失败,powershell,active-directory,powershell-5.0,Powershell,Active Directory,Powershell 5.0,早上好 我正在开发一个图形应用程序,它在active directory中搜索有关用户的信息并返回某些字段。我的公司有两台active directory服务器,我们称之为服务器A和服务器B。当我尝试按姓氏搜索用户时,它对服务器A(默认服务器)非常有效,但我得到一个错误,即在尝试搜索服务器B时,筛选器无效。我尝试将搜索字符串存储在一个变量中并将其完全写出,但在google或stack exchange上没有发现任何其他与类似问题相关的问题。注意,我可以通过服务器B上的用户ID进行搜索;搜索功能仅

早上好

我正在开发一个图形应用程序,它在active directory中搜索有关用户的信息并返回某些字段。我的公司有两台active directory服务器,我们称之为服务器A和服务器B。当我尝试按姓氏搜索用户时,它对服务器A(默认服务器)非常有效,但我得到一个错误,即在尝试搜索服务器B时,筛选器无效。我尝试将搜索字符串存储在一个变量中并将其完全写出,但在google或stack exchange上没有发现任何其他与类似问题相关的问题。注意,我可以通过服务器B上的用户ID进行搜索;搜索功能仅在尝试按姓氏搜索时失败。我的搜索功能如下:

function Search{
    $uidr = [regex]'[a-zA-Z]{3}\d{3}'
    $script:op = ''
    $script:list = $null
    [array]$y = $null
    #$uidr is a regex string to match user ids
    if($script:x -match $uidr){
        try{
            $y = Get-ADUser $script:x -Properties cC
            #using select instead of $y.property for compatibility
            $ad = ($y | select -ExpandProperty samAccountName) + ' | ' + ($y | select -ExpandProperty Name) + ' | ' + ($y | select -ExpandProperty cC)
            $script:op = $ad
        }
        catch{
            try{
                $y = Get-ADUser $script:x -Server B.Server.com -Properties Title
                $ad = ($y | select -ExpandProperty samAccountName) + ' | ' + ($y | select -ExpandProperty Name) + ' | ' + ($y | select -ExpandProperty Title)
                $script:op = $ad
            }
            catch{
                $script:op = 'User ID Not Found'
            }
        }
    }
    else{
        $script:list = $null
        try{
            $y = Get-ADUser -Filter "surname -eq '$script:x'" -Properties cC
        }
        catch{
            try{
                $y = Get-ADUser -Server B.Server.com -Filter "surname -eq '$script:x'" -Properties Title
            }
            catch{
                $script:op = 'No user with that last name found. Check spelling and try again.'
            }
        }
        #This if/else is always defaulting to last when last name stored on server B but not server A is entered. $y.count must be 0
        if($y.Count -gt 1){
            foreach($u in $y){
                [array]$script:list += $u.name
            }
            foreach($l in ($script:list | select -Unique)){
                $listbox += $l
            }
        }
        elseif($y.Count -eq 1){
            $m = $y | select -ExpandProperty cC
            if($m -ne $null){
                $ad = ($y | select -ExpandProperty samAccountName) + ' | ' + ($y | select -ExpandProperty Name) + ' | ' + ($y | select -ExpandProperty cC)
                $script:op = $ad.tostring()
            }
            else{
                $ad = ($y | select -ExpandProperty samAccountName) + ' | ' + ($y | select -ExpandProperty Name) + ' | ' + ($y | select -ExpandProperty Title)
                $script:op = $ad.tostring()
            }
        }
        elseif($y.count -eq 0){
            $script:op = 'No user with that last name found. Check spelling and try again.'
        }
    }
    if($script:op -eq ''){
        continue
    }
    else{
        write-host $script:op
    }
}
目前,除服务器B的姓氏搜索外,该功能的各个方面都在工作。该功能本身只是返回“未找到具有该姓氏的用户”,但在尝试搜索姓氏时,我能够将其隔离到筛选器

请注意,我还尝试以独立脚本的形式运行搜索功能,我知道服务器B上的姓氏直接写入筛选器,但仍然返回“找不到具有此姓氏的用户”


不过,我看不出过滤器有什么问题。任何帮助都将不胜感激。

因此,问题在于我使用了Try/Catch。我通过用If语句替换Try/Catch解决了这个问题:

else{
    $script:list = $null
    $y = Get-ADUser -Filter "surname -eq '$script:x'" -Properties cC
    if($y -eq $null){
        $y = Get-ADUser -Server B.Server.com -Filter "surname -eq '$script:x'" -Properties Title
    }
    if($y -eq $null){
        $script:op = 'No user with that last name found. Check spelling and try again.'
    }
}

嗯,我觉得你的过滤语法有点不对劲,我会这样做:
-filter{lausname-eq$script:x}
看到你的错误让我觉得它在搜索文字“$script:x”或类似的东西。@JacobColvin,刚刚尝试过。那是行不通的。我以前在函数的其他部分搜索literal$脚本时确实出现了一个错误,当时我用大括号而不是引号对它进行了格式化,这通过切换到引号得到了修复。你是对的。我只是尝试了一下你的确切语法(我通过复制和粘贴你的代码并将$x设置为我的姓氏来查询我的姓氏),它对我起了作用。您的用户帐户属性可能有问题?我不确定。@JacobColvin,当我尝试一个独立的广告查询时,它非常适合我,唯一不起作用的时候是通过搜索函数传递的时候。我想也许$x变量在某个地方被清除了,但我看不出会在哪里happen@Cameron
Get ADUser
在没有
-ErrorAction Stop
的情况下不会给出终止错误,这就是它没有捕获的原因