我是相当新的PS用户...寻找PowerShell脚本的一些帮助,以获取用户所属的安全组列表 .
描述我需要的东西:
-
我有许多用户的输入列表(txt文件)(samaccountnames) . 每个名字都在新的一行上 .
-
我需要脚本在AD中搜索这些名称 - 整个林,而不仅仅是一个域
-
输出应该看起来像"samaccountname"这个帐户所属的组列表在一行中,所以我可以在excel中对它进行排序
这是我的脚本:
$users = Get-Content C:\users.txt
ForEach ($User in $users) {
$getmembership = Get-ADUser $User.Users -Properties MemberOf | Select -ExpandProperty memberof
$getmembership | Out-File -Append c:\membership.txt
}
但它给我一个错误:
Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
At line:4 char:28
+ $getmembership = Get-ADUser <<<< $User.Users -Properties MemberOf | Select -ExpandProperty memberof
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
无论如何,这个脚本不会搜索整个森林 .
样本输入列表:
username1
username2
username3
username4... etc
示例输出列表
username1;group1;group2;group3
username2;group1;group2;group3;group4... etc or something similar
任何帮助将不胜感激 .
5 回答
First :目前看来,
$User
变量没有.Users
属性 . 在您的代码中,$User
只是表示文本文件中的一行(foreach循环中的"current"行) .Secondly ,我不相信你可以用一个命令查询整个森林 . 您将不得不将其分解为更小的块:
查询林列表中的域名
为每个域调用
Get-ADUser
(您可能必须通过-Credential
参数指定备用凭据Thirdly ,获取用户所属的组列表:
Fourthly :要获得最终的所需字符串格式,只需将
$User.Name
,分号和$GroupMembership
字符串一起添加:从另一个答案得到它,但脚本工作魔术 . :)
或者按字母顺序添加“排序名称”
以下代码将使用samaccountname返回用户名组成员身份 . 您可以修改它以从文件获取输入或更改查询以获取具有非过期密码的帐户等
样本输出:
一行中的一切: