AzStackHciNetwork/AzStackHci.Network.Helpers.psm1

Import-LocalizedData -BindingVariable lnTxt -FileName AzStackHci.Network.Strings.psd1

function Test-StorageAdapterReadiness
{
    [CmdletBinding()]
    param (
        [System.Management.Automation.Runspaces.PSSession[]] $PSSession,
        [PSObject[]] $AtcHostIntents,
        [PSCredential] $SessionCredential
    )

    try
    {
        [System.Management.Automation.Runspaces.PSSession[]] $AllNodeSessions = @()

        foreach ($session in $PSSession)
        {
            #region Make sure session is there and open
            # This is needed here as the Test-MgmtIP test might break the session due to potential VMSwitch/vNIC creation
            if ($session.State -ne 'Opened')
            {
                Log-Info "Previous PSSession is closed. Opening new session for testing"
                if ($SessionCredential)
                {
                    $sessionToCheck = New-PsSessionWithRetriesInternal -Node $session.ComputerName -Credential $SessionCredential
                }
                else
                {
                    throw "Session is not opened and no credential provided"
                }
            }
            else
            {
                $sessionToCheck = $session
            }
            #endregion

            $AllNodeSessions += $sessionToCheck
        }

        # For the AtcHostIntents object array, we only need to check the storage only intents
        [PSObject[]] $storageIntents = $AtcHostIntents | Where-Object { $_.TrafficType.Contains("Storage") -and (-not $_.TrafficType.contains("Management")) -and (-not $_.TrafficType.contains("Compute")) }
        [String[]] $allPhysicalAdaptersToCheck = @()
        if ($storageIntents.Count -eq 1)
        {
            $allPhysicalAdaptersToCheck = $storageIntents[0].Adapter
        }
        elseif ($storageIntents.Count -gt 1)
        {
            # Should not get into here as ATC does not support multiple storage intent, so the input $AtcHostIntents
            # object should not have multiple storage intent in it, but keep it here as a safe guard
            throw "More than one storage intent found in the AtcHostIntents object array. Fail the storage adapter configuration validation."
        }
        else
        {
            Log-Info "No storage only intent found in the AtcHostIntents object array. Skip the storage adapter configuration validation."
            return
        }

        $storageAdapterReadinessResults = @()

        #region Test storage adatper configuration
        # Now, check every session and make sure that storage pNIC don't have IP or VLANID
        foreach ($testSession in $AllNodeSessions)
        {
            Log-Info "Checking storage adapter IP status on $($testSession.ComputerName)"
            $adapterReadinessResult = Invoke-Command -Session $testSession -ScriptBlock ${function:CheckStorageAdapterReadiness} -ArgumentList @(, $allPhysicalAdaptersToCheck)

            Log-Info "Got storage adapter readiness validation results from $($testSession.ComputerName)"
            $storageAdapterReadinessValidationStatus = if ($adapterReadinessResult.Pass) { 'SUCCESS' } else { 'FAILURE' }
            $storageAdapterReadinessValidationDetailMessage = $adapterReadinessResult.Message

            $storageAdapterReadinessRstObject = @{
                Name               = 'AzStackHci_Network_Test_StorageAdapterReadiness'
                Title              = 'Test storage adapter readiness on node'
                DisplayName        = 'Test storage adapter readiness on node'
                Severity           = 'CRITICAL'
                Description        = 'Test storage adapter readiness on node: no pre-defined IP on it, no VLANID configured on it'
                Tags               = @{}
                Remediation        = "Remove any pre-defined IP on the storage adapter, remove any VLANID configured on the storage adapter"
                TargetResourceID   = "StorageAdapterReadiness"
                TargetResourceName = "StorageAdapterReadiness"
                TargetResourceType = "StorageAdapterReadiness"
                Timestamp          = [datetime]::UtcNow
                Status             = $storageAdapterReadinessValidationStatus
                AdditionalData     = @{
                    Source    = $testSession.ComputerName
                    Resource  = 'StorageAdapterReadiness'
                    Detail    = $storageAdapterReadinessValidationDetailMessage
                    Status    = $storageAdapterReadinessValidationStatus
                    TimeStamp = [datetime]::UtcNow
                }
                HealthCheckSource  = $ENV:EnvChkrId
            }

            $storageAdapterReadinessResults += New-AzStackHciResultObject @storageAdapterReadinessRstObject
        }
        #endregion

        return $storageAdapterReadinessResults
    }
    catch
    {
        throw $_
    }
}

function Test-StorageConnection
{
    <#
    .SYNOPSIS
        Test stamp storage connection for deployment
 
    .DESCRIPTION
        This validator tests the storage connection for the deployment using Test-Cluster or ping MESH.
        It will only be run if the end user provides one storage intent:
            If the storage intent is not converged intent, we check pNIC connection only.
            If the storage intent is converged intent (a.k.a., with management or compute intent combined), we create VMSwitch and storage vNIC for connection testing.
 
        The validation will be done with one of below choices:
            Run “Test-Cluster” with network test
                With this test, there will be only 1 validation result generated
            Run ping mesh on each host for storage adapters using APIPA (Automatic Private IP Addressing) (address in subnet 169.254.0.0/16).
            We only run “ping” test and check if the results have “Reply from <TARGETIP>: bytes=” string in it.
                With this test, there will be 1 validation result generated for each of the server in the stamp.
 
        After the validation is finished, we clean the test artifacts created during the validation (like the VLANID configuration on pNIC, VMSwitch itself, etc.)
 
    .PARAMETER PSSession
        The remote session array to the nodes to be validated
 
    .PARAMETER AtcHostIntents
        The ATC host intent object array, which contains the intent information for the nodes
        Easiest way to get this is to convert from the "HostNetwork | Intents" object in the unattended JSON file.
 
    .PARAMETER StorageAdapterVLANIDInfo
        The hashtable that contains the physical storage adapter name and VLANID information
 
    .PARAMETER SessionCredential
        The credential to be used for the remote session
 
    .EXAMPLE
        Test-StorageConnection -PSSession $PSSession -AtcHostIntents $AtcHostIntents -StorageAdapterVLANIDInfo $StorageAdapterVLANIDInfo -SessionCredential $SessionCredential
    #>

    [CmdletBinding()]
    param (
        [System.Management.Automation.Runspaces.PSSession[]] $PSSession,
        [PSObject[]] $AtcHostIntents,
        [System.Collections.Hashtable] $StorageAdapterVLANIDInfo,
        [PSCredential] $SessionCredential
    )

    try
    {
        Log-Info "Start to run Test-StorageConnection function"

        [System.Management.Automation.Runspaces.PSSession[]] $newTestSessionsBeforeChecking = @()
        foreach ($testSession in $PSSession)
        {
            #region Make sure the test session is opened
            Log-Info "Make sure test session is opened for $($testSession.ComputerName)"
            if ($testSession.State -ne 'Opened')
            {
                Log-Info "Previous PSSession is closed. Opening new session for testing"
                if ($SessionCredential)
                {
                    $sessionToCheck = New-PsSessionWithRetriesInternal -Node $testSession.ComputerName -Credential $SessionCredential
                }
                else
                {
                    throw "Session is not opened and no credential provided"
                }
            }
            else
            {
                $sessionToCheck = $testSession
            }
            #endregion

            $newTestSessionsBeforeChecking += $sessionToCheck
        }

        #region Define variables used during the validation
        [PSObject[]] $allStorageAdaptersToCheck = @()
        [System.Collections.Hashtable] $storageAdapterVlanIdToConfig = @{}
        [System.Boolean] $needStorageVMSwitch = $false
        [System.String] $storageVMSwitchName= ""
        # If we need to create VMSwitch, we will use only 1 pNIC for the VMSwitch
        [System.String] $adapterForStorageVMSwitch = ""
        [System.Boolean] $needMgmtOnStorageVMSwitch = $false
        #endregion

        #region Prepare variables
        # Only need to worry about storage intent
        [PSObject[]] $tmpStorageIntents = $AtcHostIntents | Where-Object { $_.TrafficType.Contains("Storage") }
        [String[]] $tmpStorageIntentPhysicalAdapters = @()

        # Prepare $allStorageAdaptersToCheck, $needStorageVMSwitch, $storageVMSwitchName, $adapterForStorageVMSwitch, $needMgmtOnStorageVMSwitch, $storageAdapterVlanIdToConfig
        if ($tmpStorageIntents.Count -eq 1)
        {
            Log-Info "Found storage intent [ $($tmpStorageIntents[0].Name) ]. Need to run storage adapter connection validation."

            $tmpStorageIntentPhysicalAdapters = $tmpStorageIntents[0].Adapter

            if ($tmpStorageIntentPhysicalAdapters.Count -eq 0)
            {
                # Ideally should not come to here but keep it here as a safe guard
                Log-Info "No physical adapter found in the storage intent. Fail the storage adapter connection validation."
                throw "No physical adapter found in the storage intent. Fail the storage adapter connection validation."
            }

            Log-Info "Found physical adapter list [ $($tmpStorageIntentPhysicalAdapters | Out-String) ] in the intent."

            # If it is a converged intent, we will need to create VMSwitch/vNIC for validation
            # if it is storage only intent, we just check the physical storage adapter
            if ($tmpStorageIntents[0].TrafficType.Contains("Management") -or $tmpStorageIntents[0].TrafficType.Contains("Compute"))
            {
                # Converged intent with storage, will need to create vNIC for storage
                Log-Info "Converged intent. Will need to create VMSwitch/vNIC for storage connection validation."

                foreach ($tempName in $tmpStorageIntentPhysicalAdapters)
                {
                    # Pre-define the storage vNIC to be checked
                    $allStorageAdaptersToCheck += "vStorage($($tempName))"
                    $storageAdapterVlanIdToConfig.Add("vStorage($($tempName))", $StorageAdapterVLANIDInfo[$tempName])
                }

                $needStorageVMSwitch = $true
                $tmpGuid = [System.Guid]::NewGuid()
                $storageVMSwitchName = "StorageTestVMSwitch$($tmpGuid)"

                Log-Info "Will need to create VMSwitch with name $($storageVMSwitchName) for storage connection validation."

                $adapterForStorageVMSwitch = $tmpStorageIntentPhysicalAdapters[-1]

                if ($tmpStorageIntentPhysicalAdapters.Count -eq 1)
                {
                    $needMgmtOnStorageVMSwitch = $true
                    Log-Info "Only one adapter in the converged intent. Will need make sure mgmt connection vNIC configured correctly on the VMSwitch."
                }
            }
            else
            {
                Log-Info "Storage only intent. Will need to run storage connection validation on pNIC only."
                $allStorageAdaptersToCheck = $tmpStorageIntentPhysicalAdapters
                $storageAdapterVlanIdToConfig = $StorageAdapterVLANIDInfo
            }

            Log-Info "Storage adapter NAME list used for the validation:"
            Log-Info "$($allStorageAdaptersToCheck | Out-String)"
            Log-Info "Storage adapter VLANID list used for the validation:"
            Log-Info "$($storageAdapterVlanIdToConfig | Out-String)"
        }
        elseif ($tmpStorageIntents.Count -gt 1)
        {
            # Should not get into here as ATC does not support multiple storage intent, so the input $AtcHostIntents
            # object should not have multiple storage intent in it, but keep it here as a safe guard
            Log-Info "More than one storage intent found in the AtcHostIntents object array. Fail the storage adapter connection validation."
            throw "More than one storage intent found in the AtcHostIntents object array. Fail the storage adapter connection validation."
        }
        else
        {
            Log-Info "No storage intent found in the AtcHostIntents object array. Skip the storage adapter connection validation."
            return
        }
        #endregion

        # validation results to be returned
        $storageAdapterConnectionResults = @()

        [System.Boolean] $needCleanUpStorageVMSwitch = $false

        if ($needStorageVMSwitch)
        {
            # First make sure all the nodes have same configuration

            Log-Info "Need to create storage VMSwitch for the validation. Make sure same configuration on all host(s)."

            [System.Boolean] $allNodeSameConfig = $true
            [System.String] $allNodeVMSwitchStatus = $null
            [System.String] $existingVMSwitchOnMachine = $null

            [System.String] $nodesVMSwitchConfigStatusMessage = "Server VMSwitch Configuration Status:"

            foreach ($testSession in $newTestSessionsBeforeChecking)
            {
                Log-Info "Checking VMSwitch configuration on node $($testSession.ComputerName)"

                $currentNodeConfig = Invoke-Command -Session $testSession -ScriptBlock {
                    param ($AdapterForStorageVMSwitch)

                    $retVal = New-Object PSObject -Property @{
                        retValVMSwitchStatus = ""
                        existingVMSwitchName = ""
                    }

                    [System.String] $retValVMSwitchStatus = ""
                    [System.String] $existingVMSwitchName = ""
                    $storageadapterGuidOnCurrentNode = (Get-NetAdapter -Physical -Name $AdapterForStorageVMSwitch -ErrorAction SilentlyContinue).InterfaceGuid.Trim('{').Trim('}')

                    try
                    {
                        $vmSwitchTeam = Get-VMSwitchTeam -ErrorAction SilentlyContinue

                        if ($vmSwitchTeam -and ($vmSwitchTeam.NetAdapterInterfaceGuid.Guid -contains $storageadapterGuidOnCurrentNode))
                        {
                            # Already have a VMSwitch that contains the storage physical adapter, no need to create a new one
                            $retValVMSwitchStatus = "EXIST_VMSWITCH"
                            $existingVMSwitchName = $vmSwitchTeam.Name
                        }
                        else
                        {
                            # Need to create VMSwitch on node for storage connection validation
                            $retValVMSwitchStatus = "NEED_CREATE_VMSWITCH"
                        }
                    }
                    catch
                    {
                        $retValVMSwitchStatus = "HYPER_V_POWERSHELL_NOT_READY"
                    }

                    $retVal.retValVMSwitchStatus = $retValVMSwitchStatus
                    $retVal.existingVMSwitchName = $existingVMSwitchName

                    return $retVal
                } -ArgumentList $adapterForStorageVMSwitch

                Log-Info "Current node $($testSession.ComputerName) with VMSwitch status $($currentNodeConfig.retValVMSwitchStatus)"

                if (-not $allNodeVMSwitchStatus)
                {
                    $allNodeVMSwitchStatus = $currentNodeConfig.retValVMSwitchStatus
                    $existingVMSwitchOnMachine = $currentNodeConfig.existingVMSwitchName
                }
                else
                {
                    if ($allNodeVMSwitchStatus -ne $currentNodeConfig.retValVMSwitchStatus)
                    {
                        Log-Info "VMSwitch status is different with other nodes. Will fail the validation."
                        $nodesVMSwitchConfigStatusMessage += "`n Server $($testSession.ComputerName): $($currentNodeConfig.retValVMSwitchStatus)"
                        $allNodeSameConfig = $false
                    }
                }
            }

            if ((-not $allNodeSameConfig) -or ($allNodeVMSwitchStatus -notin @("EXIST_VMSWITCH", "NEED_CREATE_VMSWITCH")))
            {
                # In case the node VMSwitch configuration is different between each other, we cannot do anything here
                # Should not be here, but will fail the validation just in case end user did not configure the system correctly
                $nodesVMSwitchConfigStatusMessage += "`nCannot check VM switch configuration between servers for storage adapters. Skip the validation."
                $nodesVMSwitchConfigStatusMessage += "`nMake sure the storage adapter configuration are the same across all nodes."
                $nodesVMSwitchConfigStatusMessage += "`n If teamed within a VMSwitch, the teaming should be same on all nodes."
                $nodesVMSwitchConfigStatusMessage += "`n The VMSwitch might be a left over of previous environment validator run. You could clean it up manually."
                $nodesVMSwitchConfigStatusMessage += "`n For example: Remove-VMSwitch -Name `"StorageTestVMSwitch`" -Force"
                Log-Info $nodesVMSwitchConfigStatusMessage -Type 'WARNING'

                $storageAdapterConnectionRstObject = @{
                    Name               = "AzStackHci_Network_Test_StorageConnections_VMSWITCH_CONFIG_DIFFERENT"
                    Title              = 'VMSwitch configuration different between nodes for storage adpaters.'
                    DisplayName        = 'VMSwitch configuration different between nodes for storage adpaters.'
                    Severity           = 'WARNING'
                    Description        = 'VMSwitch configuration different between nodes for storage adpaters'
                    Tags               = @{}
                    Remediation        = 'To validate storage connections, make sure the storage adapter configuration are the same across all nodes. If teamed within a VMSwitch, the teaming should be same on all nodes.'
                    TargetResourceID   = 'CannotFindSameStorageAdapterConfiguration'
                    TargetResourceName = 'CannotFindSameStorageAdapterConfiguration'
                    TargetResourceType = 'CannotFindSameStorageAdapterConfiguration'
                    Timestamp          = [datetime]::UtcNow
                    Status             = 'FAILURE'
                    AdditionalData     = @{
                        Source    = ''
                        Resource  = 'CannotFindSameStorageAdapterConfiguration'
                        Detail    = $nodesVMSwitchConfigStatusMessage
                        Status    = 'FAILURE'
                        TimeStamp = [datetime]::UtcNow
                    }
                    HealthCheckSource  = $ENV:EnvChkrId
                }

                $storageAdapterConnectionResults += New-AzStackHciResultObject @storageAdapterConnectionRstObject

                return $storageAdapterConnectionResults
            }

            if ($allNodeVMSwitchStatus -eq "EXIST_VMSWITCH")
            {
                $needCleanUpStorageVMSwitch = $false

                # Just need to create new storage vNIC to test the storage connection
                foreach ($testSession in $newTestSessionsBeforeChecking)
                {
                    Invoke-Command -Session $testSession -ScriptBlock {
                        New-Item -Path "Function:\CreateStorageVNIC" -Value ${using:function:CreateStorageVNIC} -Force | Out-Null

                        CreateStorageVNIC -StorageVMSwitchName $using:existingVMSwitchOnMachine `
                                        -StorageVNICNames $using:allStorageAdaptersToCheck `
                                        -StorageAdapterVLANIDInfo $using:storageAdapterVlanIdToConfig
                    }
                }
            }
            elseif ($allNodeVMSwitchStatus -eq "NEED_CREATE_VMSWITCH")
            {
                $needCleanUpStorageVMSwitch = $true
                # Need to create VMSwitch on each node for storage connection validation
                Log-Info "Prepare VMSwitch for storage connection validation on all nodes"
                Log-Info " VMSwitch name: $($storageVMSwitchName)"
                Log-Info " Physical adapter to be teamed: $($adapterForStorageVMSwitch)"
                Log-Info " Storage vNIC will be created: $($allStorageAdaptersToCheck)"
                Log-Info " Storage VLANID will be used: $($storageAdapterVlanIdToConfig | Out-String)"
                Log-Info " Need to create management VNIC: $($needMgmtOnStorageVMSwitch)"

                foreach ($testSession in $newTestSessionsBeforeChecking)
                {
                    Log-Info "Trying to prepare storage connection validation VMSwitch/vNIC on $($testSession.ComputerName)"
                    if ($needMgmtOnStorageVMSwitch)
                    {
                        Log-Info " Might lost computer connection momentarily..."
                    }

                    Log-Info " Make sure function ConfigureVMSwitchForTesting and CreateStorageVNIC exist in remote session on the machine..."

                    Invoke-Command -Session $testSession -ScriptBlock {
                        New-Item -Path "Function:\ConfigureVMSwitchForTesting" -Value ${using:function:ConfigureVMSwitchForTesting} -Force | Out-Null
                        New-Item -Path "Function:\CreateStorageVNIC" -Value ${using:function:CreateStorageVNIC} -Force | Out-Null
                    }

                    $invokeCommandCreateStorageVMSwitchParam = @{
                        Session = $testSession
                        ScriptBlock = ${function:ConfigureStorageVMSwitchVNICForTesting}
                        ArgumentList = @($adapterForStorageVMSwitch, $storageVMSwitchName, $allStorageAdaptersToCheck, $storageAdapterVlanIdToConfig, $needMgmtOnStorageVMSwitch)
                    }

                    Log-Info " Calling ConfigureStorageVMSwitchVNICForTesting to create VMSwitch/storage vNIC on the machine..."
                    $vmSwitchCreationInfo = Invoke-Command @invokeCommandCreateStorageVMSwitchParam

                    Log-Info " Done with machine $($testSession.ComputerName)!"
                }
            }
        }
        else
        {
            Log-Info "No need to create VMSwitch for storage connection validation. Will need to set the VLANID on pNIC on all nodes for the validation."

            foreach ($testSession in $newTestSessionsBeforeChecking)
            {
                Log-Info "Trying to set storage adapter VLANID on $($testSession.ComputerName)"

                Invoke-Command -Session $testSession -ArgumentList $storageAdapterVlanIdToConfig -ScriptBlock {
                    param ([System.Collections.Hashtable] $StorageAdapterVLANIDInfo)
                    foreach ($adapterName in $StorageAdapterVLANIDInfo.Keys)
                    {
                        $vlanId = $StorageAdapterVLANIDInfo[$adapterName]
                        if ($vlanId -ne 0)
                        {
                            $adapter = Get-NetAdapter -Name $adapterName -ErrorAction SilentlyContinue
                            if ($adapter)
                            {
                                Set-NetAdapterAdvancedProperty -Name $adapterName -RegistryKeyword "VLANID" -RegistryValue $vlanId
                            }
                        }
                    }
                }

                Log-Info "Done with $($testSession.ComputerName)!"
            }
        }

        # Need to make sure we have the pre-req that needed for the storage connection validation
        # Feature Failover-Clustering is installed, or storage adapter has IPv4 APIPA address
        # If converged intent with storage, then hyper-v feature should be there in the system.

        $useTestCluster = $false
        $usePingMeshOnIPv4 = $true
        $hostIPv4AddressTable = @{}

        $checkHostClusterFeatureScript = {
            $clusterFeatureInstalled = Get-WindowsFeature -Name Failover-Clustering -ErrorAction SilentlyContinue
            $clusterPowerShellToolFeatureInstalled = Get-WindowsFeature -Name RSAT-Clustering-PowerShell -ErrorAction SilentlyContinue
            if ($clusterFeatureInstalled.Installed -and $clusterPowerShellToolFeatureInstalled.Installed) { return $true } else { return $false }
        }

        $checkHostIPv4AddressScript = {
            [CmdletBinding()]
            param ([String[]] $StorageAdaptersToCheck)

            $retVal = New-Object PSObject -Property @{
                Pass = $true
                AdapterIPv4Mapping = @{}
            }

            foreach ($storageAdapter in $StorageAdaptersToCheck)
            {
                [System.Boolean] $currentAdapterIPReady = $false
                $ipStopWatch = [System.diagnostics.stopwatch]::StartNew()
                while (-not $currentAdapterIPReady -and ($ipStopWatch.Elapsed.TotalSeconds -lt 60))
                {
                    # Using Get-NetIPConfiguration here as Get-NetIPAddress seems has issue while executed for local machine in Invoke-Command
                    [PSObject[]] $adapterIpv4Addresses = (Get-NetIPConfiguration -InterfaceAlias $storageAdapter).IPV4Address `
                                        | Where-Object { $_.AddressState -eq "Preferred" -and $_.PrefixOrigin -eq "WellKnown" -and $_.IPAddress -like "169.254.*" }

                    if ($adapterIpv4Addresses.Count -gt 0)
                    {
                        $currentAdapterIPReady = $true
                        break
                    }
                    else
                    {
                        Start-Sleep -Seconds 3
                    }
                }

                if ($adapterIpv4Addresses.Count -gt 0)
                {
                    $retVal.AdapterIPv4Mapping.Add($storageAdapter, $adapterIpv4Addresses[0].IPAddress)
                }
                else
                {
                    $retVal.Pass = $false
                    break
                }
            }

            return $retVal
        }

        [System.Management.Automation.Runspaces.PSSession[]] $newTestSessions = @()

        foreach ($testSession in $newTestSessionsBeforeChecking)
        {
            #region Make sure the test session is opened
            Log-Info "Make sure test session is opened for $($testSession.ComputerName)"
            if ($testSession.State -ne 'Opened')
            {
                Log-Info "Previous PSSession is closed. Opening new session for testing"
                if ($SessionCredential)
                {
                    $sessionToCheck = New-PsSessionWithRetriesInternal -Node $testSession.ComputerName -Credential $SessionCredential
                }
                else
                {
                    throw "Session is not opened and no credential provided"
                }
            }
            else
            {
                $sessionToCheck = $testSession
            }
            #endregion

            $newTestSessions += $sessionToCheck

            Log-Info "Check if we could use test cluster to validate storage connection on $($testSession.ComputerName)"
            $clusterFeatureInstalledOnCurrentNode = Invoke-Command -Session $sessionToCheck -ScriptBlock $checkHostClusterFeatureScript

            $useTestCluster = $false
            Log-Info "Will check if IPv4 APIPA address could be used for storage connection validation."
            $storageAdapterIPv4ForCurrentNode = Invoke-Command -Session $sessionToCheck -ScriptBlock $checkHostIPv4AddressScript -ArgumentList @(, $allStorageAdaptersToCheck)
            if (-not $storageAdapterIPv4ForCurrentNode.Pass)
            {
                Log-Info "Cannot find IPv4 APIPA address on storage adapter on $($testSession.ComputerName). Will not run the validation."
                $usePingMeshOnIPv4 = $false
            }
            else
            {
                Log-Info "IPv4 APIPA address found on storage adapter on $($testSession.ComputerName)."
                $hostIPv4AddressTable.Add($testSession.ComputerName, $storageAdapterIPv4ForCurrentNode.AdapterIPv4Mapping)
            }
        }

        # Prepare node name for running of Test-Cluster
        # Test-Cluster cannot use IP address, as it might return "Object reference not set to an instance of an object" error
        # So have to use node name to run it
        [String[]] $allNodeNames = @()
        foreach ($tmpSession in $newTestSessions)
        {
            $allNodeNames += Invoke-Command -Session $tmpSession -ScriptBlock { $env:COMPUTERNAME }
        }

        Log-Info "Got all node names: $($allNodeNames | Out-String)"

        if ($useTestCluster)
        {
            $validationFunctionParams = @{
                AllNodeNames = $allNodeNames
            }

            $storageAdapterConnectionResults = ValidateStorageConnections @validationFunctionParams
        }
        elseif ($usePingMeshOnIPv4)
        {
            $validationFunctionParams = @{
                AllNodeSessions = $newTestSessions
                HostIPv4Table = $hostIPv4AddressTable
            }

            $storageAdapterConnectionResults = ValidateStorageConnections @validationFunctionParams
        }
        else
        {
            $tmpMessage = "No valid method available to test storage connection. Skip the validation."
            Log-Info $tmpMessage -Type 'WARNING'

            $storageAdapterConnectionRstObject = @{
                Name               = "AzStackHci_Network_Test_StorageConnections_NO_VALID_METHOD"
                Title              = 'Cannot find valid validation method to test storage adapter connections'
                DisplayName        = 'Cannot find valid validation method to test storage adapter connections'
                Severity           = 'WARNING'
                Description        = 'Cannot find valid validation method to test storage adapter connections'
                Tags               = @{}
                Remediation        = 'To validate storage connections, make sure your cluster feature is installed on all nodes, or IPv4 APIPA address is available on the storage adapters.'
                TargetResourceID   = 'CannotValidateStorageConnections'
                TargetResourceName = 'CannotValidateStorageConnections'
                TargetResourceType = 'CannotValidateStorageConnections'
                Timestamp          = [datetime]::UtcNow
                Status             = 'FAILURE'
                AdditionalData     = @{
                    Source    = ''
                    Resource  = 'CannotValidateStorageConnections'
                    Detail    = $tmpMessage
                    Status    = 'FAILURE'
                    TimeStamp = [datetime]::UtcNow
                }
                HealthCheckSource  = $ENV:EnvChkrId
            }

            $storageAdapterConnectionResults += New-AzStackHciResultObject @storageAdapterConnectionRstObject
        }

        return $storageAdapterConnectionResults
    }
    catch
    {
        throw $_
    }
    finally
    {
        # Need to clean the storage vNIC
        Log-Info "Done with validation. Now try to clean up test artifacts created during validation"
        Log-Info "Try to clean up storage vNIC and/or storage pNIC VLANID"
        foreach ($testSession in $newTestSessions)
        {
            Log-Info "Working to remove storage vNIC and or storage pNIC VLANID on $($testSession.ComputerName)"
            Invoke-Command -Session $testSession -ScriptBlock {
                foreach ($adapterName in $using:allStorageAdaptersToCheck)
                {
                    Remove-VMNetworkAdapter -ManagementOS -Name $adapterName -ErrorAction SilentlyContinue
                }

                if (-not $using:needStorageVMSwitch)
                {
                    foreach ($adapterName in $using:allStorageAdaptersToCheck)
                    {
                        Set-NetAdapterAdvancedProperty -Name $adapterName -RegistryKeyword "VLANID" -RegistryValue 0 -ErrorAction SilentlyContinue
                    }
                }

                # Device Management Service might need to be restarted to refresh the nic details
                # It also might not be there
                if (Get-Service -Name DeviceManagementService -ErrorAction SilentlyContinue)
                {
                    Restart-Service -Name DeviceManagementService -Force -ErrorAction SilentlyContinue
                    Start-Sleep -Seconds 20
                }
            }

            Log-Info " Done with $($testSession.ComputerName)!"
        }

        if ($needCleanUpStorageVMSwitch)
        {
            Log-Info "Need to clean up VMSwitch created during the validation"

            $mgmtVlanIdToRestore = $vmSwitchCreationInfo.MgmtVlanId

            foreach ($testSession in $newTestSessions)
            {
                Log-Info "Working to remove VMSwitch on $($testSession.ComputerName)..."

                if ($needMgmtOnStorageVMSwitch)
                {
                    Log-Info " Might lost computer connection momentarily..."

                    if ( $mgmtVlanIdToRestore -ne 0)
                    {
                        Log-Info " Need to restore management VLANID to: $mgmtVlanIdToRestore"
                    }
                }

                Invoke-Command -Session $testSession -ScriptBlock {
                    Remove-VMSwitch -Name $using:storageVMSwitchName -Force

                    if ($using:needMgmtOnStorageVMSwitch)
                    {
                        # Need to restore the mgmt VLANID on the mgmt adapter
                        if ($using:mgmtVlanIdToRestore -ne 0)
                        {
                            Set-NetAdapterAdvancedProperty -Name $using:adapterForStorageVMSwitch -RegistryKeyword "VlanID" -RegistryValue $using:mgmtVlanIdToRestore
                        }

                        # In case of DHCP scenario, after VMSwitch removed, the pNIC might not get the IP address immediately
                        # Wait for some time (60 seconds) to make sure the new IP is settled correctly.
                        [System.Boolean] $currentIPReady = $false
                        $ipStopWatch = [System.diagnostics.stopwatch]::StartNew()
                        while (-not $currentIPReady -and ($ipStopWatch.Elapsed.TotalSeconds -lt 60))
                        {
                            # If the pNIC has Manual or Dhcp IPv4 address with "Preferred" state, we consider it as "ready"
                            $ipConfig = Get-NetIPAddress -InterfaceAlias $using:adapterForStorageVMSwitch | Where-Object { ($_.PrefixOrigin -eq "Manual" -or $_.PrefixOrigin -eq "Dhcp") -and $_.AddressFamily -eq "IPv4" -and $_.AddressState -eq "Preferred" }

                            if ($ipConfig)
                            {
                                $currentIPReady = $true
                                break
                            }
                            else
                            {
                                Start-Sleep -Seconds 3
                            }
                        }

                        if (-not $currentIPReady)
                        {
                            # should not get into here, but keep it here for safety
                            $ipInfoAll = Get-NetIPAddress -AddressFamily IPv4 | Format-Table IPAddress, InterfaceAlias, PrefixLength, PrefixOrigin -AutoSize
                            Write-Host "$($ipInfoAll | Out-String)"
                            Write-Host "Cannot get the IP address back to the pNIC after VMSwitch removed. Please check the system manually."
                            throw "Cannot get the IP address back to the pNIC after VMSwitch removed. Please check the system manually."
                        }
                        else
                        {
                            Write-Host "IP address back to the pNIC after VMSwitch removed. System is ready for connection."
                        }
                    }

                    # Same need to restart Device Management Service to refresh the nic details here
                    if (Get-Service -Name DeviceManagementService -ErrorAction SilentlyContinue)
                    {
                        Restart-Service -Name DeviceManagementService -Force -ErrorAction SilentlyContinue
                        Start-Sleep -Seconds 20
                    }
                }
                Log-Info " Done with machine $($testSession.ComputerName)!"
            }
        }
        else
        {
            Log-Info "No VMSwitch created during the storage connection validation. No need to clean up."
        }

        Log-Info "End of Test-StorageConnection function"
    }
}

