Skip to content

Commit 891df05

Browse files
authored
Add files via upload
Switched to LDAP Filter for groups with >5000 members
1 parent 7780b4f commit 891df05

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

‎Scripts/01_Server_Logins.ps1‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,20 @@ catch
5858
Throw('SQLTranscriptase.psm1 not found')
5959
}
6060

61+
try
62+
{
63+
Import-Module ".\LoadSQLSmo.psm1"
64+
}
65+
catch
66+
{
67+
Throw('LoadSQLSmo.psm1 not found')
68+
}
69+
6170
LoadSQLSMO
6271

72+
# Load Powershell Module for AD Group Member Name Resolution
73+
Import-Module ActiveDirectory
74+
6375
function CopyObjectsToFiles($objects, $outDir) {
6476

6577
if (-not (Test-Path $outDir)) {
@@ -163,9 +175,8 @@ if ($OnDomain -eq $true)
163175
try
164176
{
165177
$old_ErrorActionPreference = $ErrorActionPreference
166-
$ErrorActionPreference = 'SilentlyContinue'
167-
168-
Import-Module ActiveDirectory
178+
$ErrorActionPreference = 'SilentlyContinue'
179+
169180

170181
# Test if Im on a windows Domain, If so and we find Windows Group SQL Logins, resolve all related Windows AD Users
171182
$MyDCs = Get-ADDomainController -Filter * | Select-Object name
@@ -281,7 +292,12 @@ foreach ($Login in $Logins)
281292
# Get all Users of this AD Group
282293
try
283294
{
284-
$ADGroupUsers = Get-AdGroupMember -identity $ADName -recursive -ErrorAction Stop |Where {$_.objectClass -eq "user"} | sort name
295+
#$ADGroupUsers = Get-ADGroupMember -Identity $ADName -Recursive -ErrorAction Stop | Where-Object {$_.objectClass -eq "user"} | Sort-Object name
296+
297+
# Dec 11 2024 - switch to LDAP filter to overcome 5000 member AD limit
298+
# First, Get the DistinguishedName of the Group
299+
$GroupDN = (Get-ADGroup -Identity $ADName).DistinguishedName
300+
$ADGroupUsers = Get-ADUser -LDAPFilter "(&(objectCategory=user)(memberof=$GroupDN))" -ErrorAction Stop | Sort-Object name
285301
}
286302
catch
287303
{
@@ -292,7 +308,7 @@ foreach ($Login in $Logins)
292308
# Export Users for this AD Group
293309
$myoutputfile = $WinGroupSinglePath+"Users in "+$myFixedGroupName+".sql"
294310
$myoutputstring = "-- These Domain Users are members of the SQL Login and Windows Group ["+$ADName+ "]`n"
295-
$myoutputstring | out-file -FilePath $myoutputfile -encoding ascii
311+
$myoutputstring | Out-File -FilePath $myoutputfile -encoding ascii
296312

297313
# Create the Group Itself
298314
CopyObjectsToFiles $login $WinGroupSinglePath
@@ -313,7 +329,7 @@ foreach ($Login in $Logins)
313329
{
314330
$CreateObjectName = "CREATE LOGIN ["+$ADDomain+"\"+$ADUser.SamAccountName+"] FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english] "
315331
}
316-
$CreateObjectName | out-file -FilePath $myoutputfile -append -Encoding ascii
332+
$CreateObjectName | Out-File -FilePath $myoutputfile -append -Encoding ascii
317333
}
318334

319335
}
@@ -401,7 +417,7 @@ foreach ($Login in $Logins)
401417

402418
# output results
403419
$MyScriptingFilePath = $WinUsersPath+"\"+$fixedFileName2+".sql"
404-
$CreateObjectName | out-file -FilePath $MyScriptingFilePath -Encoding ascii -Force
420+
$CreateObjectName | Out-File -FilePath $MyScriptingFilePath -Encoding ascii -Force
405421
}
406422
#else
407423
#{
@@ -632,5 +648,4 @@ else
632648

633649

634650
# Return To Base
635-
set-location $BaseFolder
636-
651+
set-location $BaseFolder

0 commit comments

Comments
 (0)