我试图让脚本根据输入文件返回AD用户ID,显示名称和组成员身份,并将结果导出到另一个文件。
但是,群组成员资格信息似乎在此过程中丢失了。
有什么想法吗?
我目前的剧本:
$Result = @()
ForEach ($_ in gc userlist.csv)
{
$User = Get-ADUser $_ -Properties DisplayName, SamAccountName, LastLogonDate | Select DisplayName, SamAccountName, LastLogonDate
$Groups = Get-ADPrincipalGroupMembership $_ | Select Name
# So far it seems to work
$Properties = @{
UserID = (@($User.SamAccountName) -join ',')
Name = (@($User.DisplayName) -join ',')
LastLogonDate = (@($User.LastLogonDate) -join ',')
Groups = (@($Groups.Name) -join ',')
}
# By this time, Groups doesn't return any information
$Result += New-Object psobject -Property $Properties
}
$Result | Select-Object Name, UserID, Groups, LastLogonDate | Export-Csv -NoTypeInformation -Path output.csv
答案 0 :(得分:1)
以下是我认为可行的方法:
$Result = @()
ForEach ($User in gc userlist.csv) {
$UserDetails = Get-ADUser $User -Properties DisplayName, SamAccountName, LastLogonDate | Select DisplayName, SamAccountName, LastLogonDate
$Groups = Get-ADPrincipalGroupMembership $User | Select Name
$Properties = @{
UserID = $UserDetails.SamAccountName
Name = $UserDetails.DisplayName
LastLogonDate = $UserDetails.LastLogonDate
Groups = ($Groups.Name -join '; ')
}
$Result += New-Object psobject -Property $Properties
}
$Result | Select-Object Name, UserID, Groups, LastLogonDate | Export-Csv -NoTypeInformation -Path output.csv
$_
可能会起到手动设置该变量并不是特别好的做法,因此我已将其更改为$ user。@()
,并将其与,
一起加入。我不确定为什么这是必要的,除了Groups
属性,但为了不混淆CSV结果,我已经用;
加入了这些。