function Test-MgmtIpRange
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false, HelpMessage = "Specify starting Management IP Range")]
        [System.Collections.ArrayList]
        $IpPools,

        [Parameter(Mandatory = $false, HelpMessage = "Specify Management Subnet")]
        [string] $ManagementSubnetValue,

        [int[]]
        $port = @(5986, 5985, 22),

        [int]
        $Timeout = 1000,

        [int]
        $Minimum = 6,

        [int]
        $Maximum = 255,

        [PSObject[]] $AtcHostIntents,

        [System.Boolean] $ProxyEnabled = $false
    )
    try
    {
        $instanceResults = @()

        # Check no repeating ips in pool and all in management subnet
        $TestMgmtIpPools = TestMgmtIpPools -IpPools $IpPools -ManagementSubnetValue $ManagementSubnetValue
        $Status = if ($TestMgmtIpPools) { 'SUCCESS' } else { 'FAILURE' }
        $params = @{
            Name               = "AzStackHci_Network_Test_IP_Pools_Subnet_No_Duplicates"
            Title              = 'Test IP Pools in Management Subnet and No duplicate IPs in IpPools'
            DisplayName        = "Test IP Pools $ManagementSubnetValue"
            Severity           = 'CRITICAL'
            Description        = 'Checking start and end address are on the same subnet'
            Tags               = @{}
            Remediation        = 'https://aka.ms/hci-envch'
            TargetResourceID   = "IpPool-$ManagementSubnetValue"
            TargetResourceName = "ManagementIPRange"
            TargetResourceType = 'Network Range'
            Timestamp          = [datetime]::UtcNow
            Status             = $Status
            AdditionalData     = @{
                Source    = 'CustomerNetwork'
                Resource  = 'CustomerSubnet'
                Detail    = if ($TestMgmtIpPools) { $lnTxt.TestIpPoolPass -f $ManagementSubnetValue } else { $lnTxt.TestIpPoolFail -f $ManagementSubnetValue }
                Status    = $status
                TimeStamp = [datetime]::UtcNow
            }
            HealthCheckSource  = $ENV:EnvChkrId
        }
        $instanceResults += New-AzStackHciResultObject @params

        foreach ($ipPool in $IpPools)
        {
            $StartingAddress = $ipPool.StartingAddress
            $EndingAddress = $ipPool.EndingAddress

            # Check same subnet
            $TestMgmtSubnet = TestMgmtSubnet -StartingAddress $StartingAddress -EndingAddress $EndingAddress
            $Status = if ($TestMgmtSubnet) { 'SUCCESS' } else { 'FAILURE' }
            $params = @{
                Name               = 'AzStackHci_Network_Test_Management_IP_Range_Subnet'
                Title              = 'Test Management IP Subnet'
                DisplayName        = "Test Management IP Subnet $StartingAddress - $EndingAddress"
                Severity           = 'CRITICAL'
                Description        = 'Checking start and end address are on the same subnet'
                Tags               = @{}
                Remediation        = 'https://aka.ms/hci-envch'
                TargetResourceID   = "$StartingAddress-$EndingAddress"
                TargetResourceName = "ManagementIPRange"
                TargetResourceType = 'Network Range'
                Timestamp          = [datetime]::UtcNow
                Status             = $Status
                AdditionalData     = @{
                    Source    = 'CustomerNetwork'
                    Resource  = 'CustomerSubnet'
                    Detail    = if ($TestMgmtSubnet) { $lnTxt.TestMgmtSubnetPass -f $StartingAddress, $EndingAddress } else { $lnTxt.TestMgmtSubnetFail -f $StartingAddress, $EndingAddress }
                    Status    = $status
                    TimeStamp = [datetime]::UtcNow
                }
                HealthCheckSource  = $ENV:EnvChkrId
            }
            $instanceResults += New-AzStackHciResultObject @params

            # Get IP in Range
            $MgmtIpRange = GetMgmtIpRange -StartingAddress $StartingAddress -EndingAddress $EndingAddress
            foreach ($Ip in $MgmtIpRange)
            {
                $result = @{}
                $result += @{
                    'Ping' = Test-NetConnection -ComputerName $Ip -InformationLevel Quiet -WarningAction SilentlyContinue
                }
                foreach ($p in $port)
                {
                    $result += @{
                        $p = IsTcpPortInUse -Ip $ip -Port $p -Timeout $Timeout
                    }
                }
                $Status = if ($true -notin $result.Values) { 'SUCCESS' } else { 'FAILURE' }
                $msg = $lnTxt.ActiveHostCheck -f $ip, (($result.Keys | ForEach-Object { "{0}:{1}" -f $psitem,$result[$psitem] }) -join ', ')
                $Type = if ($result.Values -contains $true) { 'WARNING' } else { 'INFORMATIONAL' }
                Log-Info $msg -Type $Type

                $params = @{
                    Name               = 'AzStackHci_Network_Test_Management_IP_No_Active_Hosts'
                    Title              = 'Test Management IP Range for Active Hosts'
                    DisplayName        = "Test Management IP Range $Ip for Active Hosts"
                    Severity           = 'CRITICAL'
                    Description        = 'Checking no hosts respond on Management IP range'
                    Tags               = @{}
                    Remediation        = 'https://aka.ms/hci-envch'
                    TargetResourceID   = $Ip
                    TargetResourceName = "ManagementIPRange"
                    TargetResourceType = 'Network Range'
                    Timestamp          = [datetime]::UtcNow
                    Status             = $Status
                    AdditionalData     = @{
                        Source    = $Ip
                        Resource  = 'ICMP/SSH/WINRM'
                        Detail    = ($result.Keys | ForEach-Object { "{0}:{1}" -f $psitem,$result[$psitem] }) -join ', '
                        Status    = $Status
                        TimeStamp = [datetime]::UtcNow
                    }
                    HealthCheckSource  = $ENV:EnvChkrId
                }
                $instanceResults += New-AzStackHciResultObject @params
            }
        }

        # Check range size
        $TestMgmtRangeSize = TestMgmtRangeSize -IpPools $IpPools -Minimum $Minimum -Maximum $Maximum
        $status = if ($TestMgmtRangeSize) { 'SUCCESS' } else { 'FAILURE' }
        $allIps = GetMgmtIpRangeFromPools -IpPools $IpPools
        $ipCount = $allIps.Count
        $params = @{
            Name               = 'AzStackHci_Network_Test_Management_IP_Range_Size'
            Title              = 'Test Management IP Range Size'
            DisplayName        = "Test Management IP Range Size of all the pools. $ipCount ips found."
            Severity           = 'CRITICAL'
            Description        = "Checking management IP range size is between $minimum-$maximum"
            Tags               = @{}
            Remediation        = 'https://aka.ms/hci-envch'
            TargetResourceID   = "Size:$ipCount "
            TargetResourceName = "ManagementIPRange"
            TargetResourceType = 'Network Range'
            Timestamp          = [datetime]::UtcNow
            Status             = $Status
            AdditionalData     = @{
                Source    = 'CustomerNetwork'
                Resource  = 'CustomerRange'
                Detail    = if ($TestMgmtRangeSize) { $lnTxt.TestMgmtRangeSizePass -f $Minimum, $Maximum } else { $lnTxt.TestMgmtRangeSizeFail -f $Minimum, $Maximum }
                Status    = if ($TestMgmtRangeSize) { 'SUCCESS' } else { 'FAILURE' }
                TimeStamp = [datetime]::UtcNow
            }
            HealthCheckSource  = $ENV:EnvChkrId
        }
        $instanceResults += New-AzStackHciResultObject @params

        # Check pool sizes
        $TestMgmtRangePoolCount = TestMgmtRangePoolCount -IpPools $IpPools -Minimum $Minimum
        $poolCount = $IpPools.Count
        $status = if ($TestMgmtRangePoolCount) { 'SUCCESS' } else { 'FAILURE' }
        $params = @{
            Name               = 'AzStackHci_Network_Test_Management_IP_Range_Pool_Count'
            Title              = 'Test Management IP Pool Count'
            DisplayName        = "Test Management IP Range Number of IP Pools."
            Severity           = 'CRITICAL'
            Description        = "Checking management IP pools has one or two pools. First pool must only have 1 ip if 2 pools"
            Tags               = @{}
            Remediation        = 'https://aka.ms/hci-envch'
            TargetResourceID   = "Count:$poolCount "
            TargetResourceName = "ManagementIPRange"
            TargetResourceType = 'Network Range'
            Timestamp          = [datetime]::UtcNow
            Status             = $Status
            AdditionalData     = @{
                Source    = 'CustomerNetwork'
                Resource  = 'CustomerRange'
                Detail    = if ($TestMgmtRangePoolCount) { $lnTxt.TestMgmtRangePoolCountPass -f $poolCount} else { $lnTxt.TestMgmtRangePoolCountFail -f $poolCount, ($Minimum - 1) }
                Status    = if ($TestMgmtRangePoolCount) { 'SUCCESS' } else { 'FAILURE' }
                TimeStamp = [datetime]::UtcNow
            }
            HealthCheckSource  = $ENV:EnvChkrId
        }
        $instanceResults += New-AzStackHciResultObject @params

        #region check infra IP conneciton
        # A new vSwitch and management vNIC is created using Network ATC naming standards. The vSwitch is created using the intents configuration provided by the customer in the deployment json.
        # We will rotate the vNIC IP with the infra IPs to be tested.
        # Whatever is the Mgmt intent, we will create the vSwitch using the selected pNICs from the customer.
        # curl tool seems to provide the solution to test from specific source IP and allows to check TCP ports and also URLs
        # Only the first 9 IPs from the infra range will be tested.
        # DNS registration must be avoided on the IPs tested from the vNIC

        $infraIPRangeToValidate = GetMgmtIpRangeFromPools -IpPools $IpPools

        if ($ProxyEnabled)
        {
            Log-Info "Proxy is enabled on the host. Will check public endpoint connection via proxy."
        }
        else
        {
            Log-Info "Proxy is not enabled on the host. Will check public endpoint connetion directly."
        }

        if ((Get-Command Get-VMSwitch -ErrorAction SilentlyContinue) -and (Get-WindowsFeature -Name Hyper-V -ErrorAction SilentlyContinue).Installed)
        {
            [PSObject[]] $mgmtIntent = $AtcHostIntents | Where-Object { $_.TrafficType.Contains("Management") }
            [System.String] $mgmtIntentName = $mgmtIntent[0].Name
            [System.String[]] $mgmtAdapters = GetSortedMgmtIntentAdapter -MgmtAdapterNames $mgmtIntent[0].Adapter

            [System.Guid[]] $intentAdapterGuids = (Get-NetAdapter -Name $mgmtAdapters -ErrorAction SilentlyContinue).InterfaceGuid

            try
            {
                $needCleanUpVMSwitch = $false
                $mgmtVlanIdToRestore = 0

                #region prepare VMSwitch for testing infra IP connection
                [PSObject[]] $allExistingVMSwitches = Get-VMSwitch -SwitchType External
                $externalVMSwitchsCount = $allExistingVMSwitches.Count

                [PSObject] $foundVMSwitchToUse = $null

                if ($externalVMSwitchsCount -eq 0)
                {
                    # if we found 0 VMSwitch, we will need to create one for this testing
                    # Note that this operation will have the host disconnected from network for a moment (due to VMSwitch/vNIC creation)
                    # Since the code is executed locally, this disconnection should not affect the execution
                    Log-Info "No VMSwitch exists in system. Will create VMSwitch for testing infra IP connection."
                    $tmpVMSwitchConfigInfo = ConfigureVMSwitchForTesting -SwitchAdapterNames $mgmtAdapters -MgmtIntentName $mgmtIntentName
                    $foundVMSwitchToUse = $tmpVMSwitchConfigInfo.VMSwitchInfo
                    $needCleanUpVMSwitch = $tmpVMSwitchConfigInfo.NeedCleanUp
                    $mgmtVlanIdToRestore = $tmpVMSwitchConfigInfo.MgmtVlanId

                    if (-not $tmpVMSwitchConfigInfo.IPReady)
                    {
                        Log-Info "Cannot get a VMSwitch ready on $($env:COMPUTERNAME) with valid IP on the vNIC created. Fail the validation"
                        throw "Cannot get a VMSwitch ready on $($env:COMPUTERNAME) with valid IP on the vNIC created. Fail the validation"
                    }
                }
                else
                {
                    # if we found at least 1 VMSwitch in the system, we then need to check
                    # If there is one VMSwitch that has the same mgmt intent adapters
                    Log-info "Found $($externalVMSwitchsCount) VMSwitch in the system. Need to check if a valid one could be used for validation."

                    foreach ($externalVMSwitch in $allExistingVMSwitches)
                    {
                        # Need to check the switch is good for deployment: using same adapter as the intent
                        [System.Guid[]] $switchAdapterGuids = $externalVMSwitch.NetAdapterInterfaceGuid

                        if (Compare-Object -ReferenceObject $switchAdapterGuids -DifferenceObject $intentAdapterGuids)
                        {
                            # Adapters used in pre-defined VMSwitch and the intent are different. Ignore that VMSwitch
                            Log-Info "Found $($externalVMSwitch.Name) with different adapters than the mgmt intent. Skip it."
                        }
                        else
                        {
                            # if the system already have a VMSwitch with the same mgmt adpaters in its teaming, we will just use that adpater
                            $foundVMSwitchToUse = $externalVMSwitch
                            break
                        }
                    }

                    if (-not $foundVMSwitchToUse)
                    {
                        Log-info "No valid VMSwitch found! Check if we could create a new VMSwitch for validaton."

                        # At this moment, we need further checking:
                        # If all adapters in the mgmt intent is not used by any adapter, we will need to create a new VMSwitch
                        # If any of the adapter in the mgmt intent is used by any adapter, we will need to error out as this is not a supported scenario
                        [System.Guid[]] $allSwitchAdapterGuids = $allExistingVMSwitches.NetAdapterInterfaceGuid

                        [System.Boolean] $intentAdapterAlreadyUsed = $false
                        foreach ($tmpAdapterGuid in $intentAdapterGuids)
                        {
                            if ($allSwitchAdapterGuids.Contains($tmpAdapterGuid))
                            {
                                $intentAdapterAlreadyUsed = $true
                                break
                            }
                        }

                        if (-not $intentAdapterAlreadyUsed)
                        {
                            # if none of the adapter in the mgmt intent is used by any adapter, we will create a new VMSwitch
                            Log-Info "VMSwitch found, but no VMSwitch mgmt adapters in the system. Will create VMSwitch for testing infra IP connection."
                            $tmpVMSwitchConfigInfo = ConfigureVMSwitchForTesting -SwitchAdapterNames $mgmtAdapters -MgmtIntentName $mgmtIntentName
                            $foundVMSwitchToUse = $tmpVMSwitchConfigInfo.VMSwitchInfo
                            $needCleanUpVMSwitch = $tmpVMSwitchConfigInfo.NeedCleanUp
                            $mgmtVlanIdToRestore = $tmpVMSwitchConfigInfo.MgmtVlanId

                            if (-not $tmpVMSwitchConfigInfo.IPReady)
                            {
                                Log-Info "Cannot get a VMSwitch ready on $($env:COMPUTERNAME) with valid IP on the vNIC created. Fail the validation"
                                throw "Cannot get a VMSwitch ready on $($env:COMPUTERNAME) with valid IP on the vNIC created. Fail the validation"
                            }
                        }
                        else
                        {
                            # This is an error situation: some of the mgmt intent adapters is already used by an existing VMSwitch, some other mgmt
                            # intent adapter is still "free" in the system. We don't know what to do with this, so need to error out
                            Log-Info "VMSwitch found, mgmt adapter list is not matching to any VMSwitch adapter list. Wrong configuration. Will to fail the validation"
                        }
                    }
                }
                #endregion

                $allPublicEndpointServicesToCheck = Get-AzstackHciConnectivityTarget | Where-Object { $_.Name -Like "Azure_Kubernetes_Service_*" -or $_.Name -Like "AzStackHci_MOCStack_*" -or $_.Name -Like "Vm_Management_HCI_*" }

                if ($foundVMSwitchToUse)
                {
                    Log-Info "Got VMSwitch $($foundVMSwitchToUse.Name) to use for validating infra IP connection. Start the validation..."

                    $mgmtAlias = "vManagement($($MgmtIntentName))"
                    [PSOBject[]] $mgmtAdapterIP = Get-NetIPAddress -InterfaceAlias $mgmtAlias -ErrorAction SilentlyContinue | Where-Object { ($_.PrefixOrigin -eq "Manual" -or $_.PrefixOrigin -eq "Dhcp") -and $_.AddressFamily -eq "IPv4" -and $_.AddressState -eq "Preferred" }

                    if ($mgmtAdapterIP -and $mgmtAdapterIP.Count -gt 0)
                    {
                        $prefixLength = $mgmtAdapterIP[0].PrefixLength[0]
                        $mgmtIPConfig = Get-NetIPConfiguration -InterfaceAlias $mgmtAlias
                        $defaultGateway = $mgmtIPconfig.IPv4DefaultGateway[0].NextHop

                        # Try to get DNS server IP from running system
                        [PSObject[]] $getDNSServers = Get-DnsClientServerAddress -InterfaceAlias $mgmtAlias -AddressFamily IPv4 -ErrorAction SilentlyContinue

                        if ($getDNSServers -and ($getDNSServers.Count -gt 0) -and ($getDNSServers[0].ServerAddresses.Count -gt 0))
                        {
                            # Get up to 3 DNS servers to check
                            [System.String[]] $dnsServersIPToCheck = $getDNSServers[0].ServerAddresses[0..2]

                            try
                            {
                                #region configure vNIC for testing infra IP connection
                                $tmpGuid = [System.Guid]::NewGuid()
                                $newVNICName = "TestvNIC$($tmpGuid)"

                                Log-Info "Prepare vNIC $($newVNICName) on the VMSwitch to use for infra IP connection validation"

                                if (Get-VMNetworkAdapter -All | where-Object { $_.Name -eq $newVNICName })
                                {
                                    Remove-VMNetworkAdapter -ManagementOS -SwitchName $foundVMSwitchToUse.Name -Name $newVNICName -Confirm:$false
                                }

                                Add-VMNetworkAdapter -ManagementOS -SwitchName $foundVMSwitchToUse.Name -Name $newVNICName
                                Get-NetAdapter -name "vEthernet ($($newVNICName))" -ErrorAction SilentlyContinue | Rename-NetAdapter -NewName $newVNICName
                                Set-DnsClient -InterfaceAlias $newVNICName -RegisterThisConnectionsAddress $false

                                # There is a possibility that the mgmt VMNetworkAdapter is not there
                                # (NOTE: we have the validation in Test-AdapterDriverMgmtAdapterReadiness, however if the result failed there, the current test will still run.
                                # So there is a chance that below call will throw exception out if we do not specify -ErrorAction SilentlyContinue)
                                $tempVMNetworkAdapterIsolation = Get-VMNetworkAdapterIsolation -ManagementOS -VMNetworkAdapterName $mgmtAlias -ErrorAction SilentlyContinue
                                if ($tempVMNetworkAdapterIsolation -and $tempVMNetworkAdapterIsolation.DefaultIsolationID -ne 0)
                                {
                                    Set-VMNetworkAdapterIsolation -ManagementOS `
                                                            -VMNetworkAdapterName $newVNICName `
                                                            -IsolationMode Vlan `
                                                            -AllowUntaggedTraffic $true `
                                                            -DefaultIsolationID $tempVMNetworkAdapterIsolation.DefaultIsolationID
                                }

                                Set-NetIPInterface -InterfaceAlias $newVNICName -Dhcp Disabled

                                # Need to wait until the DHCP is disabled on the adapter. Otherwise, following call might fail
                                [System.Boolean] $vNicReady = $false
                                $stopWatch = [System.diagnostics.stopwatch]::StartNew()
                                while (-not $vNicReady -and ($stopWatch.Elapsed.TotalSeconds -lt 30))
                                {
                                    if ((Get-VMNetworkAdapter -ManagementOS -Name $newVNICName -ErrorAction SilentlyContinue) -and ((Get-NetIPInterface -InterfaceAlias $newVNICName -AddressFamily IPv4).Dhcp -eq "Disabled"))
                                    {
                                        $vNicReady = $true
                                        break
                                    }
                                    else
                                    {
                                        Start-Sleep -Seconds 3
                                    }
                                }
                                #endregion

                                if ($vNICReady)
                                {
                                    Log-Info "VMNetworkAdapter [ $($newVNICName) ] ready and DHCP is disabled on the adapter."

                                    $retryTimes = 10
                                    Log-Info "Will check connection from Infra IP to DNS server(s) port 53 and public endpoints for max of $($retryTimes) times"
                                    Log-Info "DNS Server to check: $($dnsServersIPToCheck | Out-String)"

                                    ###################################
                                    # Start testing infra IP connection
                                    ###################################
                                    # Magic number: we will test only first 9 IPs from the infra range as:
                                    # 6 are the one we requested right now for services running in HCI cluster
                                    # 3 are the additional that might be used in the future (for example, SLB VM, etc.)
                                    # We don't want to test all the infra IP as it will requires a lot of time to finish the validation
                                    $ipNumberToCheck = 9

                                    if ($infraIPRangeToValidate.Count -lt $ipNumberToCheck)
                                    {
                                        $ipNumberToCheck = $infraIPRangeToValidate.Count
                                    }

                                    for ($i=0; $i -lt $ipNumberToCheck; $i++)
                                    {
                                        $endpointIndex = 1

                                        $ipToCheck = $infraIPRangeToValidate[$i]
                                        Log-Info "`nCheck IP $($i+1) / $($ipNumberToCheck): [ $($ipToCheck) ]"

                                        #region Set new IP on the adapter
                                        # Make sure no IP on the adapter
                                        $oldIpAddresses = Get-NetIPAddress -InterfaceAlias $newVNICName -ErrorAction SilentlyContinue

                                        foreach ($ip in $oldIpAddresses)
                                        {
                                            Remove-NetIPAddress -InterfaceAlias $newVNICName -IPAddress $ip.IPAddress -Confirm:$false -ErrorAction SilentlyContinue
                                        }

                                        if (Get-NetRoute -InterfaceAlias $newVNICName -DestinationPrefix 0.0.0.0/0 -ErrorAction SilentlyContinue)
                                        {
                                            New-NetIPAddress -InterfaceAlias $newVNICName -IPAddress $ipToCheck -PrefixLength $prefixLength -SkipAsSource $true | Out-Null
                                        }
                                        else
                                        {
                                            New-NetIPAddress -InterfaceAlias $newVNICName -IPAddress $ipToCheck -PrefixLength $prefixLength -DefaultGateway $defaultGateway -SkipAsSource $true | Out-Null
                                        }

                                        # Wait for the new IP to be ready
                                        [System.Boolean] $currentIPReady = $false
                                        $ipStopWatch = [System.diagnostics.stopwatch]::StartNew()
                                        while (-not $currentIPReady -and ($ipStopWatch.Elapsed.TotalSeconds -lt 60))
                                        {
                                            $ipConfig = Get-NetIPAddress -InterfaceAlias $newVNICName | Where-Object { $_.IPAddress -eq $ipToCheck -and $_.PrefixOrigin -eq "Manual" -and $_.AddressFamily -eq "IPv4" -and $_.AddressState -eq "Preferred" }

                                            if ($ipConfig)
                                            {
                                                $currentIPReady = $true
                                                break
                                            }
                                            else
                                            {
                                                Start-Sleep -Seconds 3
                                            }
                                        }
                                        #endregion

                                        #region Check connection from infra IP to DNS server
                                        # Note that we cannot use Resolve-DnsName or nslookup here directly, as those call cannot specify the source IP
                                        foreach ($currentDNSServerToCheck in $dnsServersIPToCheck)
                                        {
                                            Log-Info " >> Trying DNS connection to $($currentDNSServerToCheck) port 53."

                                            [System.Boolean] $isDnsConnected = $false
                                            $retry = 1
                                            while ((-not $isDnsConnected) -and ($retry -le $retryTimes))
                                            {
                                                try
                                                {
                                                    $src  = [System.Net.IPEndPoint]::new([ipaddress]::Parse($ipToCheck),0)
                                                    $tc   = [System.Net.Sockets.TcpClient]::new($src)
                                                    $tc.Connect($currentDNSServerToCheck, 53)

                                                    if ($tc.Connected)
                                                    {
                                                        Log-Info " == DNS connection ESTABLISHED to $($currentDNSServerToCheck) on attempt $($retry)"
                                                        $isDnsConnected = $true
                                                        break
                                                    }
                                                    else
                                                    {
                                                        Log-Info " ?? FAILED DNS connection to $($currentDNSServerToCheck) on attempt $($retry)"
                                                    }
                                                }
                                                catch
                                                {
                                                    Log-Info " ?? FAILED! Got exception while checking DNS connection on attempt ($($retry))!"
                                                }
                                                finally
                                                {
                                                    if ($tc)
                                                    {
                                                        $tc.Dispose()
                                                    }
                                                }

                                                Start-Sleep -Seconds 3
                                                $retry++
                                            }

                                            if ($isDnsConnected)
                                            {
                                                Log-Info " == Found valid DNS connection"
                                                break
                                            }
                                        }
                                        #endregion

                                        $dnsConnectionRstParams = @{
                                            Name               = 'AzStackHci_Network_Test_Infra_IP_Connection_DNS_Server_Port_53'
                                            Title              = 'Test DNS server port connection for all IP in infra IP pool'
                                            DisplayName        = "Test DNS server port connection for all IP in infra IP pool"
                                            Severity           = 'CRITICAL'
                                            Description        = 'Test DNS server port connection for all IP in infra IP pool'
                                            Tags               = @{}
                                            Remediation        = "Make sure infra IP $ipToCheck could connect to your DNS server correctly."
                                            TargetResourceID   = "Infra_IP_Connection_$($ipToCheck)"
                                            TargetResourceName = "Infra_IP_Connection_$($ipToCheck)"
                                            TargetResourceType = "Infra_IP_Connection_$($ipToCheck)"
                                            Timestamp          = [datetime]::UtcNow
                                            Status             = "FAILURE"
                                            AdditionalData     = @{
                                                Source    = $env:COMPUTERNAME
                                                Resource  = $($ipToCheck)
                                                Detail    = "[FAILED] Connection from $ipToCheck to DNS server port 53 failed after 3 attempts. DNS server used: $($dnsServersIPToCheck | Out-String)"
                                                Status    = "FAILURE"
                                                TimeStamp = [datetime]::UtcNow
                                            }
                                            HealthCheckSource  = $ENV:EnvChkrId
                                        }

                                        if ($ProxyEnabled)
                                        {
                                            # In case proxy is enabled, we will downgrade the severity to WARNING as the DNS resolution might happen on proxy server
                                            $dnsConnectionRstParams.Severity = 'WARNING'
                                        }

                                        if ($isDnsConnected)
                                        {
                                            $dnsConnectionRstParams.Status = "SUCCESS"
                                            $dnsConnectionRstParams.AdditionalData.Detail = "[PASSED] Connection from $ipToCheck to DNS server port 53 passed. DNS server used: $($dnsServersIPToCheck | Out-String)"
                                            $dnsConnectionRstParams.AdditionalData.Status = "SUCCESS"

                                            #region Check connection from infra IP to well known endpoints
                                            # Since we rely on DNS naming resolution, we put the checking here in this if statement
                                            foreach ($service in $allPublicEndpointServicesToCheck)
                                            {
                                                foreach ($endpointInService in $service.EndPoint)
                                                {
                                                    # Will try $retryTimes curl connection to the remote endpoint

                                                    # Note that curl.exe honor the system HTTP_PROXY/HTTPS_PROXY settings, so we don't need to specify "--proxy" parameter here
                                                    $endpointToCheck = "$($service.Protocol[0])://$($endpointInService)"
                                                    $curlGetExpression = "curl.exe -sS --connect-timeout 5 -m 6 `"$($endpointToCheck)`" --interface $($ipToCheck)"
                                                    $curlHeaderExpression = "curl.exe -i -sS --connect-timeout 5 -m 6 `"$($endpointToCheck)`" --interface $($ipToCheck)"

                                                    # If Arcgateway is enabled, then we need to ensure the endpoint is reachable via the Arc proxy.
                                                    # Detailed Explanation: The curl command uses the proxy settings from the HTTP_PROXY/HTTPS_PROXY environment variables,
                                                    # which is pointing to Arc proxy on Arc gateway enabled system. Thus, Without the --interface parameter it will just make sure
                                                    # the endpoint is reachable via the Arc proxy.
                                                    $arcConnectionType =  & "$env:ProgramW6432\AzureConnectedMachineAgent\azcmagent.exe" config get connection.type
                                                    if ( $arcConnectionType -eq "gateway")
                                                    {
                                                        # Just check if Arc proxy is able to route this traffic
                                                        $curlGetExpression = "curl.exe -sS --connect-timeout 5 -m 6 `"$($endpointToCheck)`" "
                                                        $curlHeaderExpression = "curl.exe -i -sS --connect-timeout 5 -m 6 `"$($endpointToCheck)`" "
                                                    }

                                                    Log-Info " >> Trying public Endpoint connection $($endpointIndex) to $($endpointToCheck)."

                                                    [System.Boolean] $isPublicEndpointConnected = $false

                                                    $retry = 1
                                                    while ((-not $isPublicEndpointConnected) -and ($retry -le $retryTimes))
                                                    {
                                                        try
                                                        {
                                                            $curlGetContent = Invoke-Expression $curlGetExpression

                                                            if (-not [System.String]::IsNullOrWhiteSpace($curlGetContent))
                                                            {
                                                                Log-Info " == Connection ESTABLISHED with GET on attempt $($retry)"
                                                                $isPublicEndpointConnected = $true
                                                                break
                                                            }
                                                            else
                                                            {
                                                                $curlHeaderContent = Invoke-Expression $curlHeaderExpression

                                                                # Need to analyze the output of $curlHeaderContent to see if the connection is established
                                                                # If proxy enabled, the response will need to contain something in addition to the "HTTP/1.1 200 Connection established"
                                                                if ($ProxyEnabled)
                                                                {
                                                                    $curlHeaderContent = $curlHeaderContent -replace "^HTTP\/\d\.\d 200 Connection established", ""
                                                                }

                                                                if (-not [System.String]::IsNullOrWhiteSpace($curlHeaderContent))
                                                                {
                                                                    Log-Info " == Connection ESTABLISHED with HEADER only on attempt $($retry)"
                                                                    $isPublicEndpointConnected = $true
                                                                    break
                                                                }
                                                                else
                                                                {
                                                                    Log-Info " ?? FAILED connection on attempt $($retry)"
                                                                }
                                                            }
                                                        }
                                                        catch
                                                        {
                                                            Log-Info " ?? FAILED! Got exception while checking public endpoint connection on attempt ($($retry))!"
                                                        }

                                                        Start-Sleep -Seconds 3
                                                        $retry++
                                                    }

                                                    if ([System.String]::IsNullOrEmpty($service.Severity) -or [System.String]::IsNullOrWhiteSpace($service.Severity))
                                                    {
                                                        $currentSeverity = "CRITICAL"
                                                    }
                                                    else
                                                    {
                                                        $currentSeverity = $service.Severity
                                                    }

                                                    $publicEndpointRstParams = @{
                                                        Name               = 'AzStackHci_Network_Test_Infra_IP_Connection_' + $service.Name
                                                        Title              = 'Test outbound connection for IP in infra IP pool'
                                                        DisplayName        = "Test outbound connection for IP in infra IP pool"  + $service.Title
                                                        Severity           = $currentSeverity
                                                        Description        = 'Test connection for IP in infra IP pool ' + $service.Description
                                                        Tags               = @{}
                                                        Remediation        = "Make sure infra IP $ipToCheck could connect to public endpoint $endpointToCheck correctly. `nhttps://learn.microsoft.com/en-us/azure/azure-arc/servers/network-requirements?tabs=azure-cloud#urls"
                                                        TargetResourceID   = $service.TargetResourceID
                                                        TargetResourceName = $service.TargetResourceName
                                                        TargetResourceType = $service.TargetResourceType
                                                        Timestamp          = [datetime]::UtcNow
                                                        Status             = "FAILURE"
                                                        AdditionalData     = @{
                                                            Source    = $env:COMPUTERNAME
                                                            Resource  = $($ipToCheck)
                                                            Detail    = "[FAILED] Connection from $ipToCheck to $endpointToCheck failed after $retryTimes attempts"
                                                            Status    = "FAILURE"
                                                            TimeStamp = [datetime]::UtcNow
                                                        }
                                                        HealthCheckSource  = $ENV:EnvChkrId
                                                    }

                                                    if ($isPublicEndpointConnected)
                                                    {
                                                        $publicEndpointRstParams.Status = "SUCCESS"
                                                        $publicEndpointRstParams.AdditionalData.Detail = "[PASSED] Connection from $ipToCheck to $endpointToCheck passed."
                                                        $publicEndpointRstParams.AdditionalData.Status = "SUCCESS"
                                                    }
                                                    else
                                                    {
                                                        Log-info "Public Endpoint connection failed for infra IP $ipToCheck."
                                                    }

                                                    $instanceResults += New-AzStackHciResultObject @publicEndpointRstParams
                                                    $endpointIndex++
                                                }
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            Log-info "DNS connection failed for infra IP $ipToCheck."
                                        }

                                        $instanceResults += New-AzStackHciResultObject @dnsConnectionRstParams
                                    }
                                }
                                else
                                {
                                    # vNIC creation failure. Normally won't hit this path, but keep it here for safety
                                    Log-Info "Cannot get a vNIC ready on VMSwitch $($foundVMSwitchToUse.Name) in $($env:COMPUTERNAME) for validating infra IP connection. Fail the validation"

                                    $params = @{
                                        Name               = 'AzStackHci_Network_Test_Infra_IP_Connection_vNIC_Readiness'
                                        Title              = 'Test virtual adapter readiness for all IP in infra IP pool'
                                        DisplayName        = "Test virtual adapter readiness for all IP in infra IP pool"
                                        Severity           = 'CRITICAL'
                                        Description        = 'Test virtual adapter readiness for all IP in infra IP pool'
                                        Tags               = @{}
                                        Remediation        = "Make sure Add/Get-VMNetworkAdapter on $($env:COMPUTERNAME) can run correctly."
                                        TargetResourceID   = "Infra_IP_Connection_VNICReadiness"
                                        TargetResourceName = "Infra_IP_Connection_VNICReadiness"
                                        TargetResourceType = "Infra_IP_Connection_VNICReadiness"
                                        Timestamp          = [datetime]::UtcNow
                                        Status             = "FAILURE"
                                        AdditionalData     = @{
                                            Source    = $env:COMPUTERNAME
                                            Resource  = 'VNICReadiness'
                                            Detail    = "[FAILED] Cannot test connection for infra IP. VM network adapter is not configured correctly on host $($env:COMPUTERNAME)."
                                            Status    = "FAILURE"
                                            TimeStamp = [datetime]::UtcNow
                                        }
                                        HealthCheckSource  = $ENV:EnvChkrId
                                    }

                                    $instanceResults += New-AzStackHciResultObject @params
                                }
                            }
                            finally
                            {
                                # Best effort to clean the IP used, as the last IP checked might not be cleaned in the previous checking
                                for ($i=0; $i -lt $ipNumberToCheck; $i++)
                                {
                                    Remove-NetIPAddress -IPAddress $infraIPRangeToValidate[$i] -ErrorAction SilentlyContinue -Confirm:$false
                                }

                                # Clean up the vNIC
                                if (Get-VMNetworkAdapter -ManagementOS -Name $newVNICName -ErrorAction SilentlyContinue)
                                {
                                    Remove-VMNetworkAdapter -ManagementOS -SwitchName $foundVMSwitchToUse.Name -Name $newVNICName -Confirm:$false
                                }
                            }
                        }
                        else
                        {
                            # No DNS client server address found on the adapter
                            Log-Info "Cannot get DNS client server address correctly on $($env:COMPUTERNAME) for validating infra IP connection. Fail the validation"

                            $params = @{
                                Name               = 'AzStackHci_Network_Test_Infra_IP_Connection_DNSClientServerAddress_Readiness'
                                Title              = 'Test DNS client server addresses readiness for all IP in infra IP pool'
                                DisplayName        = "Test DNS client server addresses readiness for all IP in infra IP pool"
                                Severity           = 'CRITICAL'
                                Description        = 'Test DNS client server addresses readiness for all IP in infra IP pool'
                                Tags               = @{}
                                Remediation        = "Set DNS client server address correctly on management adapter [ $($mgmtAlias) ] on $($env:COMPUTERNAME). Check it using Get-DnsClientServerAddress"
                                TargetResourceID   = "Infra_IP_Connection_DNSClientReadiness"
                                TargetResourceName = "Infra_IP_Connection_DNSClientReadiness"
                                TargetResourceType = "Infra_IP_Connection_DNSClientReadiness"
                                Timestamp          = [datetime]::UtcNow
                                Status             = "FAILURE"
                                AdditionalData     = @{
                                    Source    = $env:COMPUTERNAME
                                    Resource  = 'DNSClientReadiness'
                                    Detail    = "[FAILED] Cannot find correctly DNS client server address on host $($env:COMPUTERNAME)."
                                    Status    = "FAILURE"
                                    TimeStamp = [datetime]::UtcNow
                                }
                                HealthCheckSource  = $ENV:EnvChkrId
                            }

                            $instanceResults += New-AzStackHciResultObject @params
                        }
                    }
                    else
                    {
                        Log-Info "Got VMSwitch, but cannot get a valid vNIC to use on $($env:COMPUTERNAME) for validating infra IP connection. Fail the validation"

                        $params = @{
                            Name               = 'AzStackHci_Network_Test_Infra_IP_Connection_MANAGEMENT_VNIC_Readiness'
                            Title              = 'Test VMSwitch/Management VMNetworkAdapter readiness for all IP in infra IP pool'
                            DisplayName        = 'Test VMSwitch/Management VMNetworkAdapter readiness for all IP in infra IP pool'
                            Severity           = 'CRITICAL'
                            Description        = 'Test VMSwitch/Management VMNetworkAdapter readiness for all IP in infra IP pool'
                            Tags               = @{}
                            Remediation        = "Make sure at least one management VMNetworkAdapter with name $($mgmtAlias) configured correctly on the host $($env:COMPUTERNAME)."
                            TargetResourceID   = "Infra_IP_Connection_ManagementVMNetworkAdapterReadiness"
                            TargetResourceName = "Infra_IP_Connection_ManagementVMNetworkAdapterReadiness"
                            TargetResourceType = 'Infra_IP_Connection_ManagementVMNetworkAdapterReadiness'
                            Timestamp          = [datetime]::UtcNow
                            Status             = "FAILURE"
                            AdditionalData     = @{
                                Source    = $env:COMPUTERNAME
                                Resource  = 'ManagementVMNetworkAdapterReadiness'
                                Detail    = "[FAILED] Cannot test connection for infra IP with wrong management VMNetworkAdapter configured on host $($env:COMPUTERNAME)."
                                Status    = "FAILURE"
                                TimeStamp = [datetime]::UtcNow
                            }
                            HealthCheckSource  = $ENV:EnvChkrId
                        }

                        $instanceResults += New-AzStackHciResultObject @params
                    }
                }
                else
                {
                    Log-Info "Cannot get a VMSwitch to use on $($env:COMPUTERNAME) for validating infra IP connection. Fail the validation"

                    $params = @{
                        Name               = 'AzStackHci_Network_Test_Infra_IP_Connection_VMSwitch_Readiness'
                        Title              = 'Test VMSwitch readiness for all IP in infra IP pool'
                        DisplayName        = "Test VMSwitch readiness for all IP in infra IP pool"
                        Severity           = 'CRITICAL'
                        Description        = 'Test VMSwitch readiness for all IP in infra IP pool'
                        Tags               = @{}
                        Remediation        = "Make sure at least one VMSwitch preconfigured on the host $($env:COMPUTERNAME) has the same set of adapters defined in management intent."
                        TargetResourceID   = "Infra_IP_Connection_VMSwitchReadiness"
                        TargetResourceName = "Infra_IP_Connection_VMSwitchReadiness"
                        TargetResourceType = 'Infra_IP_Connection_VMSwitchReadiness'
                        Timestamp          = [datetime]::UtcNow
                        Status             = "FAILURE"
                        AdditionalData     = @{
                            Source    = $env:COMPUTERNAME
                            Resource  = 'VMSwitchReadiness'
                            Detail    = "[FAILED] Cannot test connection for infra IP with wrong VMSwitch configured on host $($env:COMPUTERNAME)."
                            Status    = "FAILURE"
                            TimeStamp = [datetime]::UtcNow
                        }
                        HealthCheckSource  = $ENV:EnvChkrId
                    }

                    $instanceResults += New-AzStackHciResultObject @params
                }
            }
            finally
            {
                if ($needCleanUpVMSwitch)
                {
                    # Clean up the VMSwitch created for testing
                    Log-Info "Clean up VMSwitch $($foundVMSwitchToUse.Name) created during the testing"
                    Remove-VMSwitch -Name $foundVMSwitchToUse.Name -Force -ErrorAction SilentlyContinue

                    if ($mgmtVlanIdToRestore -ne 0)
                    {
                        foreach ($tmpAdapter in $mgmtAdapters)
                        {
                            Log-Info "Restore VlanId for adapter $tmpAdapter to $mgmtVlanIdToRestore"
                            Set-NetAdapterAdvancedProperty -Name $tmpAdapter -RegistryKeyword "VlanID" -RegistryValue $mgmtVlanIdToRestore
                        }
                    }

                    # In case of DHCP scenario, after VMSwitch removed, the pNIC might not get the IP address immediately
                    # Wait for some time (60 seconds) to make sure the new IP is settled correctly.
                    [System.Boolean] $currentIPReady = $false
                    $ipStopWatch = [System.diagnostics.stopwatch]::StartNew()
                    while (-not $currentIPReady -and ($ipStopWatch.Elapsed.TotalSeconds -lt 60))
                    {
                        # If the pNIC has Manual or Dhcp IPv4 address with "Preferred" state, we consider it as "ready"
                        $ipConfig = Get-NetIPAddress -InterfaceAlias $mgmtAdapters[0] | Where-Object { ($_.PrefixOrigin -eq "Manual" -or $_.PrefixOrigin -eq "Dhcp") -and $_.AddressFamily -eq "IPv4" -and $_.AddressState -eq "Preferred" }

                        if ($ipConfig)
                        {
                            $currentIPReady = $true
                            break
                        }
                        else
                        {
                            Start-Sleep -Seconds 3
                        }
                    }

                    if (-not $currentIPReady)
                    {
                        # should not get into here, but keep it here for safety
                        $ipInfoAll = Get-NetIPAddress -AddressFamily IPv4 | Format-Table IPAddress, InterfaceAlias, PrefixLength, PrefixOrigin -AutoSize
                        Log-Info "$($ipInfoAll | Out-String)"
                        Log-Info "Cannot get the IP address back to the pNIC after VMSwitch removed. Please check the system manually."
                        throw "Cannot get the IP address back to the pNIC after VMSwitch removed. Please check the system manually."
                    }
                    else
                    {
                        Log-Info "IP address back to the pNIC after VMSwitch removed. System is ready for next validation."
                    }
                }
                else
                {
                    Log-Info "VMSwitch $($foundVMSwitchToUse.Name) pre-exist in the system. No need to clean up."
                }
            }
        }
        else
        {
            Log-Info "Hyper-V is not working correctly on $($env:COMPUTERNAME). Fail testing infra IP connection."

            $params = @{
                Name               = 'AzStackHci_Network_Test_Infra_IP_Connection_Hyper_V_Readiness'
                Title              = 'Test Hyper-V readiness for all IP in infra IP pool'
                DisplayName        = "Test Hyper-V readiness for all IP in infra IP pool"
                Severity           = 'CRITICAL'
                Description        = 'Test Hyper-V readiness for all IP in infra IP pool'
                Tags               = @{}
                Remediation        = "Make sure that Hyper-V is installed on host $($env:COMPUTERNAME) and rerun the validation."
                TargetResourceID   = "Infra_IP_Connection_HYPERVReadiness"
                TargetResourceName = "Infra_IP_Connection_HYPERVReadiness"
                TargetResourceType = 'Infra_IP_Connection_HYPERVReadiness'
                Timestamp          = [datetime]::UtcNow
                Status             = "FAILURE"
                AdditionalData     = @{
                    Source    = $env:COMPUTERNAME
                    Resource  = 'HYPERVReadiness'
                    Detail    = "[FAILED] Cannot test connection for infra IP without Hyper-V on host $($env:COMPUTERNAME)."
                    Status    = "FAILURE"
                    TimeStamp = [datetime]::UtcNow
                }
                HealthCheckSource  = $ENV:EnvChkrId
            }

            $instanceResults += New-AzStackHciResultObject @params
        }
        #endregion

        return $instanceResults
    }
    catch
    {
        throw $_
    }
    finally
    {
        # Device Management Service might need to be restarted to refresh the nic details
        # It also might not be there
        if (Get-Service -Name DeviceManagementService -ErrorAction SilentlyContinue)
        {
            Restart-Service -Name DeviceManagementService -Force -ErrorAction SilentlyContinue
            Start-Sleep -Seconds 20
            Log-Info "Restarted the Device Management Service successfully and waited 20 seconds which should refresh the nic details"
        }
    }
}

