這似乎是我缺乏的一個領域。我已經編寫了這個腳本,如果用戶不在其他3中,並且只有他們具有Exchange GUID,纔會將用戶添加到1個組中,但由於來回調用了AD,所以它像往常一樣緩慢。我嘗試過使用散列表的各種嘗試,但我無法弄清楚如何從鍵中獲得像MemberOf這樣的子字符串屬性。我發現通過$hash = @{}
首先收集數據的優勢,但是我的各種測試總是出現null,因爲我不知道如何從中取出數據。下面是我想把它放回到舊的工作版本之後... THXPowershell陣列或哈希表?
Import-Module ActiveDirectory
$a = Get-Date
$Logs = "C:\scripts\SEC-E2010TierGroup3\SEC-E2010TierGroup3-additions_$((Get-Date).ToString('MM-dd-yyyy')).csv"
$Grp0 = "SEC-E2010TierGroup0"
$Grp1 = "SEC-E2010TierGroup1"
$Grp2 = "SEC-E2010TierGroup2"
$Grp3 = "SEC-E2010TierGroup3"
#$ADUserTable = @{}
$Users = Get-ADUser -server "dc114" -LdapFilter "(&(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))(&(objectCategory=person)(objectClass=user)(!objectClass=inetOrgPerson))(sAMAccountName=s0*)(!sAMAccountName=*-d)(!sAMAccountName=*-e)(!sAMAccountName=*-a)(!Name=Test*)(!Name=v-*)(!employeeID=\00))" -properties SamAccountName, DistinguishedName,Name, memberof, msExchMailboxGuid
<#ForEach-Object {
$ADUserTable[$_.SamAccountName] = $_
}#>
function Get-GroupMembership($DN,$group){
$objEntry = [adsi]("LDAP://"+$DN)
$objEntry.memberOf | where { $_ -match $group}
}
#Usage:
# Get-GroupMembership "DistinguishedName" "DomainAdmins"#>
<#$Active = #>ForEach ($user in $Users) {
$strName = $User.samaccountname
$DNUser = $User.distinguishedname
$strFilter = "(&(objectCategory=User)(samAccountName=$strName))"
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.Filter = $strFilter
$objPath = $objSearcher.FindOne()
$objUser = $objPath.GetDirectoryEntry()
$Exch = $objUser.msExchMailboxGuid#>
#If here, has an Exchange Mailbox.(Get-GroupMembership "$UserDN" "$Grp0")
if ($Exch.count -eq 1){
If (Get-GroupMembership "$DNUser" "$Grp0") {(""+ $a +" [" + $User + "], User:[" + $User.value.Name + "] in SEC-E2010TierGroup0.") | Out-File -FilePath $Logs -Append
Continue}
If (Get-GroupMembership "$DNUser" "$Grp1") {(""+ $a +" [" + $User + "], User:[" + $User.value.Name + "] in SEC-E2010TierGroup1.") | Out-File -FilePath $Logs -Append
Continue}
If (Get-GroupMembership "$DNUser" "$Grp2") {(""+ $a +" [" + $User + "], User:[" + $User.value.Name + "] in SEC-E2010TierGroup2.") | Out-File -FilePath $Logs -Append
Continue}
If (!(Get-GroupMembership "$DNUser" "$Grp3")) {
(""+ $a +" [" + $User + "], User:[" + $User.value.Name + "] not in SEC-E2010TierGroup3. Adding to group3.") | Out-File -FilePath $Logs -Append}
}
#If here, no Exch Mailbox.
if ($Exch.count -eq 1) {(""+ $a +" [" + $User + "], User:[" + $User.value.Name + "] does not have Exchange GUID.") | Out-File -FilePath $Logs -Append
continue}
} #$Active | Export-csv C:\Scripts\SEC-E2010TierGroup3\SEC-E2010TIER-Pre-Implimentation-rpt_2-18-2016.csv -NoTypeInformation
代碼中有一個明智的引號。我用普通的雙引號替換它以滿足Google Prettify。 – Matt