我尝试通过Firstname,Lastname从OU导出AD组和用户,但我只能使用Lastname,Firstname。
我尝试的其他所有内容都为我提供了一个空字符串。我尝试将Select-Object下的行更改为:
@{Name='Member';Expression={$_.FirstName = GetFirstName $_.Name $_.LastName = GetLastName $_.Name}},
$firt = $_.firstname
$last = $_.lastname
@{Name='Member';Expression={$_.name = "$first,$last"}},
这是工作代码,但应切换名称。
$OU = 'OU=Groups,OU=City,OU=Continent,DC=DomainControler, DC=Domain, DC=net' #Change this to get different groups
$DateTime = Get-Date -f "dd-MM-yyyy"
$MyFileName = "CompanyName-Groups_"+$DateTime+".csv"
$Path = Join-Path $PSScriptRoot $MyFileName
$Groups = get-adobject -Filter 'ObjectClass -eq "group"' -SearchBase $OU
$i=0
$tot = $Groups.count
$Data = foreach ($Group in $Groups) {
$i++
$status = "{0:N0}" -f ($i / $tot * 100)
Write-Progress -Activity "Exporting AD Groups" -status "Processing Group $i of $tot : $status% Completed" -PercentComplete ($i / $tot * 100)
Get-ADGroupMember -Identity $Group |
Select-Object @{Name='Group';Expression={$Group.Name}},
@{Name='Member';Expression={$_.Name}},
@{Name='Enabled';Expression={if ($_.ObjectClass -eq 'user') {Get-ADUser $_ | Select-Object -Expand Enabled} else {'NA/Group'}}}
}
$Data | Export-Csv -Path $Path -NoTypeInformation
这是一个示例输出:
Group, "member", enabled
Admin, "Mario, Speedwagon", True
Admin, "Petey, Cruiser", True
Admin, "Anna, Sthesia", False
HR, "Paul, Molive", True
HR, "Phaedra, Lugt", True
IT, "Paul, Molive", False
IT, "Cliff, Hanger", True
它应该成为:
Group, "member", enabled
Admin, "Speedwagon, Mario", True
Admin, "Cruiser, Petey", True
Admin, "Sthesia, Anna", False
HR, "Molive, Paul", True
HR, "Lugt, Phaedra", True
IT, "Molive, Paul", False
IT, "Hanger, Cliff", True
我想这可能会让你感到清楚:
$OU = 'OU=Groups,OU=City,OU=Continent,DC=DomainControler, DC=Domain, DC=net'
$PathParams = @{
Path = $PSScriptRoot
ChildPath = "PA-AD-Groups_{0}.csv" -f (Get-Date -f "dd-MM-yyyy")
}
$FilePath = Join-Path @PathParams
$Groups = Get-ADObject -Filter 'ObjectClass -eq "group"' -SearchBase $OU
$i = 0
$tot = $Groups.count
$Data = foreach ($Group in $Groups) {
$i++
$ProgressParams = @{
Activity = 'Exporting AD Groups'
PercentComplete = ($i / $tot * 100)
status = "Processing Group $i of $tot : {0:N0} Completed" -f
($i / $tot * 100)
}
Write-Progress @ProgressParams
Get-ADGroupMember -Identity $Group |
Select-Object @{Name = 'Group'; Expression = {$Group.Name}},
@{Name = 'Member'; Expression = {$_.Name}},
@{Name = 'Enabled'; Expression = {
$Script:User = $false
if ($_.ObjectClass -eq 'user') {
$Script:User = Get-ADUser $_
if ($User.Enabled) {$true} else {$false}
}
else {
'NA/Group'
}
}
},
@{Name = 'FirstName'; Expression = {
if ($User) {
$User.GivenName
}
}
},
@{Name = 'LastName'; Expression = {
if ($User) {
$User.Surname
}
}
},
@{Name = 'CombinedName'; Expression = {
if ($User) {
"{0}, {1}" -f $User.GivenName, $User.Surname
}
}
}
}
$Data | Export-Csv -Path $FilePath -NoTypeInformation
你遇到的问题是你不能在$User
中使用Expression
之外的Select-Object
属性。这可以通过创建一个在整个脚本中可用的变量来修复,因此称为Script scope
,用作$Script:User
。
更多信息可以在Get-Help about_Scopes
或here找到。
在旁注中我会建议你使用适当的缩进,它使事情更具可读性。 splatted参数hashtable
也在这方面有所帮助。作为最后一个提示:如果您只使用一次,请不要创建变量。否则它只会让你后悔。