function Test-HostNetworkConfigurationReadiness
{
    [CmdletBinding()]
    param
    (
        [System.Management.Automation.Runspaces.PSSession[]] $PSSession,
        [PSObject[]] $AtcHostIntents
    )

    try
    {
        if (($PSSession.Count -eq 0) -or ($AtcHostIntents.Count -eq 0))
        {
            Log-Info "No PSSession or AtcHostIntents provided. Skip run of Test-HostNetworkConfigurationReadiness"
            return
        }
        else
        {
            Log-Info "Will check host network adapter RDMA status, adapter symmetry and bandwidth, and other host network"
            Log-Info "configuration (include DNS client configuraion, Hyper-V is running correctly, VMSwitch (if exists)"
            Log-Info "has mgmt intent adapters, VlanId for adapters, physical adapter used in JSON."
        }

        # Check host network readiness status
        $hostNetworkReadinessTestResults = @()

        foreach ($session in $PSSession)
        {
            #region Check RDMA status
            Log-Info "Checking NetAdapter RDMA status on $($session.ComputerName)"
            $rdmaResult = Invoke-Command -Session $session -ScriptBlock ${function:CheckNetAdapterRDMAStatus} -ArgumentList @(, $AtcHostIntents)

            if ($null -ne $rdmaResult)
            {
                Log-Info "Got RDMA validation results from $($session.ComputerName)"
                $currentMachineRdmaStatus = if ($rdmaResult.Pass) { 'SUCCESS' } else { 'FAILURE' }
                $currentMachineRdmaTestDetailMessage = $rdmaResult.Message
                Log-Info " Result: $($currentMachineRdmaTestDetailMessage)"
            }
            else
            {
                Log-Info "NO RDMA validation results found from $($session.ComputerName)"
                $currentMachineRdmaStatus = 'FAILURE'
                $currentMachineRdmaTestDetailMessage = "NO RDMA validation results returned by function CheckNetAdapterRDMAStatus from server $($session.ComputerName)"
            }

            $rdmaRstObject = @{
                Name               = 'AzStackHci_Network_Test_NetAdapter_RDMA_Operational'
                Title              = 'Test NetAdapter RDMA requirement'
                DisplayName        = "Test if RDMA requirement meets for the deployment on all servers"
                Severity           = 'CRITICAL'
                Description        = 'Checking RDMA Operational Status on {0}' -f $session.ComputerName
                Tags               = @{}
                Remediation        = 'Make sure adapter RDMA is operational. Use Get-NetAdapterRdma cmdlet to check the status of RDMA for the network adapter in the system.'
                TargetResourceID   = $session.ComputerName
                TargetResourceName = "NetAdapter"
                TargetResourceType = 'Network Adapter RDMA'
                Timestamp          = [datetime]::UtcNow
                Status             = $currentMachineRdmaStatus
                AdditionalData     = @{
                    Source    = $session.ComputerName
                    Resource  = 'Network Adapter RDMA Operational Status'
                    Detail    = $currentMachineRdmaTestDetailMessage
                    Status    = $currentMachineRdmaStatus
                    TimeStamp = [datetime]::UtcNow
                }
                HealthCheckSource  = $ENV:EnvChkrId
            }

            $hostNetworkReadinessTestResults += New-AzStackHciResultObject @rdmaRstObject
            #endregion

            #region Check adapter symmetry and bandwidth requirement
            Log-Info "Checking NetAdapter symmetry bandwidth requirement on $($session.ComputerName)"
            $adapterSymmetryAndBandwidthResult = Invoke-Command -Session $session -ScriptBlock ${function:CheckAdapterSymmetryAndBandwidth} -ArgumentList @(, $AtcHostIntents)

            if ($null -ne $adapterSymmetryAndBandwidthResult)
            {
                Log-Info "Got adapter symmetry and bandwidth validation results from $($session.ComputerName)"
                $currentMachineAdapterSymmetryBandwidthStatus = if ($adapterSymmetryAndBandwidthResult.Pass) { 'SUCCESS' } else { 'FAILURE' }
                $currentMachineAdapterSymmetryBandwidthTestDetailMessage = $adapterSymmetryAndBandwidthResult.Message
            }
            else
            {
                Log-Info "NO adapter symmetry and bandwidth validation results found from $($session.ComputerName)"
                $currentMachineAdapterSymmetryBandwidthStatus = 'FAILURE'
                $currentMachineAdapterSymmetryBandwidthTestDetailMessage = "NO adapter symmetry and bandwidth validation results returned by function CheckAdapterSymmetryAndBandwidth from server $($session.ComputerName)"
            }

            $adapterSymmetryRstObject = @{
                Name               = 'AzStackHci_Network_Test_NetAdapter_Symmetry_Bandwidth'
                Title              = 'Test NetAdapter symmetry and bandwidth requirement'
                DisplayName        = "Test if network adapters used in one intent is symmetry and if bandwidth meets minimum requirement"
                Severity           = 'CRITICAL'
                Description        = 'Checking network adapters and bandwidth Status on {0}' -f $session.ComputerName
                Tags               = @{}
                Remediation        = 'Make sure adapters used in intent are symmetry and mininum bandwidth to use for RDMA is 10G. Use Get-NetAdapter cmdlet on the system to check the adapter information.'
                TargetResourceID   = $session.ComputerName
                TargetResourceName = "NetAdapter"
                TargetResourceType = 'Network Adapter Symmetry and Bandwidth'
                Timestamp          = [datetime]::UtcNow
                Status             = $currentMachineAdapterSymmetryBandwidthStatus
                AdditionalData     = @{
                    Source    = $session.ComputerName
                    Resource  = 'Network Adapter Symmetry and Bandwidth'
                    Detail    = $currentMachineAdapterSymmetryBandwidthTestDetailMessage
                    Status    = $currentMachineAdapterSymmetryBandwidthStatus
                    TimeStamp = [datetime]::UtcNow
                }
                HealthCheckSource  = $ENV:EnvChkrId
            }

            $hostNetworkReadinessTestResults += New-AzStackHciResultObject @adapterSymmetryRstObject
            #endregion

            #region Host network configuration readiness
            Log-Info "Checking host network readiness configuration on $($session.ComputerName)"
            $networkReadinessResult = Invoke-Command -Session $session -ScriptBlock ${function:CheckHostNetworkConfigurationReadiness} -ArgumentList @(, $AtcHostIntents)

            if ($null -ne $networkReadinessResult)
            {
                Log-Info "Network readiness check results from $($session.ComputerName)"
                $currentMachineNetworkReadinessStatus = if ($networkReadinessResult.Pass) { 'SUCCESS' } else { 'FAILURE' }
                $currentMachineNetworkReadinessTestDetailMessage = $networkReadinessResult.Message
            }
            else
            {
                Log-Info "NO host network configuration readiness validation results found from $($session.ComputerName)"
                $currentMachineNetworkReadinessStatus = 'FAILURE'
                $currentMachineNetworkReadinessTestDetailMessage = "NO host network configuration readiness validation results returned by function CheckHostNetworkConfigurationReadiness from $($session.ComputerName)"
            }

            $networkReadinessRstObject = @{
                Name               = 'AzStackHci_Network_Test_HostNetworkConfigurationReadiness'
                Title              = 'Test host network configuration readiness'
                DisplayName        = "Test if host network requirement meets for the deployment on all servers"
                Severity           = 'CRITICAL'
                Description        = 'Checking host network configuration readiness status on {0}' -f $session.ComputerName
                Tags               = @{}
                Remediation        = 'Make sure host network configuration readiness is correct. Review detail message to find out the issue.'
                TargetResourceID   = $session.ComputerName
                TargetResourceName = "HostNetworkReadiness"
                TargetResourceType = 'HostNetworkReadiness'
                Timestamp          = [datetime]::UtcNow
                Status             = $currentMachineNetworkReadinessStatus
                AdditionalData     = @{
                    Source    = $session.ComputerName
                    Resource  = 'HostNetworkReadiness configuration status'
                    Detail    = $currentMachineNetworkReadinessTestDetailMessage
                    Status    = $currentMachineNetworkReadinessStatus
                    TimeStamp = [datetime]::UtcNow
                }
                HealthCheckSource  = $ENV:EnvChkrId
            }

            $hostNetworkReadinessTestResults += New-AzStackHciResultObject @networkReadinessRstObject
            #endregion
        }

        return $hostNetworkReadinessTestResults
    }
    catch
    {
        throw $_
    }
}

