我尝试从OU导出姓氏,名字的AD组和用户,但是我只能使它与姓氏,名字一起使用。
我尝试的所有其他操作都为成员提供了一个空字符串。我尝试将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
答案 0 :(得分:1)
我认为这可以为您解决问题:
$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中找到。
在旁注中,我建议您使用适当的缩进,这会使内容更具可读性。分隔的参数hashtable
在这方面也有帮助。最后提示:如果只使用一次,则不要创建变量。否则以后只会使您感到困惑。