列表是否在CSV文件中启用或禁用AD组成员

发布于 2025-01-19 09:07:56 字数 887 浏览 0 评论 0原文

我有以下脚本并且运行良好。我试图在最后添加“启用”列,但不确定如何将其添加为扩展属性。

Import-Module ActiveDirectory
#create an empty array
$temp = @()

#make it multi-dimensional array
$Record = @{
  "Group Name" = ""
  "Name" = ""
  "Username" = ""
  "Enabled" = ""
}

$Groups = (Get-AdGroup -filter * -SearchBase "CN=Domain Admins,CN..." | Where {$_.name -like "**"} | select name -ExpandProperty name)

Foreach ($Group in $Groups) {

  $Arrayofmembers = Get-ADGroupMember -identity $Group -recursive | select name,samaccountname, enabled

  foreach ($Member in $Arrayofmembers) {
    
    $Record."Group Name" = $Group
    $Record."UserName" = $Member.samaccountname
    $Record."Name" = $Member.name
    $Record."Enabled" = $Member.enabled
    $objRecord = New-Object PSObject -property $Record
    $temp += $objrecord

  }
}

$temp | export-csv "C:\temp\SecurityGroups.csv" -NoTypeInformation

I have following script and it works fine. I am trying to add 'Enabled' column in the end but not sure how to add it as an expanded property.

Import-Module ActiveDirectory
#create an empty array
$temp = @()

#make it multi-dimensional array
$Record = @{
  "Group Name" = ""
  "Name" = ""
  "Username" = ""
  "Enabled" = ""
}

$Groups = (Get-AdGroup -filter * -SearchBase "CN=Domain Admins,CN..." | Where {$_.name -like "**"} | select name -ExpandProperty name)

Foreach ($Group in $Groups) {

  $Arrayofmembers = Get-ADGroupMember -identity $Group -recursive | select name,samaccountname, enabled

  foreach ($Member in $Arrayofmembers) {
    
    $Record."Group Name" = $Group
    $Record."UserName" = $Member.samaccountname
    $Record."Name" = $Member.name
    $Record."Enabled" = $Member.enabled
    $objRecord = New-Object PSObject -property $Record
    $temp += $objrecord

  }
}

$temp | export-csv "C:\temp\SecurityGroups.csv" -NoTypeInformation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱着落日 2025-01-26 09:07:56

不幸的是,get-adgroupmember不用启用属性返回对象,因此我们需要为此使用Get-Aduser

顺便说一句,将项目添加到具有+=的数组中是非常时间和内存,因为每次全部数组都需要在内存中重建。
让PowerShell从阵列中收集循环的输出要好得多。

Import-Module ActiveDirectory

# get an array of Group objects
$Groups = Get-AdGroup -Filter * -SearchBase "CN=Domain Admins,CN..." 

# loop through this array and capture the output in variable $result
$result = foreach ($Group in $Groups) {
    # get the members of each group.
    # members can be users, groups, and computers, so filter on users only
    $Group | Get-ADGroupMember -Recursive | Where-Object { $_.objectClass -eq 'user' } |
    ForEach-Object {
        # get the user in order to find its Enabled property
        $user = Get-ADUser -Identity $_.DistinguishedName
        [PsCustomObject]@{
            'Group Name' = $Group.Name
            'UserName'   = $user.SamAccountName
            'Name'       = $user.Name
            'Enabled'    = $user.Enabled
        }
    }
}

# now export the results to a CSV file
$result | Export-Csv "C:\temp\SecurityGroups.csv" -NoTypeInformation

Unfortunately, the Get-ADGroupMember does not return objects with the Enabled property, so we need to use Get-ADUser for that.

BTW, it is very time and memory consuming to add items to an array with +=, because every time the entire array needs to be rebuilt in memory.
It is far better to let PowerShell collect the output from the loop in an array.

Import-Module ActiveDirectory

# get an array of Group objects
$Groups = Get-AdGroup -Filter * -SearchBase "CN=Domain Admins,CN..." 

# loop through this array and capture the output in variable $result
$result = foreach ($Group in $Groups) {
    # get the members of each group.
    # members can be users, groups, and computers, so filter on users only
    $Group | Get-ADGroupMember -Recursive | Where-Object { $_.objectClass -eq 'user' } |
    ForEach-Object {
        # get the user in order to find its Enabled property
        $user = Get-ADUser -Identity $_.DistinguishedName
        [PsCustomObject]@{
            'Group Name' = $Group.Name
            'UserName'   = $user.SamAccountName
            'Name'       = $user.Name
            'Enabled'    = $user.Enabled
        }
    }
}

# now export the results to a CSV file
$result | Export-Csv "C:\temp\SecurityGroups.csv" -NoTypeInformation
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文