function Test-AdapterDriverMgmtAdapterReadiness
{
    [CmdletBinding()]
    param
    (
        [System.Management.Automation.Runspaces.PSSession[]] $PSSession,
        [PSCredential] $SessionCredential,
        [PSObject[]] $AtcHostIntents
    )

    try
    {
        $inboxDriverMgmtIPTestResults = @()

        $checkInboxDriverScript = {
            [CmdletBinding()]
            param (
                [String[]] $AdapterNames
            )

            $retVal = New-Object PSObject -Property @{
                Pass = $true
                Message = "Adapter inbox driver check on $($ENV:COMPUTERNAME)"
            }

            [PSObject[]] $allAdaptersInfo = Get-NetAdapter -Name $AdapterNames -ErrorAction SilentlyContinue

            if (($allAdaptersInfo.Count -eq 0) -or ($allAdaptersInfo.Count -ne $AdapterNames.Count))
            {
                $retVal.Pass = $false
                $retVal.Message += "`nFailed: Adapter(s) not found on the system"
                $retVal.Message += "`nExpected adapter(s): $($AdapterNames | Out-String)"
                $retVal.Message += "`nFound adapter(s): $($allAdaptersInfo | Out-String)"
            }
            else
            {
                $adaptersUsingInboxDriver = $allAdaptersInfo | Where-Object { $_.DriverProvider -match "Microsoft" -or $_.DriverProvider -match "Windows" }
                $hardwareType = (Get-WmiObject -Class Win32_ComputerSystem).Model

                # Check adatper is not inbox driver for physical environment only
                if (-not $adaptersUsingInboxDriver -or ($hardwareType -eq "Virtual Machine"))
                {
                    $retVal.Message += "`nPassed: No adapter using inbox driver found, or current system is a virtual environment."
                    $retVal.Message += "`nAdapter(s) checked: $($AdapterNames)"
                }
                else
                {
                    $retVal.Pass = $false
                    $retVal.Message += "`nFailed: Adapter(s) using inbox driver found on non-virtual environment`n"
                    $retVal.Message += ($adaptersUsingInboxDriver.Name | Out-String)
                }
            }

            return $retVal
        }

        $checkMgmtAdapterScript = {
            [CmdletBinding()]
            param (
                [String[]] $MgmtAdapterNames,
                [String] $MgmtIntentName
            )

            $retVal = New-Object PSObject -Property @{
                Pass = $true
                Message = "Management adapter IP and DNS check on $($ENV:COMPUTERNAME)"
            }

            $mgmtVNicName = "vManagement($($MgmtIntentName))"

            [PSObject[]] $allExistingVMSwitches = @()
            try
            {
                $allExistingVMSwitches = Get-VMSwitch -SwitchType External -ErrorAction SilentlyContinue
            }
            catch
            {
            }

            [System.Boolean] $expectedVMSwitchReadyInSystem = $false

            if ($allExistingVMSwitches.Count -gt 0)
            {
                # VMSwitch should contains 0 or all of the mgmt adapters
                # if VMSwitch contains all mgmt adapters, then there should be 1 vNIC named as "vManagement(mgmtintentname)""
                foreach ($externalVMSwitch in $allExistingVMSwitches)
                {
                    # Need to check the switch is good for deployment: using same adapter as the intent
                    [System.Guid[]] $switchAdapterGuids = $externalVMSwitch.NetAdapterInterfaceGuid
                    [System.Guid[]] $intentAdapterGuids = (Get-NetAdapter -Name $MgmtAdapterNames -Physical -ErrorAction SilentlyContinue).InterfaceGuid

                    if (Compare-Object -ReferenceObject $switchAdapterGuids -DifferenceObject $intentAdapterGuids)
                    {
                        # Adapters used in pre-defined VMSwitch and the intent are different. Need to make sure 0 mgmt adapter used by VMSwitch
                        foreach ($mgmtAdapter in $intentAdapterGuids)
                        {
                            if ($switchAdapterGuids -contains $mgmtAdapter)
                            {
                                $retVal.Pass = $false
                                $retVal.Message += "`nFailed: Adapter with GUID $($mgmtAdapter) defined in management intent is used by VMSwitch $($externalVMSwitch.Name). Please make sure VMSwitch use ALL adapters defined in the management intent."
                            }
                        }
                    }
                    else
                    {
                        $retVal.Message += "`nPassed: Found one VMSwitch [ $($externalVMSwitch.Name) ] in the system that uses all physical adapters defined in the management intent."

                        $expectedVMSwitchReadyInSystem = $true

                        # VMSwitch uses same set of adapters defined in mgmt intent, will need to check there is a vNIC named as "vManagement(mgmtintentname)"
                        [PSObject[]] $expectedVMNetworkAdapterMgmtNIC = Get-VMNetworkAdapter -ManagementOS -Name $mgmtVNicName -ErrorAction SilentlyContinue
                        if ($expectedVMNetworkAdapterMgmtNIC.Count -ne 1)
                        {
                            $retVal.Pass = $false
                            $retVal.Message += "`nFailed: Expected 1 VMNetworkAdapter with name [ $($mgmtVNicName) ] in the system. But found $($expectedVMNetworkAdapterMgmtNIC.Count). Please check by running Get-VMNetWorkAdapter."
                        }
                        else
                        {
                            $retVal.Message += "`nPassed: Found one VMNetworkAdapter named as $mgmtVNicName in the system"
                        }

                        [PSObject[]] $expectedNetAdapterMgmtNIC = Get-NetAdapter -Name $mgmtVNicName -ErrorAction SilentlyContinue
                        if ($expectedNetAdapterMgmtNIC.Count -ne 1)
                        {
                            $retVal.Pass = $false
                            $retVal.Message += "`nFailed: Expected 1 NetAdapter with name [ $($mgmtVNicName) ] in the system. But found $($expectedNetAdapterMgmtNIC.Count). Please check by running Get-NetAdapter."
                        }
                        else
                        {
                            $retVal.Message += "`nPassed: Found one NetAdapter named as $mgmtVNicName in the system"
                        }
                    }
                }
            }

            [String[]] $adaptersToCheck = @()

            if ($expectedVMSwitchReadyInSystem)
            {
                $adaptersToCheck = @($mgmtVNicName)
            }
            else
            {
                $adaptersToCheck = $MgmtAdapterNames
            }

            # Following checks only be performed on the 1st adapter in the list as other adapters might not have valid IP address configured on it during the test
            # Adapter IP checking
            [PSObject[]] $currentAdapterAddresses = Get-NetIPAddress -InterfaceAlias $adaptersToCheck[0] -AddressFamily IPv4 -PolicyStore ActiveStore -ErrorAction SilentlyContinue | Where-Object { ($_.PrefixOrigin -eq "Manual" -or $_.PrefixOrigin -eq "Dhcp") -and $_.AddressState -eq "Preferred" }

            if ($currentAdapterAddresses.Count -ne 1)
            {
                $retVal.Pass = $false
                $retVal.Message += "`nFailed: Expected one and only one valid IP address on management adapter $($adaptersToCheck[0]). Found [ $($currentAdapterAddresses.Count) ]."
                $retVal.Message += "`nIP addresses found: $($currentAdapterAddresses.IPAddress | Out-String)"
            }
            else
            {
                $retVal.Message += "`nPassed: Got one and only one valid IP address on management adapter $($adaptersToCheck[0])"
                $retVal.Message += "`nIP address found: $($currentAdapterAddresses.IPAddress | Out-String)"
            }

            # Adapter DNS server checking.
            [PSObject[]] $mgmtAdapterDNSClientServerAddresses = Get-DnsClientServerAddress -InterfaceAlias $adaptersToCheck[0] -AddressFamily IPv4 -ErrorAction SilentlyContinue
            if ($mgmtAdapterDNSClientServerAddresses -and ($mgmtAdapterDNSClientServerAddresses.Count -gt 0) -and ($mgmtAdapterDNSClientServerAddresses[0].ServerAddresses.Count -gt 0))
            {
                $retVal.Message += "`nPassed: DNS client server addresses found on management adapter $($adaptersToCheck[0])"
                $retVal.Message += "`nIP address found: $($mgmtAdapterDNSClientServerAddresses.ServerAddresses | Out-String)"
            }
            else
            {
                $retVal.Pass = $false
                $retVal.Message += "`nFailed: Cannot find any DNS client server addresses on management adapter $($adaptersToCheck[0])"
            }

            return $retVal
        }

        foreach ($session in $PSSession)
        {
            #region Make sure session is there and open
            # This is needed here as the Test-MgmtIP test might break the session due to potential VMSwitch/vNIC creation
            if ($session.State -ne 'Opened')
            {
                Log-Info "Previous PSSession is closed. Opening new session for testing"
                if ($SessionCredential)
                {
                    $sessionToCheck = New-PsSessionWithRetriesInternal -Node $session.ComputerName -Credential $SessionCredential
                }
                else
                {
                    throw "Session is not opened and no credential provided"
                }
            }
            else
            {
                $sessionToCheck = $session
            }
            #endregion

            #region TEST1: Check inbox driver for all adapters in the host
            Log-Info "Check storage intent adapter inbox driver on $($sessionToCheck.ComputerName)"
            [System.String[]] $storageAdapters = $AtcHostIntents | Where-Object { $_.TrafficType.Contains("Storage") } | Select-Object -ExpandProperty Adapter

            if ($storageAdapters.Count -gt 0)
            {
                $tmpInboxDriverCheckRst = Invoke-Command -Session $sessionToCheck -ScriptBlock $checkInboxDriverScript -ArgumentList @(, $storageAdapters)

                if ($null -ne $tmpInboxDriverCheckRst)
                {
                    Log-Info "Got inbox driver validation results from $($sessionToCheck.ComputerName)"
                    $currentMachineInboxDriverTestStatus = if ($tmpInboxDriverCheckRst.Pass) { 'SUCCESS' } else { 'FAILURE' }
                    $currentMachineInboxDriverTestDetailMessage = $tmpInboxDriverCheckRst.Message
                }
                else
                {
                    # Should not come here, just a safe guard
                    Log-Info "NO inbox driver validation results found from $($sessionToCheck.ComputerName)"
                    $currentMachineInboxDriverTestStatus = 'FAILURE'
                    $currentMachineInboxDriverTestDetailMessage = "NO inbox driver validation results returned from server $($session.ComputerName)"
                }

                $inboxDriverCheckRstObject = @{
                    Name               = 'AzStackHci_Network_Test_AdapterDriver'
                    Title              = 'Test system adapter driver provider'
                    DisplayName        = 'Test system adapter driver provider'
                    Severity           = 'CRITICAL'
                    Description        = 'Checking adapter driver on {0}' -f $sessionToCheck.ComputerName
                    Tags               = @{}
                    Remediation        = 'https://learn.microsoft.com/en-us/azure-stack/hci/concepts/host-network-requirements#driver-requirements'
                    TargetResourceID   = $sessionToCheck.ComputerName
                    TargetResourceName = "AdapterDriver"
                    TargetResourceType = 'AdapterDriver'
                    Timestamp          = [datetime]::UtcNow
                    Status             = $currentMachineInboxDriverTestStatus
                    AdditionalData     = @{
                        Source    = $sessionToCheck.ComputerName
                        Resource  = 'AdapterDriver'
                        Detail    = $currentMachineInboxDriverTestDetailMessage
                        Status    = $currentMachineInboxDriverTestStatus
                        TimeStamp = [datetime]::UtcNow
                    }
                    HealthCheckSource  = $ENV:EnvChkrId
                }

                $inboxDriverMgmtIPTestResults += New-AzStackHciResultObject @inboxDriverCheckRstObject
            }
            else
            {
                Log-Info "No storage adapter found in intent definition for host $($sessionToCheck.ComputerName). Skip inbox driver check for storage intent adapter."
            }
            #endregion

            #region TEST2: Check no more than 1 IP address and DNS client server address on mgmt adapter
            Log-Info "Check on mgmt adapter readiness on $($sessionToCheck.ComputerName)"
            [PSObject[]] $mgmtIntent = $AtcHostIntents | Where-Object { $_.TrafficType.Contains("Management") }
            [System.String] $mgmtIntentName = $mgmtIntent[0].Name
            [System.String[]] $mgmtAdapters = GetSortedMgmtIntentAdapter -MgmtAdapterNames $mgmtIntent[0].Adapter

            $tmpMgmtAdapterIPCheckRst = Invoke-Command -Session $sessionToCheck -ScriptBlock $checkMgmtAdapterScript -ArgumentList @($mgmtAdapters, $mgmtIntentName)

            if ($null -ne $tmpMgmtAdapterIPCheckRst)
            {
                Log-Info "Got mgmt adapter IP validation results from $($sessionToCheck.ComputerName)"
                $currentMachineMgmtIPTestStatus = if ($tmpMgmtAdapterIPCheckRst.Pass) { 'SUCCESS' } else { 'FAILURE' }
                $currentMachineMgmtIPTestDetailMessage = $tmpMgmtAdapterIPCheckRst.Message
            }
            else
            {
                # Should not come here, just a safe guard
                Log-Info "NO mgmt adapter IP validation results found from $($sessionToCheck.ComputerName)"
                $currentMachineMgmtIPTestStatus = 'FAILURE'
                $currentMachineMgmtIPTestDetailMessage = "NO mgmt adapter IP validation results returned from server $($session.ComputerName)"
            }

            $mgmtAdapterCheckRstObject = @{
                Name               = 'AzStackHci_Network_Test_MgmtAdapterReadiness'
                Title              = 'Test system management adapter readiness'
                DisplayName        = 'Test system management adapter readiness'
                Severity           = 'CRITICAL'
                Description        = 'Checking there is one valid management adapter on {0}: make sure it has only one valid IP assigned on it on and DNS server set correctly on it' -f $sessionToCheck.ComputerName
                Tags               = @{}
                Remediation        = 'Make sure there is one valid outbound management adapter on host and have only one valid IP assigned to it and DNS server set correctly on it as well.'
                TargetResourceID   = $sessionToCheck.ComputerName
                TargetResourceName = "MgmtAdapterIP"
                TargetResourceType = 'MgmtAdapterIP'
                Timestamp          = [datetime]::UtcNow
                Status             = $currentMachineMgmtIPTestStatus
                AdditionalData     = @{
                    Source    = $sessionToCheck.ComputerName
                    Resource  = 'MgmtAdapterIP'
                    Detail    = $currentMachineMgmtIPTestDetailMessage
                    Status    = $currentMachineMgmtIPTestStatus
                    TimeStamp = [datetime]::UtcNow
                }
                HealthCheckSource  = $ENV:EnvChkrId
            }

            $inboxDriverMgmtIPTestResults += New-AzStackHciResultObject @mgmtAdapterCheckRstObject
            #endregion
        }

        return $inboxDriverMgmtIPTestResults
    }
    catch
    {
        throw $_
    }
}

