Skip to content

Commit 7728ccf

Browse files
authored
Merge pull request #255 from microsoft/bilong-setupassist
Simplify syntax for otherWellKnownObjects problem
2 parents ffb4603 + 47312b9 commit 7728ccf

File tree

2 files changed

+81
-81
lines changed

2 files changed

+81
-81
lines changed

Setup/SetupAssist.ps1

+80-80
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '', Justification = 'Parameter is used')]
99
[CmdletBinding()]
1010
param(
11-
[string]$OtherWellKnownObjectsContainer
11+
[switch]$OtherWellKnownObjects
1212
)
1313

1414
function IsAdministrator {
@@ -196,6 +196,80 @@ function Get-ExchangeAdSetupObjects {
196196
return $hash
197197
}
198198

199+
Function Test-OtherWellKnownObjects {
200+
[CmdletBinding()]
201+
param ()
202+
203+
$rootDSE = [ADSI]("LDAP://RootDSE")
204+
$exchangeContainerPath = ("CN=Microsoft Exchange,CN=Services," + $rootDSE.configurationNamingContext)
205+
206+
ldifde -d $exchangeContainerPath -p Base -l otherWellKnownObjects -f ExchangeContainerOriginal.txt
207+
208+
[array]$content = Get-Content .\ExchangeContainerOriginal.txt
209+
210+
if ($null -eq $content -or
211+
$content.Count -eq 0) {
212+
throw "Failed to export ExchangeContainerOriginal.txt file"
213+
}
214+
215+
$owkoLine = "otherWellKnownObjects:"
216+
$inOwkoLine = $false
217+
$outputLines = New-Object 'System.Collections.Generic.List[string]'
218+
$outputLines.Add($content[0])
219+
$outputLines.Add("changeType: modify")
220+
$outputLines.Add("replace: otherWellKnownObjects")
221+
222+
$index = 0
223+
while ($index -lt $content.Count) {
224+
$line = $content[$index++]
225+
226+
if ($line.Trim() -eq $owkoLine) {
227+
228+
if ($null -ne $testStringLine -and
229+
$null -ne $possibleAdd) {
230+
231+
if (!($testStringLine -like "*CN=Deleted Objects*")) {
232+
$outputLines.AddRange($possibleAdd)
233+
} else {
234+
Write-Host "Found object to remove: $testStringLine"
235+
}
236+
}
237+
$inOwkoLine = $true
238+
$possibleAdd = New-Object 'System.Collections.Generic.List[string]'
239+
$possibleAdd.Add($line)
240+
[string]$testStringLine = $line
241+
continue
242+
}
243+
244+
if ($inOwkoLine) {
245+
$possibleAdd.Add($line)
246+
$testStringLine += $line
247+
}
248+
249+
if ($index -eq $content.Count) {
250+
251+
if (!($testStringLine -like "*CN=Deleted Objects*")) {
252+
$outputLines.AddRange($possibleAdd)
253+
} else {
254+
Write-Host "Found object to remove: $testStringLine"
255+
}
256+
}
257+
}
258+
259+
if ([string]::IsNullOrEmpty($outputLines[-1])) {
260+
$outputLines[-1] = "-"
261+
} else {
262+
$outputLines.Add("-")
263+
}
264+
265+
$outputLines | Out-File -FilePath "ExchangeContainerImport.txt"
266+
267+
Write-Host("`r`nVerify the results in ExchangeContainerImport.txt. Then run the following command:")
268+
Write-Host("`r`n`tldifde -i -f ExchangeContainerImport.txt")
269+
Write-Host("`r`nRun Setup.exe again afterwards.")
270+
return
271+
}
272+
199273
Function MainUse {
200274
$whoamiOutput = whoami /all
201275

@@ -277,85 +351,11 @@ Function MainUse {
277351

278352
Function Main {
279353

280-
if (![string]::IsNullOrEmpty($OtherWellKnownObjectsContainer)) {
281-
282-
ldifde -d $OtherWellKnownObjectsContainer -p Base -l otherWellKnownObjects -f ExchangeContainerOriginal.txt
283-
284-
[array]$content = Get-Content .\ExchangeContainerOriginal.txt
285-
286-
if ($null -eq $content -or
287-
$content.Count -eq 0) {
288-
throw "Failed to export ExchangeContainerOriginal.txt file"
289-
}
290-
291-
$owkoLine = "otherWellKnownObjects:"
292-
$inOwkoLine = $false
293-
$outputLines = New-Object 'System.Collections.Generic.List[string]'
294-
$outputLines.Add($content[0])
295-
$outputLines.Add("changeType: modify")
296-
$outputLines.Add("replace: otherWellKnownObjects")
297-
298-
Function Test-DeleteObject ([string]$TestLine) {
299-
300-
if ($TestLine.Contains("CN=Deleted Objects")) {
301-
return $true
302-
}
303-
304-
return $false
305-
}
306-
307-
$index = 0
308-
while ($index -lt $content.Count) {
309-
$line = $content[$index++]
310-
311-
if ($line.Trim() -eq $owkoLine) {
312-
313-
if ($null -ne $testStringLine -and
314-
$null -ne $possibleAdd) {
315-
316-
if (!(Test-DeleteObject $testStringLine)) {
317-
$outputLines.AddRange($possibleAdd)
318-
} else {
319-
Write-Host "Found object to remove: $testStringLine"
320-
}
321-
}
322-
$inOwkoLine = $true
323-
$possibleAdd = New-Object 'System.Collections.Generic.List[string]'
324-
$possibleAdd.Add($line)
325-
[string]$testStringLine = $line
326-
continue
327-
}
328-
329-
if ($inOwkoLine) {
330-
$possibleAdd.Add($line)
331-
$testStringLine += $line
332-
}
333-
334-
if ($index -eq $content.Count) {
335-
336-
if (!(Test-DeleteObject $testStringLine)) {
337-
$outputLines.AddRange($possibleAdd)
338-
} else {
339-
Write-Host "Found object to remove: $testStringLine"
340-
}
341-
}
342-
}
343-
344-
if ([string]::IsNullOrEmpty($outputLines[-1])) {
345-
$outputLines[-1] = "-"
346-
} else {
347-
$outputLines.Add("-")
348-
}
349-
350-
$outputLines | Out-File -FilePath "ExchangeContainerImport.txt"
351-
352-
Write-Host("`r`nVerify the results in ExchangeContainerImport.txt. Then run the following command:")
353-
Write-Host("`tldifde -i -f ExchangeContainerImport.txt")
354-
Write-Host("Run Setup.exe again afterwards.")
355-
return
354+
if ($OtherWellKnownObjects) {
355+
Test-OtherWellKnownObjects
356+
} else {
357+
MainUse
356358
}
357-
358-
MainUse
359359
}
360360

361-
Main
361+
Main

Setup/SetupLogReviewer.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ Function Test-KnownOrganizationPreparationErrors {
263263

264264
Write-ErrorContext -WriteInfo $errorLine.Line
265265
[string]$ap = "Option 1: Restore the objects that were deleted."
266-
[string]$ap += "`r`n`tOption 2: Run the SetupAssist.ps1 script with '-OtherWellKnownObjectsContainer `"$($errorLine.Matches.Groups[2].Value)`"' to be able address deleted objects type"
266+
[string]$ap += "`r`n`tOption 2: Run the SetupAssist.ps1 script with '-OtherWellKnownObjects' to be able address deleted objects type"
267267
Write-ActionPlan $ap
268268
return $true
269269
}

0 commit comments

Comments
 (0)