This repository has been archived by the owner on May 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
genQlikGroupUserList.psm1
154 lines (133 loc) · 6.01 KB
/
genQlikGroupUserList.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
function genQlikGroupUserList([xml]$Config, [string]$LogFile)
{
$users = New-Object System.Collections.ArrayList
#obtain the server instances from the settings file.
$Servers = $Config.Settings.LDAP.Servers.Server
#for each server, create a directory entry and get the group list
foreach($server in $Servers)
{
$Conn = $server.LDAP
$Name = $server.Name
LogWrite $LogFile "Getting group list for $Name"
foreach($path in $server.Paths.Path)
{
$FullConn = $Conn + $path
LogWrite $LogFile "Connecting to $FullConn"
if($server.Security)
{
$Entry = New-Object System.DirectoryServices.DirectoryEntry($FullConn,
$server.Security.UserId, $server.Security.Password, "none")
}
else
{
$Entry = New-Object System.DirectoryServices.DirectoryEntry($FullConn)
}
#Construct GroupList
foreach($group in $server.Groups.Group)
{
if($group.type -eq "file")
{
$GroupList = Import-Csv $group.'#text' -Header "Groups"
$GroupList = $GroupList.Groups
}
else
{
$GroupList += $group.'#text'
}
}
#Process Groups and add users that are members of provided universal groups
#to user arraylist
$i=0
foreach($GroupMember in $GroupList)
{
LogWrite $LogFile "Processing $GroupMember"
$LDAPFilter = "(&(objectClass=group)(cn=$GroupMember))"
# Setup range limits.
$Last = $False
$RangeStep = 999
$LowRange = 0
$HighRange = $LowRange + $RangeStep
$Total = 0
$ExitFlag = $False
Do
{
If ($Last -eq $True)
{
# Retrieve remaining members (less than 1000).
$Attributes = "member;range=$LowRange-*"
}
Else
{
# Retrieve 1000 members.
$Attributes = "member;range=$LowRange-$HighRange"
}
LogWrite $LogFile "Retrieving $attributes"
# Write-Host $Attributes
# Write-Host "Press any key to continue ..."
# $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$directorySearcher = New-Object System.DirectoryServices.DirectorySearcher($Entry, $LDAPFilter)
$directorySearcher.SizeLimit = 0;
$directorySearcher.PageSize = 500;
$directorySearcher.SearchScope = "Subtree"
$directorySearcher.PropertiesToLoad.Add("$Attributes")
try {
$SearchResults = $directorySearcher.FindAll()
$Count = 0
foreach($property in $SearchResults.Properties.PropertyNames)
{
#$property
if($property.StartsWith("member"))
{
# if(($SearchResults.Properties[$property].Count -gt 0) -or
# ($SearchResults.Properties[$property].Count -ne $null))
# {
#$SearchCount = $SearchResults.Properties[$property].Count
foreach($member in $SearchResults.Properties[$property])
{
#Write-Host "$Count|$member"
$Count = $Count + 1
$udc = $member -split ",DC="
$udc = $udc[1]
$uid = $member -split ","
$uid = $uid[0].SubString(3,$uid[0].length -3)
$users.Add(@($udc,$uid,$GroupMember,$member)) > $null
}
# }
}
}
Remove-Variable directorySearcher
Remove-Variable SearchResults
}
catch {
LogWrite $LogFile "No Results found for $GroupMember"
break
}
$Total = $Total + $Count
# If this is the last query, exit the Do loop.
If ($Last -eq $True) {
$ExitFlag = $True
}
Else
{
# If the previous query returned no members, the query failed.
# Perform one more query to retrieve remaining members (less than 1000).
If ($Count -eq 0) {$Last = $True}
Else
{
# Retrieve the next 1000 members.
$LowRange = $HighRange + 1
$HighRange = $LowRange + $RangeStep
}
}
} Until ($ExitFlag -eq $True)
LogWrite $LogFile "$GroupMember Total records created: $Total"
$i+=1
#Write-Progress -Activity "Processed Group: $GroupMember" -status "Processed $i Groups" -percentComplete ($i/$GroupList.Count*100)
}
}
}
LogWrite $LogFile "Completed genQlikGroupUserList"
Write-Host $users.Count
return ,$users
}
Export-ModuleMember -Function genQlikGroupUserList