# Run during both Deployment and AddNode
# 1. Mgmt NIC IP should not be overlapping with IP Pool
# 2. Ensure Mgmt NIC IPs and IP Pool are in the same subnet
function TestDHCPStatus
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, HelpMessage = "Specify starting Management IP Range")]
        [System.Collections.ArrayList]
        $IpPools,

        [System.Management.Automation.Runspaces.PSSession[]]
        $PSSession,

        [pscredential]
        $SessionCredential
    )
    try
    {
        $instanceResults = @()
        foreach ($ipPool in $IpPools)
        {
            $StartingAddress = $ipPool.StartingAddress
            $EndingAddress = $ipPool.EndingAddress

            foreach ($session in $PSSession) {
                $sb = {
                    $env:COMPUTERNAME
                    (
                        Get-NetIPConfiguration |
                        Where-Object {
                            $_.IPv4DefaultGateway -ne $null -and
                            $_.NetAdapter.Status -eq "Up"
                        }
                    ).IPv4Address.IPAddress
                }

                if ($session.State -ne 'Opened')
                {
                    Log-Info "Previous PSSession is closed. Opening new session for testing"
                    if ($SessionCredential)
                    {
                        $currentSession = New-PsSessionWithRetriesInternal -Node $session.ComputerName -Credential $SessionCredential
                    }
                    else
                    {
                        throw "Session is not opened and no credential provided"
                    }
                }
                else
                {
                    $currentSession = $session
                }

                $NewNodeData = Invoke-Command $currentSession -ScriptBlock $sb
                $NodeName = $NewNodeData[0]
                # Check for all of the IPs found on the Host
                for ($i = 1; $i -lt $NewNodeData.count; $i++) {
                    $NodeManagementIPAddress = $NewNodeData[$i]

                    Log-Info "Node Name retrieved from session: $NodeName"
                    Log-Info "Node Management IP Address retrieved from session: $NodeManagementIPAddress"
                    # Check node management IP is not in infra pool range
                    Log-Info "Starting Test Mgmt IP is not in Infra IP Pool for $($currentSession.ComputerName)"
                    $ip = [system.net.ipaddress]::Parse($NodeManagementIPAddress).GetAddressBytes()
                    [array]::Reverse($ip)
                    $ip = [system.BitConverter]::ToUInt32($ip, 0)

                    $from = [system.net.ipaddress]::Parse($StartingAddress).GetAddressBytes()
                    [array]::Reverse($from)
                    $from = [system.BitConverter]::ToUInt32($from, 0)

                    $to = [system.net.ipaddress]::Parse($EndingAddress).GetAddressBytes()
                    [array]::Reverse($to)
                    $to = [system.BitConverter]::ToUInt32($to, 0)

                    $mgmtIPOutsideRange = ($ip -le $from) -or ($ip -ge $to)
                    if ($mgmtIPOutsideRange) {
                        $TestMgmtIPInfraRangeDetail = $lnTxt.TestMgmtIPInfraRangePass -f $NodeManagementIPAddress, $StartingAddress, $EndingAddress
                    }
                    else {
                        $TestMgmtIPInfraRangeDetail = $lnTxt.TestMgmtIPInfraRangeFail -f $NodeManagementIPAddress, $StartingAddress, $EndingAddress
                        Log-Info $TestMgmtIPInfraRangeDetail -Type Warning
                    }
                    $status = if ($mgmtIPOutsideRange) { 'SUCCESS' } else { 'FAILURE' }

                    $params = @{
                        Name               = 'AzStackHci_Network_Test_New_DHCP_Validity_Infra_Pool'
                        Title              = 'Test DHCP Configuration Validity Management IP Infra Pool'
                        DisplayName        = "Test DHCP Configuration Validity Management IP Infra Pool"
                        Severity           = 'CRITICAL'
                        Description        = 'Checking management IPs are not in infra IP pool'
                        Tags               = @{}
                        Remediation        = 'https://aka.ms/hci-envch'
                        TargetResourceID   = "$StartingAddress-$EndingAddress"
                        TargetResourceName = "DHCPDeploymentConfiguration"
                        TargetResourceType = 'DHCPConfiguration'
                        Timestamp          = [datetime]::UtcNow
                        Status             = $status
                        AdditionalData     = @{
                            Source    = $currentSession.ComputerName
                            Resource  = 'DHCPNodeManagementIP'
                            Detail    = $TestMgmtIPInfraRangeDetail
                            Status    = $status
                            TimeStamp = [datetime]::UtcNow
                        }
                        HealthCheckSource  = $ENV:EnvChkrId
                    }
                    $instanceResults += New-AzStackHciResultObject @params

                    $TestMgmtSubnet = TestMgmtSubnet -StartingAddress $NodeManagementIPAddress -EndingAddress $EndingAddress
                    $status = if ($TestMgmtSubnet) { 'SUCCESS' } else { 'FAILURE' }

                    $params = @{
                        Name               = 'AzStackHci_Network_Test_New_DHCP_Validity_Infra_Subnet'
                        Title              = 'Test DHCP Configuration Validity Management IP Infra Subnet'
                        DisplayName        = "Test DHCP Configuration Validity Management IP Infra Subnet"
                        Severity           = 'CRITICAL'
                        Description        = 'Checking management IPs are in same subnet as infra IP pool'
                        Tags               = @{}
                        Remediation        = 'https://aka.ms/hci-envch'
                        TargetResourceID   = "$StartingAddress-$EndingAddress"
                        TargetResourceName = "DHCPDeploymentConfiguration"
                        TargetResourceType = 'DHCPConfiguration'
                        Timestamp          = [datetime]::UtcNow
                        Status             = $status
                        AdditionalData     = @{
                            Source    = "$($currentSession.ComputerName)AndCustomerNetwork"
                            Resource  = 'DHCPNodeManagementIPAndCustomerSubnet'
                            Detail    = if ($TestMgmtSubnet) { $lnTxt.TestMgmtSubnetPass -f $NodeManagementIPAddress, $EndingAddress } else { $lnTxt.TestMgmtSubnetFail -f $NodeManagementIPAddress, $EndingAddress }
                            Status    = $status
                            TimeStamp = [datetime]::UtcNow
                        }
                        HealthCheckSource  = $ENV:EnvChkrId
                    }
                    $instanceResults += New-AzStackHciResultObject @params
                }
            }
        }
        return $instanceResults
    }
    catch
    {
        throw $_
    }
}

