Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops powershell查找所有组的所有用户标记为已禁用_Loops_Powershell_Foreach_Export To Csv - Fatal编程技术网

Loops powershell查找所有组的所有用户标记为已禁用

Loops powershell查找所有组的所有用户标记为已禁用,loops,powershell,foreach,export-to-csv,Loops,Powershell,Foreach,Export To Csv,我正在尝试使用PowerShell生成包含以下列的csv文件: 名称、用户名、组名、已启用 我就快到了,我缺少的一步是如何将用户标记为已启用或未启用。 例如,这很好: Import-Module ActiveDirectory $Groups = (Get-AdGroup -filter * | Where {$_.name -like "**"} | select name -ExpandProperty name) $Table = @() $Record = @{ "Group

我正在尝试使用PowerShell生成包含以下列的csv文件: 名称、用户名、组名、已启用

我就快到了,我缺少的一步是如何将用户标记为已启用或未启用。 例如,这很好:

Import-Module ActiveDirectory

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

$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 | export-csv "C:\temp\SecurityGroups.csv" -NoTypeInformation
如果我尝试在每个循环中添加一个额外的搜索(承认这是非常不切实际的),以在用户“启用”状态中添加,则该列将保持空白:

Import-Module ActiveDirectory

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

$Table = @()

$Record = @{
  "Group Name" = ""
  "Name" = ""
  "Username" = ""
  "Enabled" = ""
}


Foreach ($Group in $Groups) {

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

  foreach ($Member in $Arrayofmembers) {
    $Temprecord = Get-ADUser -Filter 'samaccountname -like "$Member.samaccountname"'
    $Record."Enabled" = $Temprecord.Enabled
    $Record."Group Name" = $Group
    $Record."Name" = $Member.name
    $Record."UserName" = $Member.samaccountname
    $objRecord = New-Object PSObject -property $Record
    $Table += $objrecord

  }
}

$Table | export-csv "C:\temp\SecurityGroups.csv" -NoTypeInformation
如果让我猜的话,这是一个线路问题:

$Temprecord = Get-ADUser -Filter 'samaccountname -like "$Member.samaccountname"'

like位肯定是错误的,但我不确定如何解决。

管道,不需要循环和数组构建和哈希表,存储所有输出,然后输出

将组导入ADGroupMember并将组对象也导入,将组和成员导入Select,然后选择所需的四列,并将它们导出为CSV

Import-Module ActiveDirectory

Get-AdGroup -Filter "Name -like '*e*'" -PipelineVariable Group |
    Get-ADGroupMember -Recursive | 
        Select-Object @{Label='Group Name'; Expression={$Group.Name}}, 
                      Name, 
                      SamAccountName, 
                      @{Label='Enabled'; Expression={(Get-AdUser -Identity $_.SamAccountName).Enabled}} |
            Export-Csv -Path 'C:\temp\SecurityGroups.csv' -NoTypeInformation
参见例如和 请解释

示例

PS D:\PShell> $x = 1,2,3
PS D:\PShell> $x.Count
3
PS D:\PShell> ### Single quotation marks define a literal string
PS D:\PShell> '$x.Count'
$x.Count
PS D:\PShell> ### Double quotation marks define more dynamic parsing string
PS D:\PShell> "$x.Count"
1 2 3.Count
PS D:\PShell> ### OOPS!
PS D:\PShell> "$($x.Count)"
3
PS D:\PShell> "array count $($x.Count)"
array count 3
PS D:\PShell> ### Finally, escape double quotes within double quoted string
PS D:\PShell> "array count as string ""$($x.Count)"""
array count as string "3"
PS D:\PShell>

-过滤“samaccountname-类似“$($Member.samaccountname)””
。见,谢谢!这太完美了,我现在完全明白我做错了什么。如果你把它作为答案提交,我会把它标记为正确。
PS D:\PShell> $x = 1,2,3
PS D:\PShell> $x.Count
3
PS D:\PShell> ### Single quotation marks define a literal string
PS D:\PShell> '$x.Count'
$x.Count
PS D:\PShell> ### Double quotation marks define more dynamic parsing string
PS D:\PShell> "$x.Count"
1 2 3.Count
PS D:\PShell> ### OOPS!
PS D:\PShell> "$($x.Count)"
3
PS D:\PShell> "array count $($x.Count)"
array count 3
PS D:\PShell> ### Finally, escape double quotes within double quoted string
PS D:\PShell> "array count as string ""$($x.Count)"""
array count as string "3"
PS D:\PShell>