Public/Network/Test-DHCPRequest.ps1

FUNCTION Test-DHCPRequest {
    
    
    [CmdletBinding()]
    PARAM ( 
        [INT]$Timeout = 15,
        [SWITCH]$Raw    
    )

    $ReturnResults = @()
    $ResultsSplit = @()
    $ResultsString = ""
    $AppDownloadLink = "https://integristech-my.sharepoint.com/:u:/g/personal/david_mcvicker_integrisit_com/ERuMHiTOWuZLlBsLpHU6uIABErsh9AyhbeT2dNHAppKzcA?Download=1"
    $AppPath = "C:\Program Files\IntegrisPowerShell\Utilities\DHCPTest\dhcptest-0.9-win64.exe"

    IF (Test-IntegrisFileDependency $AppDownloadLink -RootPath "$ModuleUtilityDir\DHCPTest" -ChildPath "\dhcptest-0.9-win64.exe" -Force) {
        $Results = & "$AppPath" --query --wait --quiet --timeout $Timeout

        FOREACH ($Result in $Results) { $ResultsString += $Result + [Environment]::NewLine }

        $ResultsSplit = @()
        $ResultsSplit = @($ResultsString -split "op=BOOTREPLY")
        $ResultsSplit = @($ResultsSplit | Where-Object { $_ -ne "" -and $_ -ne " " -and $_ -ne " " -and $_ -ne " " -and $_ -ne $null })
        $Count = 0 
        FOREACH ($Item in $ResultsSplit) { 
            $ResultsSplit[$Count] = " op=BOOTREPLY" + $Item
            $Count++
        }

        IF ($Raw) {
            RETURN $ResultsSplit
        }
        

        FOREACH ($Item in $ResultsSplit) { 
            $Item = $Item.Replace(" ","") 
            $Item = $Item.Replace(" ","") 
            $Item = $Item.Replace(" ","") 
            $Item = $Item.Replace(" ","") 
            $Item = $Item.Replace(" ","") 
            $Item = $Item.Replace(" ","") 
            $MessageType = "Offer"
            $DHCPServer = (($Item -split [Environment]::NewLine | Select-String "Server Identifier") -split " ")[3]
            $IP = ((($Item -split [Environment]::NewLine | Select-String "yiaddr") -split "=")[2] -split " ")[0]
            $SubnetMask = (($Item -split [Environment]::NewLine | Select-String "subnet mask") -split " ")[3]
            $Gateway = (($Item -split [Environment]::NewLine | Select-String "router option") -split " ")[3]
            $DNSServer = (((($Item -split [Environment]::NewLine | Select-String "domain name server") -split ":")[1]).Replace(" ","")) -split ","
            $LeaseTime = (($Item -split [Environment]::NewLine | Select-String "Lease") -split " ")[5]

            $ReturnResults += [PSCustomObject]@{
                PSTypeName = 'IntegrisPowerShell.TestDHCPRequest'
                MessageType = $MessageType
                DHCPServer = $DHCPServer
                IP = $IP
                SubnetMask = $SubnetMask       
                Gateway = $Gateway
                DNSServer = $DNSServer
                LeaseTime = $LeaseTime
            }

        }
        
        RETURN $ReturnResults

    }
    ELSE { 
        Write-Warning "DHCPtest exe not found. Please confirm security apps are not blocking the download and try again." 
        RETURN $null
    }
}