# Initial tests to determine if Mgmt IP of new Node is OK
# Below Tests are for Static IP Allocation (Non-DHCP)
function TestMgmtIPForNewNode
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false, HelpMessage = "Specify starting Management IP Range")]
        [System.Collections.ArrayList]
        $IpPools,

        [System.Management.Automation.Runspaces.PSSession[]]
        $PSSession,

        [Hashtable]
        $NodeToManagementIPMap,

        [PSObject[]] $AtcHostIntents
    )
    try
    {
        $instanceResults = @()
        $AdditionalData = @()

        $newNodeSession = $PSSession[0]

        [PSObject[]] $mgmtIntent = $AtcHostIntents | Where-Object { $_.TrafficType.Contains("Management") }
        $intentName = $mgmtIntent[0].Name
        $firstAdapterName = $mgmtIntent[0].Adapter[0]

        $sb = {
            $env:COMPUTERNAME
            (
                Get-NetIPConfiguration |
                Where-Object {
                    $_.IPv4DefaultGateway -ne $null -and
                    $_.NetAdapter.Status -eq "Up"
                }
            ).IPv4Address.IPAddress
        }
        $NewNodeData = Invoke-Command $newNodeSession -ScriptBlock $sb
        $NodeName = $NewNodeData[0]
        $NodeManagementIPAddress = $NewNodeData[1]

        Log-Info "Node Name retrieved from PSSession: $NodeName"
        Log-Info "Node Management IP Address retrieved from PSSession: $NodeManagementIPAddress"

        foreach ($ipPool in $IpPools)
        {
            $StartingAddress = $ipPool.StartingAddress
            $EndingAddress = $ipPool.EndingAddress


            # Check node management IP is not in infra pool range
            Log-Info "Starting Test Mgmt IP is not in Infra IP Pool for $($newNodeSession.ComputerName)"
            $ip = [system.net.ipaddress]::Parse($NodeManagementIPAddress).GetAddressBytes()
            [array]::Reverse($ip)
            $ip = [system.BitConverter]::ToUInt32($ip, 0)

            $from = [system.net.ipaddress]::Parse($StartingAddress).GetAddressBytes()
            [array]::Reverse($from)
            $from = [system.BitConverter]::ToUInt32($from, 0)

            $to = [system.net.ipaddress]::Parse($EndingAddress).GetAddressBytes()
            [array]::Reverse($to)
            $to = [system.BitConverter]::ToUInt32($to, 0)


            $mgmtIPOutsideRange = ($ip -le $from) -or ($ip -ge $to)
            if ($mgmtIPOutsideRange) {
                $TestMgmtIPInfraRangeDetail = $lnTxt.TestMgmtIPInfraRangePass -f $NodeManagementIPAddress, $StartingAddress, $EndingAddress
                $status = 'SUCCESS'
            }
            else {
                $TestMgmtIPInfraRangeDetail = $lnTxt.TestMgmtIPInfraRangeFail -f $NodeManagementIPAddress, $StartingAddress, $EndingAddress
                Log-Info $TestMgmtIPInfraRangeDetail -Type Warning
                $status = 'FAILURE'
            }
            $params = @{
                Name               = 'AzStackHci_Network_Test_New_Node_Validity_Outside_Mgmt_Range'
                Title              = 'Test New Node Configuration Outside Management Range'
                DisplayName        = "Test New Node Configuration Outside Management Range"
                Severity           = 'CRITICAL'
                Description        = 'Checking New Node IP'
                Tags               = @{}
                Remediation        = 'https://aka.ms/hci-envch'
                TargetResourceID   = $NodeManagementIPAddress
                TargetResourceName = "IPAddress"
                TargetResourceType = 'IPAddress'
                Timestamp          = [datetime]::UtcNow
                Status             = $status
                AdditionalData     = @{
                    Source    = $NodeName
                    Resource  = 'NewNodeManagementIP'
                    Detail    = $TestMgmtIPInfraRangeDetail
                    Status    = $status
                    TimeStamp = [datetime]::UtcNow
                }
                HealthCheckSource  = $ENV:EnvChkrId
            }
            $instanceResults += New-AzStackHciResultObject @params
        }

        # Check that no management IPs are the same (Mgmt IP shouldn't conflict with existing node)
        Log-Info "Starting Test for No Mgmt IPs are the same for any Nodes"
        $duplicateIPs = $false
        $numDuplicates = $NodeToManagementIPMap.GetEnumerator() | Group-Object Value | ? { $_.Count -gt 1 }
        if ($numDuplicates -ne $null) {
            $duplicateIPs = $true
            Log-Info 'Duplicate IPs found for Node Management IPs' -Type Warning
        }

        if ($duplicateIPs) {
            $dtl = 'Duplicate IPs found for Node Management IPs'
            $status = 'FAILURE'
        }
        else {
            $dtl = 'No Duplicate IPs found for Node Management IPs'
            $status = 'SUCCESS'
        }

        $params = @{
            Name               = 'AzStackHci_Network_Test_New_Node_Validity_Duplicate_IP'
            Title              = 'Test New Node Configuration Duplicate IP'
            DisplayName        = "Test New Node Configuration Duplicate IP"
            Severity           = 'CRITICAL'
            Description        = 'Checking New Node IP is not a duplicate'
            Tags               = @{}
            Remediation        = 'https://aka.ms/hci-envch'
            TargetResourceID   = $NodeManagementIPAddress
            TargetResourceName = "IPAddress"
            TargetResourceType = 'IPAddress'
            Timestamp          = [datetime]::UtcNow
            Status             = $status
            AdditionalData     = @{
                Source    = 'NodeAndManagementIPMapping'
                Resource  = 'NodeManagementIPs'
                Detail    = $dtl
                Status    = $status
                TimeStamp = [datetime]::UtcNow
            }
            HealthCheckSource  = $ENV:EnvChkrId
        }
        $instanceResults += New-AzStackHciResultObject @params

        # Check that host name exists, and the name and mgmt IP both match current node
        Log-Info "Starting Test to check if Mgmt IP is on a different node as $NodeName"
        Log-Info "Starting simultaneous Test to check if HostName and Mgmt IP Match for $NodeName"
        $ipOnAnotherNode = $false
        $NodeNameAndIPMatches = $false
        $nodeNameForIP = $null
        foreach ($NodeIP in $NodeToManagementIPMap.GetEnumerator()) {
            Write-Host "$($NodeIP.Name): $($NodeIP.Value)"
            if ($NodeIP.Name -eq $NodeName) {
                if ($NodeIP.Value -eq $NodeManagementIPAddress) {
                    $NodeNameAndIPMatches = $true
                    $nodeNameForIP = $NodeIP.Name
                }
            } else {
                if ($NodeIP.Value -eq $NodeManagementIPAddress) {
                    $ipOnAnotherNode = $true
                    $nodeNameForIP = $NodeIP.Name
                }
            }
        }

        if ($ipOnAnotherNode) {
            $CheckMgmtIPNotOnOtherNodeDetail = $lnTxt.CheckMgmtIPNotOnOtherNodeFail -f $NodeManagementIPAddress, $nodeNameForIP
            Log-Info $CheckMgmtIPNotOnOtherNodeDetail -Type Warning
        }
        else {
            $CheckMgmtIPNotOnOtherNodeDetail = $lnTxt.CheckMgmtIPNotOnOtherNodePass -f $NodeManagementIPAddress, $nodeNameForIP
        }
        $status = if ($ipOnAnotherNode) { 'FAILURE' } else { 'SUCCESS' }
        $params = @{
            Name               = 'AzStackHci_Network_Test_New_Node_Validity_IP_Conflict'
            Title              = 'Test New Node Configuration Conflicting IP'
            DisplayName        = "Test New Node Configuration Conflicting IP"
            Severity           = 'CRITICAL'
            Description        = 'Checking New Node IP is not on another node'
            Tags               = @{}
            Remediation        = 'https://aka.ms/hci-envch'
            TargetResourceID   = $NodeManagementIPAddress
            TargetResourceName = "IPAddress"
            TargetResourceType = 'IPAddress'
            Timestamp          = [datetime]::UtcNow
            Status             = $status
            AdditionalData     = @{
                Source    = 'NodeAndManagementIPMapping'
                Resource  = 'NodeNameAndManagementIP'
                Detail    = $CheckMgmtIPNotOnOtherNodeDetail
                Status    = $status
                TimeStamp = [datetime]::UtcNow
            }
            HealthCheckSource  = $ENV:EnvChkrId
        }
        $instanceResults += New-AzStackHciResultObject @params

        if ($NodeNameAndIPMatches) {
            $CheckMgmtIPOnNewNodeDetail = $lnTxt.CheckMgmtIPOnNewNodePass -f $NodeManagementIPAddress, $nodeNameForIP
            $status = 'SUCCESS'
        }
        else {
            $CheckMgmtIPOnNewNodeDetail = $lnTxt.CheckMgmtIPOnNewNodeFail -f $NodeManagementIPAddress, $nodeNameForIP
            Log-Info $CheckMgmtIPOnNewNodeDetail -Type Warning
            $status = 'FAILURE'
        }

        $params = @{
            Name               = 'AzStackHci_Network_Test_New_Node_And_IP_Match'
            Title              = 'Test New Node Configuration Name and IP Match'
            DisplayName        = "Test New Node Configuration Name and IP Match"
            Severity           = 'CRITICAL'
            Description        = 'Checking New Node Name and IP match'
            Tags               = @{}
            Remediation        = 'https://aka.ms/hci-envch'
            TargetResourceID   = $NodeManagementIPAddress
            TargetResourceName = "IPAddress"
            TargetResourceType = 'IPAddress'
            Timestamp          = [datetime]::UtcNow
            Status             = $status
            AdditionalData     = @{
                Source    = 'NodeAndManagementIPMapping'
                Resource  = 'NewNodeNameAndManagementIP'
                Detail    = $CheckMgmtIPOnNewNodeDetail
                Status    = $status
                TimeStamp = [datetime]::UtcNow
            }
            HealthCheckSource  = $ENV:EnvChkrId
        }
        $instanceResults += New-AzStackHciResultObject @params

        # Check that New Node has the first physical adapter and the physical adapter has the mgmt IP
        Log-Info "Starting Test to see if $firstAdapterName on $NodeName has the correct Mgmt IP"
        $adapterSB = {
            param($adapterName)
            $returnDict = @{}
            $returnDict["GetNetIPAddressOutput"] = Get-NetIPAddress
            $returnDict["GetNetAdapterOutput"] = Get-NetAdapter
            $AdapterIPObject = Get-NetIPAddress -InterfaceAlias $adapterName -AddressFamily IPv4 -ErrorAction SilentlyContinue
            if ($AdapterIPObject -eq $null) {
                $returnDict["Result"] = $false
                $returnDict["AdapterName"] = $adapterName
                return $returnDict
            }
            $returnDict["Result"] = $true
            $returnDict["AdapterName"] = $adapterName
            $returnDict["AdapterIP"] = $AdapterIPObject.IPAddress
            return $returnDict
        }

        $AdapterContainsMgmtIP = $false
        $physicalAdapterExists = $false
        $VirtualNICName = "vManagement($intentName)"
        try {
            $NewNodeAdapterData = Invoke-Command $newNodeSession -ScriptBlock $adapterSB -ArgumentList $firstAdapterName
            Log-Info "Data found for New Node Adapter ($firstAdapterName): $($NewNodeAdapterData | Out-String)"
            if ($NewNodeAdapterData['Result'] -eq $false) {
                Log-Info "Physical Adapter Not Found"
                Log-Info "Get-NetIPAddress output: $($NewNodeAdapterData['GetNetIPAddressOutput'] | Out-String)"
                Log-Info "Get-NetAdapter output: $($NewNodeAdapterData['GetNetAdapterOutput'] | Out-String)"
            }
            elseif ($NewNodeAdapterData['Result'] -eq $true -and $NewNodeAdapterData['AdapterIP'] -eq $NodeManagementIPAddress) {
                Log-Info "Physical Adapter found with Correct IP: $($NewNodeAdapterData['AdapterIP'] | Out-String)"
                $physicalAdapterExists = $true
                $AdapterContainsMgmtIP = $true
                $CheckAdapterContainsIPDetail = $lnTxt.CheckAdapterContainsIPPass -f $firstAdapterName, $NodeManagementIPAddress
            }
            else {
                Log-Info "Physical Adapter found but with incorrect IP"
                Log-Info "Get-NetIPAddress output: $($NewNodeAdapterData['GetNetIPAddressOutput'] | Out-String)"
                Log-Info "Get-NetAdapter output: $($NewNodeAdapterData['GetNetAdapterOutput'] | Out-String)"
            }

            # In certain cases, new node will be set up with the vNIC instead and need to check that for mgmt IP
            if (!$physicalAdapterExists) {
                Log-Info "Physical Adapter does not exist or mgmt IP is wrong. Checking Virtual Adapter" -Type Warning
                $NewNodeVirtualAdapterData = Invoke-Command $newNodeSession -ScriptBlock $adapterSB -ArgumentList $VirtualNICName
                Log-Info "Data found for New Node Virtual Adapter ($VirtualNICName): $($NewNodeVirtualAdapterData | Out-String)"
                if ($NewNodeVirtualAdapterData['Result'] -eq $false) {
                    Log-Info "Virtual Adapter Not Found"
                    Log-Info "Get-NetIPAddress output: $($NewNodeVirtualAdapterData['GetNetIPAddressOutput'] | Out-String)"
                    Log-Info "Get-NetAdapter output: $($NewNodeVirtualAdapterData['GetNetAdapterOutput'] | Out-String)"
                }
                elseif ($NewNodeVirtualAdapterData['Result'] -eq $true -and $NewNodeVirtualAdapterData['AdapterIP'] -eq $NodeManagementIPAddress) {
                    Log-Info "Virtual Adapter found with Correct IP: $($NewNodeVirtualAdapterData['AdapterIP'] | Out-String)"
                    $AdapterContainsMgmtIP = $true
                    $CheckAdapterContainsIPDetail = $lnTxt.CheckAdapterContainsIPPass -f $VirtualNICName, $NodeManagementIPAddress
                }
                else {
                    Log-Info "Virtual Adapter found but with incorrect IP"
                    Log-Info "Get-NetIPAddress output: $($NewNodeVirtualAdapterData['GetNetIPAddressOutput'] | Out-String)"
                    Log-Info "Get-NetAdapter output: $($NewNodeVirtualAdapterData['GetNetAdapterOutput'] | Out-String)"
                }
            }
        }
        catch {
            Log-Info "Exception thrown when checking New Node Adapter: $_" -Type Warning
        }

        if (!$AdapterContainsMgmtIP) {
            $CheckAdapterContainsIPDetail = $lnTxt.CheckAdapterContainsIPFail -f $firstAdapterName, $VirtualNICName, $NodeManagementIPAddress
            Log-Info $CheckAdapterContainsIPDetail -Type Warning
            $status = 'FAILURE'
        }
        else
        {
            $status = 'SUCCESS'
        }

        $params = @{
            Name               = 'AzStackHci_Network_Test_New_Node_First_Adapter_Validity'
            Title              = 'Test New Node Configuration First Network Adapter has Management IP'
            DisplayName        = "Test New Node Configuration First Network Adapter has Management IP"
            Severity           = 'CRITICAL'
            Description        = 'Checking New Node first adapter has management IP'
            Tags               = @{}
            Remediation        = 'https://learn.microsoft.com/en-us/azure-stack/hci/deploy/deployment-tool-checklist'
            TargetResourceID   = $NodeManagementIPAddress
            TargetResourceName = $firstAdapterName
            TargetResourceType = 'Network Adapter'
            Timestamp          = [datetime]::UtcNow
            Status             = $status
            AdditionalData     = @{
                Source    = 'NewNodeAdapter'
                Resource  = 'NewNodeAdapterIP'
                Detail    = $CheckAdapterContainsIPDetail
                Status    = $status
                TimeStamp = [datetime]::UtcNow
            }
            HealthCheckSource  = $ENV:EnvChkrId
        }
        $instanceResults += New-AzStackHciResultObject @params
        return $instanceResults
    }
    catch
    {
        throw $_
    }
}

function TestMgmtSubnet
{
    <#
    .SYNOPSIS
        Ensure Start and End IPs are on the same subnet.
    #>


    param (
        [Parameter(Mandatory = $false, HelpMessage = "Specify starting Management IP Range")]
        [System.Net.IPAddress]
        $StartingAddress,

        [Parameter(Mandatory = $false, HelpMessage = "Specify end Management IP Range")]
        [System.Net.IPAddress]
        $EndingAddress
    )

    try
    {
        $start = $StartingAddress -replace "\.[0-9]{1,3}$", ""
        $end = $EndingAddress -replace "\.[0-9]{1,3}$", ""

        if ($start -eq $end)
        {
            Log-info "Subnet start: $start and end: $end"
            return $true
        }
        else
        {
            return $false
        }
    }
    catch
    {
        throw "Failed to check subnet. Error: $_"
    }
}

function TestMgmtIpPools
{
    <#
    .SYNOPSIS
        Ensure all ip are in management subnet.
    #>


    param (
        [Parameter(Mandatory = $false, HelpMessage = "Specify starting Management IP Range")]
        [System.Collections.ArrayList]
        $IpPools,

        [Parameter(Mandatory = $false, HelpMessage = "Specify Management Subnet")]
        [string] $ManagementSubnetValue
    )

    try
    {
        $allIps = GetMgmtIpRangeFromPools -IpPools $IpPools

        $uniqueIPs = @{}

        $firstIp = $IpPools[0].StartingAddress
        $match =  $firstIp -replace "\.[0-9]{1,3}$", ""

        foreach ($ip in $allIps)
        {
            $ipString = $ip.ToString()
            if ($uniqueIPs.ContainsKey($ipString))
            {
                return $false
            }
            else
            {
                $uniqueIPs[$ipString] = $true
            }

            # Test to make sure all ips in the management subnet in the DHCP scenario
            $toMatch = $ip -replace "\.[0-9]{1,3}$", ""
            if ($toMatch -ne $match)
            {
                return $false
            }
        }

        # More reliable test to make sure all ips in the management pool in non-DHCP scenarios
        if (-not ([string]::IsNullOrEmpty($ManagementSubnetValue)))
        {
            foreach ($ipPool in $IpPools)
            {
                $StartingAddress = $ipPool.StartingAddress
                $EndingAddress = $ipPool.EndingAddress

                if (!(Check-IPInRange -IPAddress $StartingAddress -Range $ManagementSubnetValue))
                {
                    return $false
                }

                if (!(Check-IPInRange -IPAddress $EndingAddress -Range $ManagementSubnetValue))
                {
                    return $false
                }
            }
        }

        return $true
    }
    catch
    {
        throw "Failed to check ip pools. Error: $_"
    }
}

function Check-IPInRange {

    param(
        [Parameter(Mandatory=$true)]
        [string]
        $IPAddress,

        # Range in which to search using CIDR notation. (ippaddr/bits)
        [Parameter(Mandatory=$true)]
        [string]
        $Range
    )

    # Split range into the address and the CIDR notation
    [String]$CIDRAddress = $Range.Split('/')[0]
    [int]$CIDRBits       = $Range.Split('/')[1]

    # Address from range and the search address are converted to Int32 and the full mask is calculated from the CIDR notation.
    [int]$BaseAddress    = [System.BitConverter]::ToInt32((([System.Net.IPAddress]::Parse($CIDRAddress)).GetAddressBytes()), 0)
    [int]$Address        = [System.BitConverter]::ToInt32(([System.Net.IPAddress]::Parse($IPAddress).GetAddressBytes()), 0)
    [int]$Mask           = [System.Net.IPAddress]::HostToNetworkOrder(-1 -shl ( 32 - $CIDRBits))

    return (($BaseAddress -band $Mask) -eq ($Address -band $Mask))
}

function GetMgmtIpRangeFromPools
{
    param (
        [Parameter(Mandatory = $false, HelpMessage = "Specify starting Management IP Range")]
        [System.Collections.ArrayList]
        $IpPools
    )

    $result = @()

    foreach ($ipPool in $IpPools)
    {
        $result += GetMgmtIpRange -StartingAddress $ipPool.StartingAddress -EndingAddress $ipPool.EndingAddress
    }

    return $result
}

function GetMgmtIpRange
{
    param (
        [Parameter(Mandatory = $false, HelpMessage = "Specify starting Management IP Range")]
        [System.Net.IPAddress]
        $StartingAddress,

        [Parameter(Mandatory = $false, HelpMessage = "Specify end Management IP Range")]
        [System.Net.IPAddress]
        $EndingAddress
    )

    try
    {
        $first3 = $StartingAddress -replace "\.[0-9]{1,3}$", ""
        $start = $StartingAddress -split "\." | Select-Object -Last 1
        $end = $EndingAddress -split "\." | Select-Object -Last 1

        $range = $start..$end | ForEach-Object { ([System.Net.IPAddress]("{0}.{1}" -f $first3, $PSITEM)).IPAddressToString }
        Log-info "Start: $start and end: $end gives range: $($range -join ',')"
        return $range
    }
    catch
    {
        throw "Failed to get Mgmt range. Error: $_"
    }
}

function TestMgmtRangeSize
{
    <#
    .SYNOPSIS
        Ensure IP range is within boundaries.
    #>

    param (
        [Parameter(Mandatory = $false, HelpMessage = "Specify starting Management IP Range")]
        [System.Collections.ArrayList]
        $IpPools,

        [int]
        $Minimum = 6,

        [int]
        $Maximum = 16
    )

    try
    {
        $totalCount = 0
        foreach ($ipPool in $IpPools)
        {
            $StartingAddress = $ipPool.StartingAddress
            $EndingAddress = $ipPool.EndingAddress

            $start = $StartingAddress -split "\." | Select-Object -Last 1
            $end = $EndingAddress -split "\." | Select-Object -Last 1
            $hostCount = ($start..$end).count
            Log-info "Start: $start and end: $end gives host count: $hostcount"
            $totalCount += $hostCount
        }

        if ($totalCount -gt $Maximum -or $totalCount -lt $Minimum)
        {
            return $false
        }
        else
        {
            return $true
        }
    }
    catch
    {
        throw "Failed to check range size. Error: $_"
    }
}


function TestMgmtRangePoolCount
{
    <#
    .SYNOPSIS
        #1, either one single pool that is big enought (>= Minimum IPs) (for both DHCP and static scenario) <== for general customers , or
        #2, 2 pools with 1 pool having 1 IP and 2 pool have at least (Minimum - 1 IPs) (for non-DHCP scenario only) <== for specific customers?
    #>

    param (
        [Parameter(Mandatory = $false, HelpMessage = "Specify starting Management IP Range")]
        [System.Collections.ArrayList]
        $IpPools,

        [int]
        $Minimum = 5
    )

    try
    {
        $poolCount = $IpPools.Count

        if ($poolCount -gt 2)
        {
            Log-info "Found more than 2 IP pools. Test Failed"
            return $false
        }
        elseif ($poolCount -eq 1)
        {
            Log-info "Found only 1 IP Pool. Test Passed"
            return $true
        }
        else # 2 pools
        {
            $StartingAddress = $IpPools[0].StartingAddress
            $EndingAddress = $IpPools[0].EndingAddress

            $start = $StartingAddress -split "\." | Select-Object -Last 1
            $end = $EndingAddress -split "\." | Select-Object -Last 1
            $ipCount = ($start..$end).count
            Log-info "Start: $start and end: $end"

            if ($ipCount -ne 1)
            {
                Log-info "Found more than 1 ip in first IP pool. Test Failed"
                return $false
            }

            $StartingAddress = $IpPools[1].StartingAddress
            $EndingAddress = $IpPools[1].EndingAddress

            $start = $StartingAddress -split "\." | Select-Object -Last 1
            $end = $EndingAddress -split "\." | Select-Object -Last 1
            $ipCount = ($start..$end).count

            if ($ipCount -lt ($Minimum - 1))
            {
                Log-info "Found less then enough IPs in second IP pool. Test Failed"
                return $false
            }

            Log-info "Test Passed"
            return $true
        }
    }
    catch
    {
        throw "Failed to check IP Pool Count. Error: $_"
    }
}

function IsTcpPortInUse
{
    param(
        [System.Net.IPAddress]
        $Ip,

        [int]
        $Port = 5986,

        [int]
        $Timeout = 500
    )

    try
    {
        $tcpClient = New-Object System.Net.Sockets.TcpClient
        $portOpened = $tcpClient.ConnectAsync($ip, $p).Wait($timeout)
        $tcpClient.Dispose()
        return ($portOpened -contains $true)
    }
    catch
    {
        throw "Failed to check TCP ports. Error: $_"
    }
}

