【问题标题】:Get all users from active directory using filter使用过滤器从活动目录中获取所有用户
【发布时间】:2018-11-06 17:54:04
【问题描述】:

我正在尝试使用以下脚本从 AD 获取所有活跃用户,但是,尽管我知道有值为 512 的数据,但我没有得到任何结果。

你知道我有什么问题吗?

  Get-ADUser -filter {$userAccountControl -eq "512"} -properties Name,userAccountControl -Server myserver.local | Export-CSV "E:\Folder\ADusers.csv" -NoTypeInformation -Encoding UTF8

【问题讨论】:

  • $userAccountControl 中有什么?
  • Get-ADUser -filter {userAccountControl -band [int]512}
  • 警告:userAccountControl 是位图(即,被视为一系列位的整数,其中每个位置代表开/关状态) - 请参阅@dev.greg 的答案。
  • 好吧,我已经尝试使用 -band ...但仍然出现错误:Get-ADUser:术语“Get-ADUser”未被识别为 cmdlet、函数、脚本文件的名称,或可运行的程序。检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。在 E:\AD\ad_script_test_2.ps1:1 char:1 + Get-ADUser -filter {userAccountControl -band [int]512} -properties Na ... + ~~~~~~~~~~ + CategoryInfo : ObjectNotFound : (Get-ADUser:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

标签: powershell syntax active-directory user-management user-account-control


【解决方案1】:

正如 cmets 中所指出的,$ 不属于。这告诉 PowerShell 您要使用名为$userAccountControl 的变量中的值并将其与512 进行比较。考虑到您可能从未设置过名为 $userAccountControl 的变量,这意味着它与 512 没有进行任何比较,并且发现并非对每个帐户都适用。

删除$,它会将名为userAccountControl 的属性与512 进行比较。

Get-ADUser -filter {userAccountControl -eq "512"} -properties Name,userAccountControl -Server myserver.local | Export-CSV "E:\Folder\ADusers.csv" -NoTypeInformation -Encoding UTF8

【讨论】:

  • 不起作用,似乎语法不正确:“Get-ADUser : 术语 'Get-ADUser' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称. 检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。在 E:\AD\ad_script_test_2.ps1:1 char:1 + Get-ADUser -filter {userAccountControl -eq " 512"} -properties Name,us ... + ~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-ADUser:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException"
  • 听起来您还没有加载 ActiveDirectory 模块。先这样做:Import-Module ActiveDirectory
  • 如果您仍然收到 The term 'Get-ADUser' is not recognized 错误,这意味着 PowerShell 不知道 Get-ADUser 的含义。 ActiveDirectory 模块未安装或未加载。你需要先解决这个问题。
  • 您可以在此处找到安装说明(按照您的 Windows 版本的步骤操作):4sysops.com/wiki/…
  • 正确,该模块不在测试服务器上,当我在产品服务器上运行它时,它工作正常。谢谢;)
【解决方案2】:

你可以试试 LDAPfilter 语法

Get-ADUser -property userAccountControl -LDAPfilter "(userAccountControl=512)"

但是,这可能不是一个好方法:userAccountControl 是一个二进制字段,每个位代表一个二进制值(请参阅https://support.microsoft.com/en-us/help/305144/how-to-use-the-useraccountcontrol-flags-to-manipulate-user-account-pro) 例如:

512 是你想要的“普通帐户”

66048 是“普通账户”+“密码不会过期”,你可能也想要

514 是 "普通帐户" + "帐户已禁用" ,您可能不想要

所有值都会匹配

-band 512

您需要明确什么是活跃用户。

【讨论】:

  • 是的,我的意思是 512 状态。但是,它不起作用。看起来语法不正确。
猜你喜欢
  • 2019-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多