Repository: microsoft/CSS-Exchange
File: Databases/VSSTester/ExchangeInformation/Get-ExchangeVersion.ps1 (PowerShell)

Code before:
﻿# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

function Get-ExchangeVersion {
    [OutputType([System.Void])]
    param(
        [Parameter(Mandatory = $true)]
        [string]
        $ServerName
    )

    Write-Host "$(Get-Date) Verifying Exchange version..."
    $exchVer = (Get-ExchangeServer $ServerName).AdminDisplayVersion
    if ($exchVer -is [string]) {
    # Agar string hai (deserialize ho gaya), split karke major/minor nikal lo
    $parts = $exchVer.Split('.')
    $exchVerMajor = [int]$parts[0]
    $exchVerMinor = if ($parts.Count -gt 1) { [int]$parts[1] } else { 0 }
} else {
    # Agar object hai, normal property access
    $exchVerMajor = $exchVer.Major
    $exchVerMinor = $exchVer.Minor
}


    switch ($exchVerMajor) {
        "14" {
            $exchVer = "2010"
        }
        "15" {
            switch ($exchVerMinor) {
                "0" {
                    $exchVer = "2013"
                }
                "1" {
                    $exchVer = "2016"
                }
                "2" {
                    $exchVer = "2019"
                }
            }
        }

        default {
            Write-Host "  This script is only for Exchange 2013, 2016, and 2019 servers."
            exit
        }
    }


Review comment (suggestion):
We should do this the same as what we do in `Get-ExchangeBuildVersionInformation`

https://github.com/microsoft/CSS-Exchange/blob/15f255e9ba0459d7ce0568d94911aa52bee02521/Shared/Get-ExchangeBuildVersionInformation.ps1#L101

Then we should remove the unsupported versions of Exchange.

```suggestion
# change variable above to [string]$AdminDisplayVersion = (Get-ExchangeServer $ServerName).AdminDisplayVersion
$split1 = $AdminDisplayVersion.Substring(($AdminDisplayVersion.IndexOf(" ")) + 1, 4).Split(".")
$buildStart = $AdminDisplayVersion.LastIndexOf(" ") + 1
$split2 = $AdminDisplayVersion.Substring($buildStart, ($AdminDisplayVersion.LastIndexOf(")") - $buildStart)).Split(".")
[System.Version]$exchangeVersion = "$($split1[0]).$($split1[1]).$($split2[0]).$($split2[1])"

if ($exchangeVersion.Major -eq 15 -and $exchangeVersion.Minor -ne 0) {
   if ($exchangeVersion.Minor -eq 1) { $exchVer = "2016" }
   else { 
     # handle everything else as 2019
     $exchVer = "2019"
   }
} else {
    throw "Unsupported build of Exchange. $($exchangeVersion.ToString())" 
}

```

Code after:
﻿# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

function Get-ExchangeVersion {
    [OutputType([System.Void])]
    param(
        [Parameter(Mandatory = $true)]
        [string]
        $ServerName
    )

    Write-Host "$(Get-Date) Verifying Exchange version..."

    # Use AdminDisplayVersion and parse it into a proper System.Version
    [string]$AdminDisplayVersion = (Get-ExchangeServer $ServerName).AdminDisplayVersion
    $split1 = $AdminDisplayVersion.Substring(($AdminDisplayVersion.IndexOf(" ")) + 1, 4).Split(".")
    $buildStart = $AdminDisplayVersion.LastIndexOf(" ") + 1
    $split2 = $AdminDisplayVersion.Substring($buildStart, ($AdminDisplayVersion.LastIndexOf(")") - $buildStart)).Split(".")
    [System.Version]$exchangeVersion = "$($split1[0]).$($split1[1]).$($split2[0]).$($split2[1])"

    if ($exchangeVersion.Major -eq 15 -and $exchangeVersion.Minor -ne 0) {
        if ($exchangeVersion.Minor -eq 1) {
            $exchVer = "2016"
        } else {
            # Handle everything else as 2019
            $exchVer = "2019"
        }
    } else {
        throw "Unsupported build of Exchange. $($exchangeVersion.ToString())"
    }

    Write-Host "  $ServerName is an Exchange $exchVer server."
}