function TestNetworkIntentStatus
{
    <#
    .SYNOPSIS
        This test is run in the AddNode context only.
        This test validates if the intents configured on the existing cluster and the new node to be added are not in errored state.
 
    .DESCRIPTION
        This test performs the following Validations:
        1) Check the ATC Intent status on existing nodes are successfully allocated
        2) Check if NetworkATC service is running on the new node
        3) Check if the existing nodes have storage intent configured in them.
 
    .PARAMETERS
        [System.Management.Automation.Runspaces.PSSession[]] $PSSession
    #>

    [CmdletBinding()]
    param (
        [System.Management.Automation.Runspaces.PSSession[]]
        $PSSession
    )

    try
    {
        $sessionToCheck = $PSSession[0]
        Log-Info "Checking ATC Intent status on existing nodes and if NetworkATC service is running on the new node."
        $instanceResults = @()
        $AdditionalData = @()

        # Get the names of all nodes with an Up Status
        $activeNodes = (Get-ClusterNode | Where-Object {$_.State -eq "Up"}).Name
        Log-Info "Active nodes: $($activeNodes | Out-String)"

        # Get all intents on the active nodes
        $intents = Get-NetIntentStatus | Where-Object {$activeNodes -contains $_.Host}

        # Checks the intent status on the existing nodes.
        foreach ($intent in $intents)
        {
            $intentHealthy = $true
            if ($intent.ConfigurationStatus -ne "Success" -or $intent.ProvisioningStatus -ne "Completed")
            {
                $intentHealthy = $false
                $TestNetworkIntentStatusDetail = $lnTxt.TestNetworkIntentStatusFail -f $intent.Host, $intent.ConfigurationStatus, $intent.ProvisioningStatus
                Log-Info $TestNetworkIntentStatusDetail -Type Warning
            }
            else
            {
                $intentHealthy = $true
                $TestNetworkIntentStatusDetail = $lnTxt.TestNetworkIntentStatusPass -f $intent.Host, $intent.ConfigurationStatus, $intent.ProvisioningStatus
            }

            $params = @{
                Name               = 'AzStackHci_Network_Test_Network_AddNode_Intent_Status'
                Title              = 'Test Network intent on existing nodes'
                DisplayName        = 'Test Network intent on existing nodes'
                Severity           = 'CRITICAL'
                Description        = 'Checking if Network intent is unhealthy on existing nodes'
                Tags               = @{}
                Remediation        = 'https://learn.microsoft.com/en-us/azure-stack/hci/deploy/deployment-tool-checklist'
                TargetResourceID   = 'NetworkIntent'
                TargetResourceName = 'NetworkIntent'
                TargetResourceType = 'NetworkIntent'
                Timestamp          = [datetime]::UtcNow
                Status             = if ($intentHealthy) { 'SUCCESS' } else { 'FAILURE' }
                AdditionalData     = @{
                    Source    = $intent.Host
                    Resource  = 'AddNodeIntentStatusCheck'
                    Detail    = $TestNetworkIntentStatusDetail
                    Status    = if ($intentHealthy) { 'SUCCESS' } else { 'FAILURE' }
                    TimeStamp = [datetime]::UtcNow
                }
                HealthCheckSource  = $ENV:EnvChkrId
            }
            $instanceResults += New-AzStackHciResultObject @params
        }

        Log-Info "Checking if the storage intent is configured on the existing cluster before add node."

        $storageIntent = $intents | Where-Object {$_.IsStorageIntentSet -eq $true}

        try {
            $source = Get-Cluster
        }
        catch {
            $source = $Env:COMPUTERNAME
            Log-Info "Error getting the cluster, we could be running this test in standalone mode on $($source)"
        }

        if ($null -eq $storageIntent)
        {
            $TestNetworkIntentStatusDetail = $lnTxt.TestStorageIntentNotConfigured -f $source
            Log-Info $TestNetworkIntentStatusDetail -Type Warning
        }
        else
        {
            $TestNetworkIntentStatusDetail = $lnTxt.TestStorageIntentConfigured -f $source
            Log-Info $TestNetworkIntentStatusDetail -Type Success
        }

        $params = @{
            Name               = 'AzStackHci_Network_Test_Network_AddNode_Storage_Intent'
            Title              = 'Test Storage intent on existing nodes'
            DisplayName        = 'Test Storage intent on existing nodes'
            Severity           = 'CRITICAL'
            Description        = 'Check if the storage intent is configured on the existing cluster before add node'
            Tags               = @{}
            Remediation        = 'https://learn.microsoft.com/en-us/azure-stack/hci/deploy/deployment-tool-checklist'
            TargetResourceID   = 'StorageIntent'
            TargetResourceName = 'StorageIntent'
            TargetResourceType = 'StorageIntent'
            Timestamp          = [datetime]::UtcNow
            Status             = if ($null -eq $storageIntent) { 'FAILURE' } else { 'SUCCESS' }
            AdditionalData     = @{
                Source    = $source
                Resource  = 'AddNodeStorageIntentCheck'
                Detail    = $TestNetworkIntentStatusDetail
                Status    = if ($null -eq $storageIntent) { 'FAILURE' } else { 'SUCCESS' }
                TimeStamp = [datetime]::UtcNow
            }
            HealthCheckSource  = $ENV:EnvChkrId
        }
        $instanceResults += New-AzStackHciResultObject @params

        # Check if NetworkATC service is running on the new node
        $sb = {
            $retVal = New-Object psobject -Property @{
                Pass = $true
                Status = [string]::Empty
            }

            $atcFeature = Get-WindowsFeature -Name NetworkATC

            if ($atcFeature.Installstate -eq "Installed")
            {
                $atcService = Get-Service NetworkATC -ErrorAction SilentlyContinue
                $retVal.Status = "Feature Installed Service $($atcService.Status)"
            }
            elseif ($atcFeature.Installstate -eq "Available")
            {
                $retVal.Status = "Feature Available"
            }
            else
            {
                $retVal.Pass = $false
            }

            return $retVal
        }

        $NetworkATCStatus = Invoke-Command $sessionToCheck -ScriptBlock $sb
        $ATCStatusHealthy = $true
        if (!$NetworkATCStatus.Pass)
        {
            # NetworkATC feature not Installed, not Available on the system
            $ATCStatusHealthy = $false
            $TestNetworkATCServiceDetail = $lnTxt.TestNetworkATCFeatureNotInSystem -f $sessionToCheck.ComputerName
            Log-Info $TestNetworkATCServiceDetail -Type Warning
        }
        elseif (-not (($NetworkATCStatus.Status -eq 'Feature Installed Service Running') -or ($NetworkATCStatus.Status -eq 'Feature Available')))
        {
            # NetworkATC feature installed but service not 'Running', or feature not available
            $ATCStatusHealthy = $false
            $TestNetworkATCServiceDetail = $lnTxt.TestNetworkATCFeatureServiceStatus -f $NetworkATCStatus.Status, $sessionToCheck.ComputerName
            Log-Info $TestNetworkATCServiceDetail -Type Warning
        }
        else
        {
            $ATCStatusHealthy = $true
            $TestNetworkATCServiceDetail = $lnTxt.TestNetworkATCFeatureServiceStatus -f $NetworkATCStatus.Status, $sessionToCheck.ComputerName
            Log-Info $TestNetworkATCServiceDetail -Type Success
        }

        $params = @{
            Name               = 'AzStackHci_Network_Test_Network_AddNode_NetworkATC_Service'
            Title              = 'Test NetworkATC service is running on new node'
            DisplayName        = 'Test NetworkATC service is running on new node'
            Severity           = 'CRITICAL'
            Description        = 'Check NetworkATC service is running on new node'
            Tags               = @{}
            Remediation        = 'https://learn.microsoft.com/en-us/azure-stack/hci/deploy/deployment-tool-checklist'
            TargetResourceID   = 'NetworkATCService'
            TargetResourceName = 'NetworkATCService'
            TargetResourceType = 'NetworkATCService'
            Timestamp          = [datetime]::UtcNow
            Status             = if ($ATCStatusHealthy) { 'SUCCESS' } else { 'FAILURE' }
            AdditionalData     = @{
                Source    = $sessionToCheck.ComputerName
                Resource  = 'AddNodeNewNodeNetworkATCServiceCheck'
                Detail    = $TestNetworkATCServiceDetail
                Status    = if ($ATCStatusHealthy) { 'SUCCESS' } else { 'FAILURE' }
                TimeStamp = [datetime]::UtcNow
            }
            HealthCheckSource  = $ENV:EnvChkrId
        }
        $instanceResults += New-AzStackHciResultObject @params
        return $instanceResults
    }
    catch
    {
        throw $_
    }
}

function CheckNetAdapterRDMAStatus
{
    param (
        [PSObject[]] $IntentsInfoFromJson
    )

    $retVal = New-Object psobject -Property @{
        Pass = $true
        Message = "on $($ENV:COMPUTERNAME)"
    }

    enum NetworkDirectEnabledState { Disabled = 0; Enabled = 1 }

    # Read RDMA state info for all adapters
    [PSObject[]] $allAdapterRdmaInfo = Get-NetAdapterRdma

    [System.Boolean] $validSystemRdmaConfig = $true

    # need to check each adapter for all intents
    foreach ($currentIntent in $IntentsInfoFromJson)
    {
        [System.String[]] $adaptersToCheck = $currentIntent.Adapter
        [PSObject[]] $rdmaInfoForAdaptersToCheck = $allAdapterRdmaInfo | Where-Object { $_.Name -in $adaptersToCheck }

        [Boolean] $currentIntentAdapterOverride = $currentIntent.OverrideAdapterProperty
        [System.Int32] $currentIntentNetworkDirectOverride = 0
        if (-Not [System.String]::IsNullOrEmpty($currentIntent.AdapterPropertyOverrides.NetworkDirect))
        {
            $currentIntentNetworkDirectOverride = [System.Int32] [NetworkDirectEnabledState] $currentIntent.AdapterPropertyOverrides.NetworkDirect
        }
        $retVal.Message += "`n Intent $($currentIntent.Name) Adapter Override - [ $currentIntentAdapterOverride ]; NetworkDirect - [ $currentIntentNetworkDirectOverride ]"

        if ($rdmaInfoForAdaptersToCheck.Count -ne $adaptersToCheck.Count)
        {
            # End user provided adapter(s) that don't have RDMA support (a.k.a, Get-NetAdapterRdma returns nothing for the adapter)
            # So the intent adapter override should have NetworkDirect Disabled. Otherwise the configuration will fail the ATC configuration
            if (($currentIntentAdapterOverride -and $currentIntentNetworkDirectOverride -eq 0) -or ($rdmaInfoForAdaptersToCheck.Count -eq 0))
            {
                $retVal.Message += "`n Correct configuration for adapters $($adaptersToCheck | Out-String): RDMA not supported, and configured with intent adapter override to disable NetworkDirect"
                $retVal.Message += "`n Or Get-NetAdapterRdma call returned nothing for the adapter(s) $($adaptersToCheck | Out-String)"
            }
            else
            {
                $retVal.Message += "`n Wrong configuration for adapters $($adaptersToCheck | Out-String): RDMA not supported, but not configured with intent adapter override to disable NetworkDirect"
                $validSystemRdmaConfig = $false
            }
        }
        else
        {
            foreach ($currentRdmaInfo in $rdmaInfoForAdaptersToCheck)
            {
                # The following conditions are valid for RDMA configuration:
                # RDMA Enabled | RDMA OperationalStatus | Override | OverrideValue
                # True | True | - | -
                # - | False | True | 0
                $rdmaEnabled = $currentRdmaInfo.Enabled
                $rdmaOperationalState = $currentRdmaInfo.OperationalState

                $validRdmaForCurrentAdapter = ($rdmaEnabled -and $rdmaOperationalState) -or
                                            ((-not $rdmaOperationalState) -and $currentIntentAdapterOverride -and $currentIntentNetworkDirectOverride -eq 0)

                if (-not $validRdmaForCurrentAdapter)
                {
                    $retVal.Message += "`n Wrong configuration for adapter $($currentRdmaInfo.Name): RDMA Enabled - [ $rdmaEnabled ]; RDMA OperationalState - [ $rdmaOperationalState ]"
                }
                else
                {
                    $retVal.Message += "`n Correct configuration for adapter $($currentRdmaInfo.Name): RDMA Enabled - [ $rdmaEnabled ], RDMA OperationalState - [ $rdmaOperationalState ]"
                }

                $validSystemRdmaConfig = $validSystemRdmaConfig -and $validRdmaForCurrentAdapter
            }
        }
    }

    if (-not $validSystemRdmaConfig)
    {
        $retVal.Pass = $false
        $retVal.Message = "`nERROR: RDMA setting on adapters are invalid " + $retVal.Message
    }
    else
    {
        $retVal.Pass = $true
        $retVal.Message = "`nPASS: RDMA setting on adapters are valid " + $retVal.Message
    }

    return $retVal
}
function CheckAdapterSymmetryAndBandwidth
{
    param (
        [PSObject[]] $IntentsInfoFromJson
    )

    enum NetworkDirectEnabledState { Disabled = 0; Enabled = 1 }

    $nodeName = $env:COMPUTERNAME
    $retVal = New-Object psobject -Property @{
        Pass = $true
        Message = "on $($nodeName)`n"
    }

    [PSObject[]] $allAdapterInfo = Get-NetAdapter

    foreach ($currentIntent in $IntentsInfoFromJson)
    {
        [System.String[]] $adaptersToCheck = $currentIntent.Adapter

        $intentAdapterInfoToCheck = $allAdapterInfo | Where-Object { $_.Name -in $adaptersToCheck }

        # Check adapter symmetry
        $retVal.Message += "`n--- Adapter Symmetry Check: Link speed and Component ID should be same for all adapters in the intent"

        $compIDFail = $false
        $linkSpeedFail = $false
        $expectedSpeed = $null
        $expectedComponentID = $null

        foreach ($nicInfo in $intentAdapterInfoToCheck)
        {
            if ($null -eq $expectedSpeed)
            {
                $expectedSpeed = $nicInfo.Speed
            }

            if ($null -eq $expectedComponentID)
            {
                $expectedComponentID = $nicInfo.ComponentID
            }

            if ($expectedSpeed -ne $nicInfo.Speed)
            {
                $linkSpeedFail = $true
            }
            if ($expectedComponentID -ne $nicInfo.ComponentID)
            {
                $compIDFail = $true
            }

            if ($linkSpeedFail -Or $compIDFail)
            {
                $retVal.Pass = $false
            }

            $retVal.Message += "`n -- $nodeName ($($nicInfo.Name),`t$($nicInfo.LinkSpeed),`t$($nicInfo.ComponentID))"
        }

        # Check adapter bandwidth
        # This is needed if current intent is for storage traffic and adapter property is not overridden with NetworkDirect Disabled
        [Boolean] $currentIntentAdapterOverride = $currentIntent.OverrideAdapterProperty
        [System.Int32] $currentIntentNetworkDirectOverride = 0
        if (-Not [System.String]::IsNullOrEmpty($currentIntent.AdapterPropertyOverrides.NetworkDirect))
        {
            $currentIntentNetworkDirectOverride = [System.Int32] [NetworkDirectEnabledState] $currentIntent.AdapterPropertyOverrides.NetworkDirect
        }

        $needCheckBandwidth = $currentIntent.TrafficType.Contains("Storage") -and (-not $currentIntentAdapterOverride -or $currentIntentNetworkDirectOverride -ne 0)
        if ($needCheckBandwidth)
        {
            $retVal.Message += "`n--- Adapter Bandwidth Check for storage adapters when RDMA enabled: Need to be 10Gbps or higher"

            foreach ($nicInfo in $intentAdapterInfoToCheck)
            {
                if ($nicInfo.Speed)
                {
                    if ($nicInfo.Speed -lt 10000000000)
                    {
                        $retVal.Pass = $false
                    }

                    $retVal.Message += "`n -- $nodeName ($($nicInfo.Name),`t$($nicInfo.LinkSpeed))"
                }
                else
                {
                    $retVal.Pass = $false
                    $retVal.Message += "`n -- $nodeName ($($nicInfo.Name), Speed not available)"
                }
            }
        }
    }

    if ($retVal.Pass)
    {
        $retVal.Message = "`nPASS: Network adapter(s) are symmetric and meet bandwidth requirement " + $retVal.Message
    }
    else
    {
        $retVal.Message = "`nERROR: Network adapter(s) are not symmetric or do not meet bandwidth requirement " + $retVal.Message
    }

    return $retVal
}

function CheckHostNetworkConfigurationReadiness
{
    param
    (
        [PSObject[]] $IntentsInfoFromJson
    )

    $retVal = New-Object psobject -Property @{
        Pass = $true
        Message = "On $($ENV:COMPUTERNAME):"
    }

    [System.String[]] $intentAdapters = $IntentsInfoFromJson | ForEach-Object { $_.Adapter } | Select-Object -Unique

    [PSObject[]] $extSwitchInfo = @()

    if ((Get-Command Get-VMSwitch -ErrorAction SilentlyContinue) -and (Get-WindowsFeature -Name Hyper-V -ErrorAction SilentlyContinue).Installed)
    {
        $extSwitchInfo = Get-VMSwitch -SwitchType External
    }

    [System.String] $interimPassMessage = ""

    #region Check DNS client configuration
    [PSObject[]] $adapterDnsClientInfo = Get-DNSClient
    [System.String[]] $adpaterWithDNSClientInfo = $adapterDnsClientInfo.InterfaceAlias | Select-Object -Unique

    [System.String[]] $adaptersToCheck = @()

    if ($extSwitchInfo.Count -eq 0)
    {
        # In case there is no VMSwitch in the system, we will need to make sure all adapters used in intents are in the result of Get-DNSClient
        $adaptersToCheck = $intentAdapters
    }
    else
    {
        # if there is a VMSwitch, we will need to make sure that those adapters not in VMSwitch but in intent are in the result of Get-DNSClient
        [System.Guid[]] $switchAdapterGuids = $extSwitchInfo | ForEach-Object { $_.NetAdapterInterfaceGuid }
        [System.String[]] $adaptersNotInVMSwitchNames = Get-NetAdapter -Physical | Where-Object { $_.InterfaceGuid -notin $switchAdapterGuids } | ForEach-Object { $_.Name }
        $adaptersToCheck = $intentAdapters | Where-Object { $_ -in $adaptersNotInVMSwitchNames }
    }

    if ($adaptersToCheck.Count -eq 0)
    {
        #This means all the adapters defined in intent are used in VMSwitch
        $intentAdapterMissingDnsClient = $null
    }
    else
    {
        $intentAdapterMissingDnsClient = Compare-Object $adaptersToCheck $adpaterWithDNSClientInfo | Where-Object { $_.SideIndicator -eq "<=" } | ForEach-Object { $_.InputObject }
    }

    if ($intentAdapterMissingDnsClient.Count -gt 0)
    {
        $retVal.Pass = $false
        $retVal.Message += "`nERROR: DNS Client configuration is missing for the following adapter(s): $($intentAdapterMissingDnsClient -join ', ')"

        # in case of failure, return directly
        return $retVal
    }
    else
    {
        $interimPassMessage += "`nPASS: DNS Client configuration has valid data for all adapters defined in intent"
    }
    #endregion

    #region Check Hyper-V running status by calling Get-VMHost
    if ((Get-Command Get-VMHost -ErrorAction SilentlyContinue) -and (Get-WindowsFeature -Name Hyper-V -ErrorAction SilentlyContinue).Installed)
    {
        [PSObject[]] $vmHostInfo = Get-VMHost -ErrorAction SilentlyContinue
        if ($vmHostInfo.Count -eq 0)
        {
            $retVal.Pass = $false
            $retVal.Message += "`nERROR: Hyper-V is not running correctly on the system"

            # in case of failure, return directly
            return $retVal
        }
        else
        {
            $interimPassMessage += "`nPASS: Hyper-V is running correctly on the system"
        }
    }
    else
    {
        $interimPassMessage += "`nWARNING: Hyper-V-PowerShell might not installed correctly on the system. Will skip VM host check."
    }
    #endregion

    #region Check VMSwitch readiness
    # At leas 1 VMSwitch is having the network adapter defined in the management intent
    # Or management intent adapters are not included in any VMSwitch
    if ($extSwitchInfo.Count -ge 1)
    {
        [System.String[]] $mgmtIntentAdapterNames = $IntentsInfoFromJson | Where-Object { $_.TrafficType.Contains("Management") } | ForEach-Object { $_.Adapter } | Select-Object -Unique

        [System.Boolean] $foundMgmtVMSwitch = $false
        foreach ($currentSwitchInfo in $extSwitchInfo)
        {
            [System.Guid[]] $currentSwitchAdapterGuids = $currentSwitchInfo | ForEach-Object { $_.NetAdapterInterfaceGuid }
            [System.String[]] $currentSwitchAdapterNames = Get-NetAdapter -Physical | Where-Object { $_.InterfaceGuid -in $currentSwitchAdapterGuids } | ForEach-Object { $_.Name }

            $tempRst = Compare-Object $mgmtIntentAdapterNames $currentSwitchAdapterNames | Where-Object { $_.SideIndicator -eq "<=" } | ForEach-Object { $_.InputObject }
            if ($tempRst.Count -eq 0)
            {
                $foundMgmtVMSwitch = $true
                break
            }
        }

        if ($foundMgmtVMSwitch)
        {
            $interimPassMessage += "`nPASS: At least 1 VMSwitch is having the network adapter defined in the management intent"
        }
        else
        {
            $retVal.Pass = $false
            $retVal.Message += "`nERROR: No VMSwitch is having the network adapter defined in the management intent"

            # in case of failure, return directly
            return $retVal
        }
    }
    #endregion

    #Region Check advanced property VlanId on adapters
    foreach ($pNIC in $intentAdapters)
    {
        $currentAdapterAdvancedPropertyVlanId = Get-NetAdapterAdvancedProperty -Name $pNIC -RegistryKeyword VlanId -ErrorAction SilentlyContinue

        if (($null -eq $currentAdapterAdvancedPropertyVlanId) -or ($null -eq $currentAdapterAdvancedPropertyVlanId.RegistryValue))
        {
            $retVal.Pass = $false
            $retVal.Message += "`nERROR: Cannot find valid advanced property VlanId for adapter $pNIC. Use Get-NetAdapterAdvancedProperty/Set-NetAdapterAdvancedProperty with parameter RegistryKeyword set to VlanId to verify and configure it."

            # in case of failure, return directly
            return $retVal
        }
    }

    $interimPassMessage += "`nPASS: Advanced property VlanId for all adapters defined in intent are correct"
    #endregion

    #region Check pNIC are in the intent adapters
    [System.String[]] $allpNicUpInSystem = Get-NetAdapter -Physical -Name $intentAdapters -ErrorAction SilentlyContinue | Where-Object { $_.Status -eq "Up" } | ForEach-Object { $_.Name }

    $adapterCompareResult = Compare-Object $intentAdapters $allpNicUpInSystem | Where-Object { $_.SideIndicator -eq "<=" } | ForEach-Object { $_.InputObject }
    if ($adapterCompareResult.Count -gt 0)
    {
        $retVal.Pass = $false
        $retVal.Message += "`nERROR: The following adapter(s) are not physical adapter or not Up in the system: $($adapterCompareResult -join ', '). Intent adapters should be physical adapters and Up in the system."

        # in case of failure, return directly
        return $retVal
    }
    else
    {
        $interimPassMessage += "`nPASS: All adapters defined in intent are physical NICs and Up in the system"
    }
    #endregion

    $retVal.Message += $interimPassMessage
    return $retVal
}

