【问题标题】:Get users (sammacountname) in ad group, save them in csv, display all of the groups of these users获取广告组中的用户(sammacountname),将他们保存在 csv 中,显示这些用户的所有组
【发布时间】:2016-10-25 11:14:56
【问题描述】:

我正在尝试编写一个脚本,它将: 获取特定 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 以显示用户及其组时,我被卡住了 1.我不知道如何在一行中制作 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

提前感谢您的帮助。

【问题讨论】:

  • 您好,请将 $user 替换为 $user.SamAccountName$user 是这里的一个对象。
  • 谢谢。看来我必须向对象学习更多对象并参考它们的属性。会继续努力的。

标签: loops powershell csv


【解决方案1】:

只需替换

Get-ADUser $user –Properties MemberOf ...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多