从Exchange / AD导出组数据时,剩下的域名全是CN =,OU =,OU =等。我可以在脚本中进行一些更改,以使其仅导出名称,而不必进入excel并搜索和替换不需要的内容吗?
#Written by Tekwhat 10-26-17
write-host "Group Memberships"
#Settings for file ouput
$fLocation = "D:\Exchange Reports\O365 Reports\"
import-module activedirectory
#Get OU
$OU = Read-Host -Prompt "Input the OU name to search: (0202 - Dev Bank)"
#create File to write report to:
$fName = $fLocation+$OU+" Group Memberships.csv"
$test = test-path $fName
if ($test -eq $True)
{
write-host "Removing Old File..." -ForeGroundColor Red
Remove-Item $fName
}
#Else
#{
#New-Item $fName -type file
#}
Write-host "Creating New File..." -ForeGroundColor darkgreen
New-Item $fName -type file
$SearchBase = "OU=$OU,OU=Hosted Exchange Customers,DC=Domain,DC=local"
$ADGroups = Get-ADGroup -Filter * -SearchBase $SearchBase | % { [ADSI]("LDAP://$_") }
$Members = @()
foreach ($ADGroup in $ADGroups){
$Members += $ADGroup.Member |
Select -Property @{Name="ADGroup";Expression={$ADGroup.cn}},
@{Name="Member";Expression={$_}}
}
$Members | Select-Object AdGroup, Member | Export-Csv -Path $fname -NoType
write-host "Your file is located at " $fname -ForegroundColor DarkGreen
上述内容的导出内容如下:
“所有员工”,“ CN =测试X用户,OU = OU名称,OU =托管Exchange 客户,DC =域,DC =本地”
我必须进入excel并搜索替换“ CN =”和“ OU = OUname,OU = Hosted Exchange 客户,DC =域,DC =本地”以获取可用数据。
答案 0 :(得分:1)
对于我的假设有误,我深表歉意,但我相信您在member属性的输出上遇到了麻烦。您可以使用类似以下的内容:
$SearchBase = "OU=$OU,OU=Hosted Exchange Customers,DC=Domain,DC=local"
$ADGroups = Get-ADGroup -Filter * -SearchBase $SearchBase -Properties Member,CN
$Members = @()
foreach ($ADGroup in $ADGroups){
$Members += $ADGroup.Member |
Select -Property @{Name="ADGroup";Expression={$ADGroup.cn}},
@{Name="Member";Expression={($_ | Select-String -pattern "(?<=CN=).*?(?=,OU=|,DC=)").matches.value}}
}
上面的代码的一个潜在影响是,每个成员都有一个重复的ADGroup名称。如果那不是故意的,那么就需要改变一切。
我保留了您的大部分代码,因此我将解释更改的内容:
($_ | Select-String -pattern "(?<=CN=).*?(?=,OU=|,DC=)").matches.value
$_
引用$ADGroup.Member
中的当前管道对象Select-String
基于正则表达式匹配从管道输入中选择一个字符串。正则表达式文本将传递到-pattern
参数。"(?<=CN=).*?(?=,OU=|,DC=)"
是正则表达式(正则表达式)。
(?<=CN=)
:(?<=)
是一种积极的后向机制,它在比赛中当前位置后面寻找字符串CN=
。.*?
匹配尽可能少的字符(.
),除了换行符(?=,OU=|,DC=)
使用正向超前机制((?=)
)查找字符串,OU=
或(|
)字符串,DC=
。.Matches
是Matches
输出的Select-String
属性。它包含其他用于指定正则表达式捕获组或从多个匹配项中选择的属性。 value
是此类属性之一,其中包含实际的字符串匹配项。答案 1 :(得分:1)
根据您的最终目标,您似乎已经对此进行了过度设计。如果我错了,请纠正我。然而,在查看您的代码和最终目标时,这就是我的看法。
默认情况下,不需要仅将Write-Host(颜色对象除外)发送到屏幕(“ Write-Output”(写入输出)),因此您不必专门将其写出来,但这是一种样式选择。
"Create OU based Group Memberships Report"
$fLocation = 'C:\Temp'
# no need to do this as modules are auto imported since v3, but it does not hurt to have it here
# Import-Module -Name ActiveDirectory
# Use a GUI to provide an OU list to select from.
$OUName = Get-ADOrganizationalUnit -Filter '*' |
Select-Object -Property Name, DistinguishedName |
Out-GridView -Title 'Select the OU name to search' -PassThru
# Set a file name to use. 'spaces in files names are just bad', so remove them
$fName = "$(($OUName).Name)_OU_ADGroup_Membership.csv" -replace ' '
# Remove any report of the same name
If(Test-Path -Path "$fLocation\$fName")
{
Write-Warning -Message "A previous version of the report file is in the destination folder. Removing the file!"
Remove-Item -Path "$fLocation\$fName" -Force
}
# Collect OU, AD group and user data and create a new CSV file
Get-ADOrganizationalUnit -Identity $OUName.DistinguishedName |
ForEach{
Get-ADGroup -Filter * -SearchBase $PSItem |
ForEach{
$ADGroup = $PSItem.Name
Get-ADGroupMember -Identity $ADGroup |
Select-Object @{Name = 'GroupName';Expression = {$ADGroup}},
Name, SamAccountName |
Export-Csv -Path "$fLocation\$fName" -NoTypeInformation -Append
}
}
Write-Host "Your new report file is licated here: $fLocation\$fName" -ForegroundColor Yellow
Import-Csv -Path "$fLocation\$fName"
#Results
WARNING: A previous version of the report file is in the destination folder. Removing the file!
Your new report file is licated here: C:\Temp\LabUsers_OU_ADGroup_Membership.csv
GroupName Name SamAccountName
--------- ---- --------------
...
TestUsers Test User001 testuser001
...