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(" ","") 
            TRY { $MessageType = (($Item -split [Environment]::NewLine | Select-String "DHCP Message Type") -split " ")[4] } CATCH { $MessageType = $null } 
            TRY { $DHCPServer = (($Item -split [Environment]::NewLine | Select-String "Server Identifier") -split " ")[3] } CATCH { $DHCPServer = $null } 
            TRY { $IP = ((($Item -split [Environment]::NewLine | Select-String "yiaddr") -split "=")[2] -split " ")[0] } CATCH { $IP = $null } 
            TRY { $SubnetMask = (($Item -split [Environment]::NewLine | Select-String "subnet mask") -split " ")[3] } CATCH { $SubnetMask = $null } 
            TRY { $Gateway = (($Item -split [Environment]::NewLine | Select-String "router option") -split " ")[3] } CATCH { $Gateway = $null } 
            TRY { $DNSServer = (((($Item -split [Environment]::NewLine | Select-String "domain name server") -split ":")[1]).Replace(" ","")) -split "," } CATCH { $DNSServer = $null } 
            TRY { $LeaseTime = (($Item -split [Environment]::NewLine | Select-String "Lease") -split " ")[5] } CATCH { $LeaseTime = $null } 

            $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
    }
}