Repository: microsoft/CSS-Exchange
File: Admin/Remove-DuplicateEntriesFromIanaMappings.ps1 (PowerShell)

Code before:
        }

        $writeProgressParams.Activity = $activityBase + " Searching for duplicate entries in IanaTimeZoneMappings.xml"
        Write-Progress @writeProgressParams

        # Check if IanaTimeZoneMappings.xml contains duplicate entries - this is done by the custom Test-IanaTimeZoneMapping function
        $testIanaTimeZoneMappingResults = Test-IanaTimeZoneMapping -FilePath $mappingFilePath

        if ($null -ne $testIanaTimeZoneMappingResults -and
            $testIanaTimeZoneMappingResults.DuplicateEntries.Count -ge 1) {

            $duplicateEntries = $testIanaTimeZoneMappingResults.DuplicateEntries
            $ianaMappingXml = $testIanaTimeZoneMappingResults.IanaMappingXml

            Write-Host "[+] Duplicate entries detected!" -ForegroundColor Yellow

            try {
                $writeProgressParams.Activity = $activityBase + " Creating backup $mappingBackupFilePath"
                Write-Progress @writeProgressParams

                # If duplicate entries were detected, create a backup of the file before modifying it
                Copy-Item -Path $mappingFilePath -Destination $mappingBackupFilePath
                Write-Host "[+] Backup created: $mappingBackupFilePath"
            } catch {
                Write-Host "[+] Failed to create backup file. Inner Exception: $_" -ForegroundColor Red
                Write-Progress @writeProgressParams -Completed

                return
            }

            $dupeIndex = 0

            # Iterate through all of the duplicate entries and remove them one by one
            foreach ($dupe in $duplicateEntries) {
                Write-Host "[+] Processing duplicate entry: $dupe"

                $writeProgressParams.Activity = $activityBase + " Processing duplicate entry: $dupe"
                Write-Progress @writeProgressParams

                try {
                    # Select the duplicate node and remove it - we use SelectSingleNode here as it could be that there are multiple duplicates
                    $singleNode = $ianaMappingXml.SelectSingleNode("//Map[@IANA='$($dupe.IANA)' and @Win='$($dupe.Win)']")
                    $singleNode.ParentNode.RemoveChild($singleNode) | Out-Null

                    $dupeIndex++
                } catch {
                    Write-Host "[+] Failed to fix duplicate entry $dupe. Inner Exception: $_" -ForegroundColor Red
                    Write-Progress @writeProgressParams -Completed

                    return
                }

Review comment (suggestion):
Recommend wrapping everything in a `try` `finally` block that handles the `Write-Progress` inside the `finally` block you will always do `Write-Progress @writeProgressParams -Completed` so you would just need to do a `return` when you don't want to continue with the code.

Code after:
            }

            $writeProgressParams.Activity = $activityBase + " Searching for duplicate entries in IanaTimeZoneMappings.xml"
            Write-Progress @writeProgressParams

            # Check if IanaTimeZoneMappings.xml contains duplicate entries - this is done by the custom Test-IanaTimeZoneMapping function
            $testIanaTimeZoneMappingResults = Test-IanaTimeZoneMapping -FilePath $mappingFilePath

            if ($null -ne $testIanaTimeZoneMappingResults -and
                $testIanaTimeZoneMappingResults.DuplicateEntries.Count -ge 1) {

                $duplicateEntries = $testIanaTimeZoneMappingResults.DuplicateEntries
                $ianaMappingXml = $testIanaTimeZoneMappingResults.IanaMappingXml

                Write-Host "[+] Duplicate entries detected!" -ForegroundColor Yellow

                try {
                    $writeProgressParams.Activity = $activityBase + " Creating backup $mappingBackupFilePath"
                    Write-Progress @writeProgressParams

                    # If duplicate entries were detected, create a backup of the file before modifying it
                    Copy-Item -Path $mappingFilePath -Destination $mappingBackupFilePath
                    Write-Host "[+] Backup created: $mappingBackupFilePath"
                } catch {
                    Write-Host "[+] Failed to create backup file. Inner Exception: $_" -ForegroundColor Red

                    return
                }

                $dupeIndex = 0

                # Iterate through all of the duplicate entries and remove them one by one
                foreach ($dupe in $duplicateEntries) {
                    Write-Host "[+] Processing duplicate entry: $dupe"

                    $writeProgressParams.Activity = $activityBase + " Processing duplicate entry: $dupe"
                    Write-Progress @writeProgressParams

                    try {
                        # Select the duplicate node and remove it - we use SelectSingleNode here as it could be that there are multiple duplicates
                        $singleNode = $ianaMappingXml.SelectSingleNode("//Map[@IANA='$($dupe.IANA)' and @Win='$($dupe.Win)']")
                        $singleNode.ParentNode.RemoveChild($singleNode) | Out-Null

                        $dupeIndex++
                    } catch {
                        Write-Host "[+] Failed to fix duplicate entry $dupe. Inner Exception: $_" -ForegroundColor Red

                        return
                    }
                }