获取广告组中的用户(sammacountname),将其保存在csv中,显示这些用户的所有组

时间:2016-10-25 11:14:57

标签: loops powershell csv

我正在尝试写一个脚本wchich将:  采取特定AD组的成员 将其保存到csv文件 打印组的用户和用户所属的所有组的成员 我希望打印结果在一行

我的代码看起来像这样

 $group = Read-Host -Prompt 'Enter the group name'
 Get-ADGroupMember -identity $group -Recursive | % { get-aduser $_} | select SamAccountName | export-csv userzy.csv
$file = import-csv  userzy.csv
ForEach ($user In $file) 
{
 $user
(Get-ADUser $user –Properties MemberOf | Select-Object MemberOf).MemberOf

 }

当涉及到循环csv以显示用户及其组时,我陷入困境 我不知道怎么做一行 2.当我在我的循环中运行Get-ADuser时出现错误:

Get-ADUser : Cannot bind parameter 'Identity'. Cannot convert value "@{SamAccountName=mczechow}" to type "Microsoft.ActiveDi
rectory.Management.ADUser". Error: "Cannot convert the "@{SamAccountName=mczechow}" value of type "CSV:Selected.Microsoft.Ac
tiveDirectory.Management.ADUser" to type "Microsoft.ActiveDirectory.Management.ADUser"."
At C:\Users\mgiedyk\Desktop\skrypty\groupmembers.ps1:16 char:13
+  Get-ADUser $user -Properties MemberOf
+             ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.GetADUser

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:-1)

只需替换

  

Get-ADUser $user –Properties MemberOf ...

  

Get-ADUser -Filter{SamAccountName -like $user} –Properties MemberOf ...