使用DirectorySearcher的Powershell LDAP过滤器

时间:2018-08-31 07:03:25

标签: powershell active-directory ldap

我正在使用DirectorySearcher类来查找单个用户。条件应为objectCategory是用户,并且其密码未设置为永不过期

经过一番搜索,我想到了:

$searcher = New-Object System.DirectoryServices.DirectorySearcher

$searcher.Filter = "(&(objectCategory=User)(samAccountName=$env:username)(!(userAccountControl:1.2.840.113556.1.4.803:=65536)))"

其中userAccountControl:1.2.840.113556.1.4.803:=65536应该用于密码永不过期的用户。

最后我做到了:

$user = $searcher.FindOne().GetDirectoryEntry()

但是它说我不能在一个空值表达式上调用方法。我想我正确地使用了括号。那么可能是我不能使用!运算符吗?

还要注意,我可以使用get-aduser命令,如下所示:

get-aduser -filter * -properties samAccountName, PasswordNeverExpires | where { $_.passwordNeverExpires -eq "true" } | where {$_.enabled -eq "true"} | where {$_.samAccountName -eq $env:username}

,但是在这种情况下,最好像上面显示的那样使用DirectorySearcher

1 个答案:

答案 0 :(得分:1)

实际上您的代码正在运行,但是当{ Error: render function or template not defined in component: anonymous at normalizeRender (/var/www/site/front/node_modules/vue-server-renderer/build.js:7396:13) at renderComponentInner (/var/www/site/front/node_modules/vue-server-renderer/build.js:7520:3) at renderComponent (/var/www/site/front/node_modules/vue-server-renderer/build.js:7491:5) at renderNode (/var/www/site/front/node_modules/vue-server-renderer/build.js:7407:5) at renderComponentInner (/var/www/site/front/node_modules/vue-server-renderer/build.js:7527:3) at renderComponent (/var/www/site/front/node_modules/vue-server-renderer/build.js:7491:5) at renderNode (/var/www/site/front/node_modules/vue-server-renderer/build.js:7407:5) at renderComponentInner (/var/www/site/front/node_modules/vue-server-renderer/build.js:7527:3) at renderComponent (/var/www/site/front/node_modules/vue-server-renderer/build.js:7491:5) at RenderContext.renderNode (/var/www/site/front/node_modules/vue-server-renderer/build.js:7407:5) at RenderContext.next (/var/www/site/front/node_modules/vue-server-renderer/build.js:2428:14) at RenderContext.next (/var/www/site/front/node_modules/vue-server-renderer/build.js:2441:12) at RenderContext.cachedWrite [as write] (/var/www/site/front/node_modules/vue-server-renderer/build.js:2287:9) at RenderContext.next (/var/www/site/front/node_modules/vue-server-renderer/build.js:2432:16) at RenderContext.cachedWrite [as write] (/var/www/site/front/node_modules/vue-server-renderer/build.js:2287:9) at RenderContext.renderNode (/var/www/site/front/node_modules/vue-server-renderer/build.js:7415:15) statusCode: 500, name: 'NuxtServerError' } )什么都不返回时,也就是说,当过滤器什么也不返回时,$searcher.FindOne(方法给出了:

GetDirectoryEntry()

尝试:

> You cannot call a method on a null-valued expression. At line:1 char:1
> + $searcher.FindOne().GetDirectoryEntry()
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
>     + FullyQualifiedErrorId : InvokeMethodOnNull