gpt4 book ai didi

powershell - 需要逗号将 SamAccountName 与组分开

转载 作者:行者123 更新时间:2023-12-04 02:22:41 26 4
gpt4 key购买 nike

我有这个脚本来提取以字母“m”开头的用户以及他们所属的组。脚本的结果没问题,但我需要 SamAccountName并且组在同一行上并用逗号分隔,以便我可以在 Excel 文件中更好地安排它。有什么建议?

Import-Module activedirectory

$file=".\atos_user_m.csv"

foreach ($suser in (Get-ADUser -Filter {samaccountname -like "m*"})) {
$user = $suser.SamAccountName
echo "`r`n"$user"`r`n" | Out-File $file -Append
foreach ($group in (Get-ADPrincipalGroupMembership -Identity $user)) {
$group.SamAccountName | Out-File $file -Append
}
}

最佳答案

如果您希望给定用户的所有组与用户名在同一行,您可以执行以下操作:

foreach ($suser in (Get-ADUser -Filter {samaccountname -like "m*"})) {
$user = $suser.SamAccountName
$groups = Get-ADPrincipalGroupMembership -Identity $user |
Select-Object -Expand SamAccountName
'{0},{1}' -f $user, ($groups -join ',') | Out-File $file -Append
}

或像这样(使用管道):
Get-ADUser -Filter {samaccountname -like "m*"} | ForEach-Object {
$groups = Get-ADPrincipalGroupMembership -Identity $_.SamAccountName |
Select-Object -Expand SamAccountName
'{0},{1}' -f $_.SamAccountName, ($groups -join ',')
} | Out-File $file

关于powershell - 需要逗号将 SamAccountName 与组分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32370788/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com