function ConfigureVMSwitchForTesting
{
    [CmdletBinding()]
    param
    (
        [System.String[]] $SwitchAdapterNames,
        [System.String] $MgmtIntentName = "",
        [System.Boolean] $UpdateMgmtAdapter = $true,
        [System.String] $ExpectedVMSwitchName = "ConvergedSwitch($($MgmtIntentName))",
        [System.String] $ExpectedMgmtVNicName = "vManagement($($MgmtIntentName))"
    )

    [PSObject] $retVal = New-Object PSObject -Property @{
        VMSwitchInfo = $null
        MgmtVlanId = 0
        NeedCleanUp = $false
        IPReady = $false
    }

    $tmpVMSwitch = New-VMSwitch -Name $ExpectedVMSwitchName -NetAdapterName $SwitchAdapterNames -EnableEmbeddedTeaming $true -AllowManagementOS $UpdateMgmtAdapter

    if ($tmpVMSwitch)
    {
        $retVal.VMSwitchInfo = $tmpVMSwitch
        $retVal.MgmtVlanId = 0
        $retVal.NeedCleanUp = $true

        if ($UpdateMgmtAdapter)
        {
            $mgmtVlanId = 0
            $existingPNICVlanId = Get-NetAdapterAdvancedProperty -RegistryKeyword VlanID -Name $SwitchAdapterNames[0] -ErrorAction SilentlyContinue

            if ($existingPNICVlanId -and $existingPNICVlanId.RegistryValue)
            {
                $mgmtVlanId = $existingPNICVlanId.RegistryValue[0]
            }

            Rename-VMNetworkAdapter -ManagementOS -Name $ExpectedVMSwitchName -NewName $ExpectedMgmtVNicName
            Get-NetAdapter -name "vEthernet ($($ExpectedMgmtVNicName))" -ErrorAction SilentlyContinue | Rename-NetAdapter -NewName $ExpectedMgmtVNicName

            if ($mgmtVlanId -ne 0)
            {
                $retVal.MgmtVlanId = $mgmtVlanId

                Set-VMNetworkAdapterIsolation -ManagementOS `
                                            -VMNetworkAdapterName $ExpectedMgmtVNicName `
                                            -IsolationMode Vlan `
                                            -AllowUntaggedTraffic $true `
                                            -DefaultIsolationID $mgmtVlanId
            }

            # In case of DHCP scenario, the new adapter might not get the IP address immediately
            # Wait for some time (60 seconds) to make sure the new IP is settled correctly.
            [System.Boolean] $currentIPReady = $false
            $ipStopWatch = [System.diagnostics.stopwatch]::StartNew()
            while (-not $currentIPReady -and ($ipStopWatch.Elapsed.TotalSeconds -lt 60))
            {
                # If the vNIC has Manual or Dhcp IPv4 address with "Preferred" state, we consider it as "ready"
                $ipConfig = Get-NetIPAddress -InterfaceAlias $ExpectedMgmtVNicName | Where-Object { ($_.PrefixOrigin -eq "Manual" -or $_.PrefixOrigin -eq "Dhcp") -and $_.AddressFamily -eq "IPv4" -and $_.AddressState -eq "Preferred" }

                if ($ipConfig)
                {
                    $currentIPReady = $true
                    $retVal.IPReady = $true
                    break
                }
                else
                {
                    Start-Sleep -Seconds 3
                }
            }

            if (-not $currentIPReady)
            {
                # should not get into here, but keep it here for safety
                Write-Host "Cannot get the IP address bind to the vNIC after VMSwitch created. Please check the system manually."
            }
            else
            {
                Write-Host "VMSwitch created successfully. VMSwitch: $($ExpectedVMSwitchName), MgmtVNic: $($ExpectedMgmtVNicName)"
            }
        }
    }

    return $retVal
}

function New-PsSessionWithRetriesInternal
{
    param
    (
        [System.String] $Node,
        [PSCredential] $Credential,
        [System.Int16] $Retries = 60,
        [System.Int16] $WaitSeconds = 10
    )

    for ($i=1; $i -le $Retries; $i++)
    {
        try
        {
            Trace-Execution "Creating PsSession ($i/$Retries) to $Node as $($Credential.UserName)..."
            $psSessionCreated = Microsoft.PowerShell.Core\New-PSSession -ComputerName $Node -Credential $Credential -ErrorAction Stop
            $computerNameFromSession = Microsoft.PowerShell.Core\Invoke-Command -Session $psSessionCreated -ScriptBlock { $ENV:COMPUTERNAME } -ErrorAction Stop
            $isAdminSession = Microsoft.PowerShell.Core\Invoke-Command -Session $psSessionCreated -ScriptBlock {
                ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')
            } -ErrorAction Stop

            if (-not $isAdminSession)
            {
                throw ("PsSession was successful but user: {0} is not an administrator on computer {1} " -f $psSessionCreated.Runspace.ConnectionInfo.Credential.Username, $computerName)
            }

            break
        }
        catch
        {
            Trace-Execution "Creating PsSession ($i/$Retries) to $Node failed: $($_.exception.message)"
            $errMsg = $_.tostring()
            Start-Sleep -Seconds $WaitSeconds
        }
    }

    if ($psSessionCreated -and $computerNameFromSession -and $isAdminSession)
    {
        Trace-Execution ("PsSession to {0} created after {1} retries. (Remote machine name: {2})" -f $Node, ("$i/$retries"), $computerName)
        return $psSessionCreated
    }
    else
    {
        throw "Unable to create a valid session to $Node`: $errMsg"
    }
}

function GetSortedMgmtIntentAdapter
{
    param
    (
        [System.String[]] $MgmtAdapterNames
    )

    Log-Info "Make sure 1st mgmt intent adapter is the one with valid IP address in it"
    Log-Info "$($MgmtAdapterNames | Out-String)"

    # Re-arrange the order in $MgmtAdapterNames to make sure the nic having a valid IPv4 address appears before the other NIC in the array
    $mgmtNicNamesTemp = [System.Collections.ArrayList] $MgmtAdapterNames

    foreach($name in $MgmtAdapterNames)
    {
        $a = Get-NetIPAddress -InterfaceAlias $name -AddressFamily ipv4 -Type Unicast -AddressState Preferred -PrefixOrigin Dhcp -ErrorAction SilentlyContinue
        $b = Get-NetIPAddress -InterfaceAlias $name -AddressFamily ipv4 -Type Unicast -AddressState Preferred -PrefixOrigin Manual -ErrorAction SilentlyContinue
        if (($null -ne $a) -or ($null -ne $b))
        {
            # move the NIC name to the top
            $mgmtNicNamesTemp.Remove($name)
            $mgmtNicNamesTemp.Insert(0, $name)
            break
        }
    }

    [System.String[]] $retVal = [System.String[]] $mgmtNicNamesTemp

    Log-Info "Got sorted adapters list:"
    Log-Info "$($retVal | Out-String)"

    return $retVal
}

function CheckStorageAdapterReadiness
{
    param ([String[]] $AdaptersToCheck)

    $retVal = New-Object PSObject -Property @{
        Pass = $true
        Message = "Storage adapter IP and VLAN check on $($ENV:COMPUTERNAME)"
    }

    foreach ($expectedAdapter in $AdaptersToCheck)
    {
        [PSObject[]] $currentAdapterInfo = Get-NetAdapter -Physical -Name $expectedAdapter -ErrorAction SilentlyContinue
        if ($currentAdapterInfo.Count -ne 1)
        {
            $retVal.Pass = $false
            $retVal.Message  += "`n !! Expect [ 1 ] adapter with name [ $expectedAdapter ]. But found [ $($currentAdapterInfo.Count) ]."
            $retVal.Message  += "`n !! Get-NetAdapter -Physical -Name $($expectedAdapter)"
        }
        else
        {
            [PSObject[]] $currentAdapterIPAddressInfo = Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $currentAdapterInfo.InterfaceIndex -PrefixOrigin @("Dhcp", "Manual") -ErrorAction SilentlyContinue

            if ($currentAdapterIPAddressInfo.Count -ne 0)
            {
                $retVal.Pass = $false
                $retVal.Message += "`n !! Found valid Dhcp/Manual IP address(es) [ $($currentAdapterIPAddressInfo.IPAddress) ] on storage adapter [ $expectedAdapter ]. Please remove the IP from the adapter and disable DHCP on it."
                $retVal.Message += "`n !! Set-NetIPInterface -InterfaceIndex $($currentAdapterInfo.InterfaceIndex) -Dhcp Disabled"
                $retVal.Message += "`n !! Remove-NetIPAddress -InterfaceIndex $($currentAdapterInfo.InterfaceIndex)"
            }
            else
            {
                $retVal.Message += "`n Passed: Storage adapter [ $expectedAdapter ] don't have any IP address on it."
            }

            [String[]] $currentAdapterVlanInfo = (Get-NetAdapterAdvancedProperty -Name $expectedAdapter -RegistryKeyword VLANID -ErrorAction SilentlyContinue).RegistryValue

            if (-not $currentAdapterVlanInfo)
            {
                $retVal.Pass = $false
                $retVal.Message += "`n !! Cannot get info of advanced property `"VLANID`" from adapter [ $expectedAdapter ]. Please make sure the adapter support VLANID."
                $retVal.Message += "`n !! Get-NetAdapterAdvancedProperty -Name $($expectedAdapter)"
            }
            else
            {
                if (($currentAdapterVlanInfo.Count -gt 1) -or ($currentAdapterVlanInfo[0] -ne "0"))
                {
                    $retVal.Pass = $false
                    $retVal.Message += "`n !! Found VLANID [ $($currentAdapterVlanInfo) ] on [ $expectedAdapter ]. Please remove the VLANID from the adapter"
                    $retVal.Message += "`n !! Set-NetAdapterAdvancedProperty -Name $($expectedAdapter) -RegistryKeyword VLANID -RegistryValue 0"
                }
                else
                {
                    $retVal.Message += "`n Passed: Storage adapter [ $expectedAdapter ] don't have any VLANID configured on it."
                }
            }
        }
    }

    return $retVal
}

function ConfigureStorageVMSwitchVNICForTesting
{
    [CmdletBinding()]
    param
    (
        [System.String] $StorageVMSwitchAdapter,
        [System.String] $StorageVMSwitchName,
        [System.String[]] $StorageVNICNames,
        [System.Collections.Hashtable] $StorageAdapterVLANIDInfo,
        [System.Boolean] $IncludeMgmt = $false
    )

    $tmpRst = ConfigureVMSwitchForTesting -SwitchAdapterNames @($StorageVMSwitchAdapter) -UpdateMgmtAdapter $IncludeMgmt -ExpectedVMSwitchName $StorageVMSwitchName

    CreateStorageVNIC -StorageVMSwitchName $tmpRst.VMSwitchInfo.Name `
                    -StorageVNICNames $StorageVNICNames `
                    -StorageAdapterVLANIDInfo $StorageAdapterVLANIDInfo

    return $tmpRst
}

function DecodeTestClusterStatus($RawStatus)
{
    New-Object -TypeName PSObject @{
        'Completed'     = ($RawStatus -band 0x1) -ne 0;
        'HasUnselected' = ($RawStatus -band 0x2) -ne 0;
        'HasFailures'   = ($RawStatus -band 0x4) -ne 0;
        'HasWarnings'   = ($RawStatus -band 0x8) -ne 0;
        'Cancel'        = ($RawStatus -band 0x10) -ne 0;
        'NotApplicable' = ($RawStatus -band 0x40) -ne 0
    }
}

function ValidateStorageConnections
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ParameterSetName = 'TESTCLUSTER', HelpMessage = "Specify String array of all nodes to be tested in Test-Cluster call.")]
        [String[]] $AllNodeNames,

        [Parameter(Mandatory = $true, ParameterSetName = 'PINGMESH', HelpMessage = "Specify PSSession array of all validation session needed for ping mesh testing.")]
        [System.Management.Automation.Runspaces.PSSession[]] $AllNodeSessions,

        [Parameter(Mandatory = $true, ParameterSetName = 'PINGMESH', HelpMessage = "Specify host IPV4 table of all nodes.")]
        [System.Collections.Hashtable] $HostIPv4Table
    )

    $retVal = @()

    switch ($PSCmdlet.ParameterSetName)
    {
        "TESTCLUSTER"
        {
            Log-Info "Use Test-Cluster to check storage adapter connections."

            $validationRst = New-Object PSObject -Property @{
                Pass = $true
                Message = "Storage adapter connection validation using Test-Cluster on $($AllNodeNames)"
            }

            # Need to run Test-Cluster on the current machine
            [System.String] $TestClusterReportPath = "C:\Windows\Cluster\Reports"

            $dateSuffix = [DateTime]::Now.ToString("yy-MM-dd.HH-mm-ss")
            [System.String] $reportFileName = "EnvValidatorStorageConnectionValidation$dateSuffix"

            Log-Info "Run [ Test-Cluster -Node $($AllNodeNames) -ReportName $($reportFileName) -Include `"Network`" ]"

            # Run Test-Cluster. Make sure output is not displayed, otherwise it will be captured by the result analyzer and cause error.
            Test-Cluster -Node $AllNodeNames -ReportName $reportFileName -Include "Network" | Out-Null

            # Log files from Test-Cluster are written into C:\Windows\Cluster\Reports. We need to parse the latest file "Validation Data for Node*", which contains the status.
            Log-Info "Test-Cluster log saved under [ $($TestClusterReportPath) ]. Status log file name is like `"Validation Data For Node*.xml`""
            $testClusterReportXml = (Get-ChildItem -Path $TestClusterReportPath -Filter "Validation Data For Node*.xml" | Sort-Object LastWriteTime | Select-Object -Last 1).FullName
            [XML]$testClusterValidationXML = Get-Content -Path $testClusterReportXml

            [int]$testClusterStatus = $testClusterValidationXML.Report.Channel.ValidationResult.Value.InnerText
            [PSObject] $decodedTestClusterStatus = DecodeTestClusterStatus -RawStatus $testClusterStatus

            if ($decodedTestClusterStatus.Completed -and $decodedTestClusterStatus.HasFailures)
            {
                Log-Info "Cluster Validation failed with errror status $testClusterStatus" -Type "WARNING"
                $validationRst.Pass = $false
                $validationRst.Message += "`nCluster validation failed with error status $decodedTestClusterStatus."
            }
            elseif ($decodedTestClusterStatus.Completed -and $decodedTestClusterStatus.HasWarnings)
            {
                Log-Info "Cluster validation completed with warnings." -Type "WARNING"
                Log-Info "Refer to the validation report $($reportFileName).htm for more information." -Type "WARNING"
                $validationRst.Message += "`nCluster validation finished with warning status $decodedTestClusterStatus."
            }
            elseif ($decodedTestClusterStatus.Completed -and ($decodedTestClusterStatus.HasUnselected -or $decodedTestClusterStatus.Cancel -or $decodedTestClusterStatus.NotApplicable))
            {
                Log-Info "Cluster validation completed, but had a few tests either unselected/cancelled/deemed not applicable. Refer to the validation report $($reportFileName).htm for more information." -Type "WARNING"
                $validationRst.Message += "`nCluster validation finished with status $decodedTestClusterStatus."
            }
            elseif ($decodedTestClusterStatus.Completed)
            {
                Log-Info "Cluster validation succeeded!"
                $validationRst.Message += "`nCluster validation succeeded!"
            }
            else
            {
                Log-Info "Cluster validation failed with unknown status $testClusterStatus" -Type "WARNING"
                $validationRst.Pass = $false
                $validationRst.Message += "`nCluster validation finished with unknown status $decodedTestClusterStatus."
            }

            Log-Info "Cluster validation detail report saved as: $($reportFileName).htm under $($TestClusterReportPath)"
            $validationRst.Message += "`nCheck cluster validation report $($reportFileName).htm under $($TestClusterReportPath) for detail information."

            $validationRstStatus = if ($validationRst.Pass) { 'SUCCESS' } else { 'FAILURE' }

            $storageAdapterConnectionRstObject = @{
                Name               = "AzStackHci_Network_Test_StorageConnections_Test_Cluster"
                Title              = "Test storage adapter connections using Test-Cluster on $($env:COMPUTERNAME)"
                DisplayName        = "Test storage adapter connections using Test-Cluster on $($env:COMPUTERNAME)"
                Severity           = 'WARNING'
                Description        = "Run Test-Cluster -Include Network on $($env:COMPUTERNAME) to validate storage adapter connections"
                Tags               = @{}
                Remediation        = "Check Test-Cluster report $($reportFileName).htm under $($TestClusterReportPath) for more information"
                TargetResourceID   = 'ValidateStorageConnectionsTestCluster'
                TargetResourceName = 'ValidateStorageConnectionsTestCluster'
                TargetResourceType = 'ValidateStorageConnectionsTestCluster'
                Timestamp          = [datetime]::UtcNow
                Status             = $validationRstStatus
                AdditionalData     = @{
                    Source    = $env:COMPUTERNAME
                    Resource  = 'ValidateStorageConnectionsTestCluster'
                    Detail    = $validationRst.Message
                    Status    = $validationRstStatus
                    TimeStamp = [datetime]::UtcNow
                }
                HealthCheckSource  = $ENV:EnvChkrId
            }

            $retVal += New-AzStackHciResultObject @storageAdapterConnectionRstObject
        }
        "PINGMESH"
        {
            Log-Info "Use ping mesh to check storage adapter connections."

            foreach ($sourceMachineSession in $AllNodeSessions)
            {
                $validationRst = New-Object PSObject -Property @{
                    Pass = $true
                    Message = "Storage adapter connection validation using ping test on $($sourceMachineSession.ComputerName)."
                }

                Log-Info "Run ping mesh on $($sourceMachineSession.ComputerName)."
                $nodePingRst = Invoke-Command -Session $sourceMachineSession -ScriptBlock {
                    param ($HostAdapterIPv4Table, $SourceNodeKey)

                    $pingRst = New-Object PSObject -Property @{
                        Pass = $true
                        Message = "Ping result from $($ENV:COMPUTERNAME) [ $($SourceNodeKey) ] to other host(s):"
                    }

                    $sourceAdapterIPv4 = $HostAdapterIPv4Table[$SourceNodeKey]

                    foreach ($destHost in $HostAdapterIPv4Table.Keys)
                    {
                        if ($destHost -ne $SourceNodeKey)
                        {
                            $pingRst.Message += "`n === Destination host [ $($destHost) ]"
                            $destAdapterIPv4 = $HostAdapterIPv4Table[$destHost]

                            foreach ($adapter in $sourceAdapterIPv4.Keys)
                            {
                                $pingRst.Message += "`n adapter [ $($adapter) ]"

                                $sourceIp = $sourceAdapterIPv4[$adapter]
                                $destIp = $destAdapterIPv4[$adapter]

                                # Check ping result: if contains string like "Reploy from 169.254.123.12: bytes=", then it's successful
                                $output = ping $destIp -S $sourceIp -n 2
                                $success = $output | Select-String "Reply from $($destIp): bytes=" -Quiet

                                if (-not $success)
                                {
                                    $pingRst.Pass = $false
                                    $pingRst.Message += "`n !!! FAILED: Ping from [ $sourceIp ] to [ $destIp ] !!!"
                                }
                                else
                                {
                                    $pingRst.Message += "`n PASSED: Ping from [ $sourceIp ] to [ $destIp ]"
                                }
                            }
                        }
                    }

                    return $pingRst
                } -ArgumentList $HostIPv4Table, $sourceMachineSession.ComputerName

                if (-not $nodePingRst.Pass)
                {
                    Log-Info " Ping test failed on $($sourceMachineSession.ComputerName)."
                    $validationRst.Pass = $false
                }
                else
                {
                    Log-Info " Ping test passed on $($sourceMachineSession.ComputerName)."
                }

                $validationRst.Message += "`n$($nodePingRst.Message)"
                $validationRstStatus = if ($validationRst.Pass) { 'SUCCESS' } else { 'FAILURE' }

                $storageAdapterConnectionRstObject = @{
                    Name               = "AzStackHci_Network_Test_StorageConnections_PING_MESH"
                    Title              = "Test storage adapter connections with ping mesh on $($sourceMachineSession.ComputerName)"
                    DisplayName        = "Test storage adapter connections with ping mesh on $($sourceMachineSession.ComputerName)"
                    Severity           = 'WARNING'
                    Description        = "Run ping mesh to validate storage adapter connections from $($sourceMachineSession.ComputerName) to other hosts"
                    Tags               = @{}
                    Remediation        = "Make sure network used by storage adapter is configured correctly so the adapters could be pinged each other"
                    TargetResourceID   = 'ValidateStorageConnectionsPingMesh'
                    TargetResourceName = 'ValidateStorageConnectionsPingMesh'
                    TargetResourceType = 'ValidateStorageConnectionsPingMesh'
                    Timestamp          = [datetime]::UtcNow
                    Status             = $validationRstStatus
                    AdditionalData     = @{
                        Source    = $sourceMachineSession.ComputerName
                        Resource  = 'ValidateStorageConnectionsPingMesh'
                        Detail    = $validationRst.Message
                        Status    = $validationRstStatus
                        TimeStamp = [datetime]::UtcNow
                    }
                    HealthCheckSource  = $ENV:EnvChkrId
                }

                $retVal += New-AzStackHciResultObject @storageAdapterConnectionRstObject

                Log-Info " Finsihed ping test on $($sourceMachineSession.ComputerName)."
            }
        }
        Default
        {
            # Should not get into here
            Log-Info "Not supported validation type: $($PSCmdlet.ParameterSetName)"
        }
    }

    return $retVal
}

function CreateStorageVNIC
{
    [CmdletBinding()]
    param (
        [System.String] $StorageVMSwitchName,
        [System.String[]] $StorageVNICNames,
        [System.Collections.Hashtable] $StorageAdapterVLANIDInfo
    )

    foreach ($tmpStorageVNIC in $StorageVNICNames)
    {
        Add-VMNetworkAdapter -ManagementOS -SwitchName $StorageVMSwitchName -Name $tmpStorageVNIC
        Get-NetAdapter -name "vEthernet ($($tmpStorageVNIC))" -ErrorAction SilentlyContinue | Rename-NetAdapter -NewName $tmpStorageVNIC
        Set-DnsClient -InterfaceAlias $tmpStorageVNIC -RegisterThisConnectionsAddress $false
        Set-VMNetworkAdapterIsolation -ManagementOS `
                                    -VMNetworkAdapterName $tmpStorageVNIC `
                                    -IsolationMode Vlan `
                                    -AllowUntaggedTraffic $true `
                                    -DefaultIsolationID $StorageAdapterVLANIDInfo[$tmpStorageVNIC]
    }
}

# SIG # Begin signature block
# MIIoLQYJKoZIhvcNAQcCoIIoHjCCKBoCAQExDzANBglghkgBZQMEAgEFADB5Bgor
# BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
# KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCC4I0+vBXdheRq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