-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModify.ps1
More file actions
275 lines (196 loc) · 8.83 KB
/
Modify.ps1
File metadata and controls
275 lines (196 loc) · 8.83 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
Param(
[Parameter(Position=0,Mandatory=$true)]
[string]$Datafile,
[Parameter(Position=1,Mandatory=$true)]
[ValidateSet('Live','Prelive','Test')]
[System.String]$Stage,
[Parameter(Position=2,Mandatory=$false)]
[System.Boolean]$ProcessMultiHit=$false,
[Parameter(Position=2,Mandatory=$false)]
[System.String]$DoneRecieptient=$null
)
. .\readINI.ps1
. .\WriteLog.ps1
$Starttime=get-date
$Timestamp=$Starttime.ToString("yyyyMMdd_HH_mm_ss")
function MakeModRequest{
param (
[Parameter(Mandatory)]
[System.DirectoryServices.Protocols.LdapConnection]$LDAPServer,
[Parameter(Mandatory)]
[string]$DN,
[Parameter(Mandatory)]
[string]$Attribute,
[string]$Value
)
if($Value)
{
$ModReq = [System.DirectoryServices.Protocols.ModifyRequest]::new()
$ModReq.DistinguishedName = $dn
Write-Log -Severity Information -Message ("Mod of: " + $dn)
Write-Log -Severity Information -Message ("Writing: "+ $Attribute+"="+$Value)
$replace = @{
Name = $Attribute
Operation = 'Replace'
} -as [System.DirectoryServices.Protocols.DirectoryAttributeModification]
$replace.Add($Value)
$ModReq.Modifications.Add($replace) | Out-Null
$result =$LDAPServer.SendRequest($ModReq)
return $result
}
else
{
$ModReq = [System.DirectoryServices.Protocols.ModifyRequest]::new()
$ModReq.DistinguishedName = $dn
Write-Log -Severity Information -Message ("Mod of: " + $dn)
Write-Log -Severity Information -Message ("Deleting: "+ $Attribute)
$delete = @{
Name = $Attribute
Operation = 'Delete'
} -as [System.DirectoryServices.Protocols.DirectoryAttributeModification]
$ModReq.Modifications.Add($delete) | Out-Null
$result =$LDAPServer.SendRequest($ModReq)
return $result
}
}
$invocation=$MyInvocation
Write-Log -Severity Information -Message ("START: " + $invocation.Line)
Write-Log -Severity Information -Message ("Reading Config")
$LDAPUser = GetConfValue -Header "General" -Key "LDAPUser"
$LDAPPass = GetConfValue -Header "General" -Key "LDAPPass"
$LDAPSearchDN= GetConfValue -Header "General" -Key "LDAPSearchDN"
$LDAPSizeLimit=GetConfValue -Header "General" -Key "LDAPaSizeLimit"
$LDAPPageSize= GetConfValue -Header "General" -Key "LDAPPageSize"
$TestServer= GetConfValue -Header "Stages" -Key "Test"
$PreliveServer= GetConfValue -Header "Stages" -Key "Prelive"
$LiveSerever= GetConfValue -Header "Stages" -Key "Live"
$outputfile= "output"+$Timestamp+".csv"
"SearchAttr;SearchVal;WriteAttr;WriteVal;DistinguishedName;Result" | Out-File -FilePath $outputfile -Encoding utf8
if(Test-Path -Path $Datafile)
{
Write-Log -Severity Information -Message ("Reading `"" + $Datafile + "`"")
$data = Import-Csv -Path $Datafile -Delimiter ";" -Encoding UTF8
$countrows=($data | measure).Count
Write-Log -Severity Information -Message ("Read " + $countrows + " rows")
}
else
{
Write-Log -Severity Error -Message ("Inputfile `"" + $Datafile + "`" not found")
exit 1
}
switch($Stage)
{
Live{
$LDAPDirectoryService = $LiveSerever
}
Prelive{
$LDAPDirectoryService = $PreliveServer
}
Test{
$LDAPDirectoryService = $TestServer
}
}
$null = [System.Reflection.Assembly]::LoadWithPartialName('System.DirectoryServices.Protocols')
$null = [System.Reflection.Assembly]::LoadWithPartialName('System.Net')
$LDAPServer = New-Object System.DirectoryServices.Protocols.LdapConnection $LDAPDirectoryService
$LDAPServer.AuthType = [System.DirectoryServices.Protocols.AuthType]::Basic
$LDAPServer.Timeout = New-Object system.TimeSpan 0,0,25,0,0
$LDAPServer.SessionOptions.ProtocolVersion = 3
$LDAPServer.SessionOptions.SecureSocketLayer =$true
$credentials = new-object "System.Net.NetworkCredential" -ArgumentList $LDAPUser,$LDAPPass
Try {
$ErrorActionPreference = 'Stop'
$LDAPServer.Bind($credentials)
$ErrorActionPreference = 'Continue'
}
Catch
{
Write-Log -Severity Error -Message ("Error binding to ldap - $($_.Exception.Message)")
Throw "Error binding to ldap - $($_.Exception.Message)"
}
$i=0
foreach($row in $data)
{
$i++
try{
$Scope = [System.DirectoryServices.Protocols.SearchScope]::Subtree
#$AttributeList = ,"+" #operational attributes
$AttributeList = ,"" #no attributes
$LDAPFilter ="(" + $row.SearchAttr + "=" + $row.SearchVal + ")"
$SearchRequest = New-Object System.DirectoryServices.Protocols.SearchRequest -ArgumentList $LDAPSearchDN,$LDAPFilter,$Scope,$AttributeList
if($ProcessMultiHit)
{
$searchRequest.SizeLimit = $LDAPSizeLimit
}
else
{
$searchRequest.SizeLimit = 3000
}
$SearchRequest.Attributes.Add("dn") | Out-Null
$pageResultControl = New-Object System.DirectoryServices.Protocols.PageResultRequestControl(10)
[Void]$searchRequest.Controls.Add($pageResultControl)
$response = @()
$j=0
Write-Log -Severity Information -Message ("Search: " + $SearchRequest.Filter + " in: " +$SearchRequest.DistinguishedName)
do
{
$j++
$searchResponse = [System.DirectoryServices.Protocols.SearchResponse] $LDAPServer.SendRequest($searchRequest)
$searchResponse.Controls | ? { $_ -is [System.DirectoryServices.Protocols.PageResultResponseControl] } | % {$pageResultControl.Cookie = ([System.DirectoryServices.Protocols.PageResultResponseControl]$_).Cookie}
$response += $searchResponse.Entries
if ($response.Count -ge $searchRequest.SizeLimit)
{
break
}
}
while ($pageResultControl.Cookie.length -gt 0)
$HitCount = ($response | measure).Count
Write-Log -Severity Information -Message ("Recieved "+ $HitCount + " Objects from " + $j + " Pages")
if($HitCount -ge 1)
{
if($ProcessMultiHit)
{
foreach($identity in $response)
{
$result = MakeModRequest -LDAPServer $LDAPServer -DN $identity.DistinguishedName -Attribute $row.WriteAttr -Value $row.WriteVal
Write-Log -Severity Information -Message ($result.ErrorMessage)
$row.SearchAttr + ";" + $row.SearchVal + ";" + $row.WriteAttr + ";" + $row.WriteVal + ";" + $identity.DistinguishedName + ";" + $result.ErrorMessage | Out-File -FilePath $outputfile -Encoding utf8 -Append
}
}
else
{
$result = MakeModRequest -LDAPServer $LDAPServer -DN $response[0].DistinguishedName -Attribute $row.WriteAttr -Value $row.WriteVal
Write-Log -Severity Information -Message ($result.ErrorMessage)
$row.SearchAttr + ";" + $row.SearchVal + ";" + $row.WriteAttr + ";" + $row.WriteVal + ";" + $response[0].DistinguishedName + ";" + $result.ErrorMessage | Out-File -FilePath $outputfile -Encoding utf8 -Append
}
}
else
{
$row.SearchAttr + ";" + $row.SearchVal + ";" + $row.WriteAttr + ";" + $row.WriteVal + ";" + "No results to process" + ";" + "No results to process" | Out-File -FilePath $outputfile -Encoding utf8 -Append
Write-Log -Severity Warning -Message ("No results to process")
}
}
catch
{
Write-Log -Severity Error -Message ("Error while Processing Line " + $i)
Write-Log -Severity Error -Message ("Input Data: " + ($row | ConvertTo-Json -Compress).ToString())
Write-Log -Severity Error -Message ("Error: " + $_.Exception.Message)
Write-Log -Severity Error -Message ("Stacktrace: " + $_.ScriptStackTrace)
}
}
Write-Log -Severity Information -Message ("END")
if($DoneRecieptient)
{
$outputfragment=Import-Csv $outputfile -Delimiter ";"| ConvertTo-Html -Fragment
$style=
$body=""
$body+= "<div><p>Dies ist eine Benachrichtigung über den beendeten Lauf folgendes Scripts:</p>"
$body+= "<ul>"
$body+= "<li><p>Startzeitpunkt: " + $Starttime + "</p></li>"
$body+= "<li><p>Aufruf: " + $invocation.Line + "</p></li>"
$body+= "<li><p>Aufrufender User: " + $env:UserName+"@"+$env:COMPUTERNAME + "</p></li>"
$body+= "<li><p>Laufzeit: " + [Math]::round(((Get-Date)-$Starttime).TotalMinutes,0) + " Minuten</p></li>"
$body+= "</ul>"
$body+= "</div>"+ $outputfragment
.\MailDoneNotification.ps1 -Body $body -Subject ("Done: "+$invocation.Line) -To $DoneRecieptient
}