![](/img/trans.png)
[英]List all Sub-Groups (members) of Groups in Active Directory - Powershell
[英]powershell find all users for all groups flag as disabled
我正在嘗試使用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"'
那樣的想法一定是錯誤的,但是不確定如何解決。
流水線流水線,不需要循環和數組構建以及哈希表和存儲所有輸出,然后將其輸出。
將組通過管道傳遞到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
$Temprecord = Get-ADUser -Filter "samaccountname -like ""$($Member.samaccountname)"""
請參見例如Weekend Scripter:了解PowerShell中的引號和轉義字符,定界符和引號以獲取解釋。
范例 :
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.