powershell 将从控制台导出的数据添加到CSV文件

7xzttuei  于 2022-11-10  发布在  Shell
关注(0)|答案(1)|浏览(107)

我有一个脚本,可以将用户和组名从Active Directory导出到PowerShell控制台。数据已导出,这是正常的,但现在,我需要将它们从控制台添加到CSV文件中,列有:组名和用户。
请检查以下代码:

$groups = "first_group","second_group"
 ForEach ($Group in $Groups) {
     Get-ADGroupMember -identity $group | 
         Get-ADUser -properties displayName | 
             Select-Object SamAccountName, displayName, @{name="group";expression={$group}}
 }

请协助解决。

bybem2ql

bybem2ql1#

试试这个

$groups = "first_group","second_group"
$Table = @() 
$Record = @{
  "Group Name" = ""
  "Name"       = ""
  "Username"   = ""
}
Foreach ($Group in $Groups) {
  $Arrayofmembers = Get-ADGroupMember -identity $Group -recursive | select name, samaccountname
  foreach ($Member in $Arrayofmembers) {
    $Record."Group Name" = $Group
    $Record."Name" = $Member.name
    $Record."UserName" = $Member.samaccountname
    $objRecord = New-Object PSObject -property $Record
    $Table += $objrecord
  }
}
$Table
$Table | export-csv "C:\Temp\Group.csv" -NoTypeInformation

`这是区分名称的更新

$Record = @{
  "Group Name" = ""
  "Name"       = ""
  "Username"   = ""
  "Group distinguishedName"=""
}
Foreach ($Group in $Groups) {
  $Arrayofmembers = Get-ADGroupMember -identity $Group -recursive | select name, samaccountname

  foreach ($Member in $Arrayofmembers) {
    $Record."Group Name" = $Group
    $Record."Group distinguishedName" = (Get-ADGroup -Identity $group).DistinguishedName 
    $Record."Name" = $Member.name
    $Record."UserName" = $Member.samaccountname
    $objRecord = New-Object PSObject -property $Record
    $Table += $objrecord
  }
}

相关问题