Finally got the network check working

This commit is contained in:
camoy 2024-11-05 21:29:09 -08:00
parent 1cf57b991e
commit a7ba75fbd2

View File

@ -1,80 +1,87 @@
Function fDefaultGatewayCheck() {
Write-Host "---Determine Default Gateway---"
#This doesn't put out a $null value when no data is returned!!! (i.e. no network connection, all network interfaces disabled)
Get-WmiObject -Class Win32_IP4RouteTable | where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0' } | Sort-Object nexthop, metric1, interfaceindex
$vSCNextHop = (Get-WmiObject -Class Win32_IP4RouteTable | where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0' } | Sort-Object metric1 | select nexthop | Format-Wide | Out-String).trim()
#It's almost midnight and I have work tomorrow so I'm just looking for anything with numbers, maybe this could be better and look for a real IP address
#Also no idea if the network resources we call later have IPv6 support but it's 2024 lol
if ($vSCNextHop -contains "0123456789abcdef")
{
Write-Host "SC-NET-ERR001: CANNOT DETERMINE DEFAULT GATEWAY"
Write-Host "You do not have a default route or gateway out to the internet"
Write-Host "Sweetcade needs an internet connection to be able to complete setup"
Write-Host "Please check your network configuration"
Write-Host "This script will now exit in 60 seconds"
Start-Sleep -Seconds 60
Exit 1
}
else
{
Test-Connection $vSCNextHop
$vSCNextHopResult = Test-Connection $vSCNextHop
$vSCNextHopResult = $?
}
}
fDefaultGatewayCheck
if ($vSCNextHopResult -contains $false)
{
Write-Host "SC-NET-ERR002: CANNOT PING DEFAULT GATEWAY"
Write-Host "Please check network connection"
Write-Host "If your default gateway doesn't respond to ping, this might be a security feature, might not be"
Write-Host "Please confirm if you can reach the internet"
Write-Host "If you would like to [F]orcefully continue, connect a keyboard and type F"
Write-Host "If you would like to run this test [A]gain, connect a keyboard and type A"
$vSCNextHopUserResponse = Read-Host "F / A?"
if ($vSCNextHopUserResponse -contains "A")
{
fDefaultGatewayCheck
}
else
{
fDNSResolutionCheck
}
}
Function fDNSResolutionCheck() {
Write-Host "---Testing DNS Resolution---"
Resolve-DnsName -Name "git.sdf.org"
$vSCDNSResolutionTestResult1 = $?
Resolve-DnsName -Name "customresolutionutility.b-cdn.net"
$vSCDNSResolutionTestResult2 = $?
Resolve-DnsName -Name "github.com"
$vSCDNSResolutionTestResult3 = $?
Resolve-DnsName -Name "www.ultimarc.com"
$vSCDNSResolutionTestResult4 = $?
Resolve-DnsName -Name "www.autohotkey.com"
$vSCDNSResolutionTestResult5 = $?
Resolve-DnsName -Name "eternallybored.org"
$vSCDNSResolutionTestResult6 = $?
Resolve-DnsName -Name "7-zip.org"
$vSCDNSResolutionTestResult7 = $?
$vSCDNSResolutionTestResultArray = @($vSCDNSResolutionTestResult1,$vSCDNSResolutionTestResult2,$vSCDNSResolutionTestResult3,$vSCDNSResolutionTestResult4,$vSCDNSResolutionTestResult5,$vSCDNSResolutionTestResult6,$vSCDNSResolutionTestResult7)
}
fDNSResolutionCheck
if ($vSCDNSResolutionTestResultArray -contains $false)
{
Write-Host "SC-NET-ERR003: CANNOT RESOLVE DNS"
Write-Host "Please check your DNS server. Sweetcade cannot continue with a broken DNS server"
Write-Host "This script will now exit in 60 seconds"
Start-Sleep -Seconds 60
Exit 1
}
else
{
Exit 0
}
Function fDefaultGatewayCheck() {
Write-Host "---Determine Default Gateway---"
#This doesn't put out a $null value when no data is returned!!! (i.e. no network connection, all network interfaces disabled)
Get-WmiObject -Class Win32_IP4RouteTable | where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0' } | Sort-Object nexthop, metric1, interfaceindex
$vSCNextHop = (Get-WmiObject -Class Win32_IP4RouteTable | where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0' } | Sort-Object metric1 | select nexthop | Format-Wide | Out-String).trim()
#It's almost midnight and I have work tomorrow so I'm just looking for anything with numbers, maybe this could be better and look for a real IP address
#Also no idea if the network resources we call later have IPv6 support but it's 2024 lol
if ($vSCNextHop -contains "0123456789abcdef")
{
Write-Host "SC-NET-ERR001: CANNOT DETERMINE DEFAULT GATEWAY"
Write-Host "You do not have a default route or gateway out to the internet"
Write-Host "Sweetcade needs an internet connection to be able to complete setup"
Write-Host "Please check your network configuration"
Write-Host "This script will now exit in 60 seconds"
Start-Sleep -Seconds 60
Exit 1
}
else
{
Test-Connection $vSCNextHop
$vSCNextHopResult = Test-Connection $vSCNextHop
$vSCNextHopResult = $?
}
if ($vSCNextHopResult -contains $false)
{
Write-Host "SC-NET-ERR002: CANNOT PING DEFAULT GATEWAY"
Write-Host "Please check network connection"
Write-Host "If your default gateway doesn't respond to ping, this might be a security feature, might not be"
Write-Host "Please confirm if you can reach the internet"
Write-Host "If you would like to [F]orcefully continue, connect a keyboard and type F"
Write-Host "If you would like to run this test [A]gain, connect a keyboard and type A"
$vSCNextHopUserResponse = Read-Host "F / A?"
if ($vSCNextHopUserResponse -contains "A")
{
fDefaultGatewayCheck
}
elseif ($vSCNextHopUserResponse -contains "F")
{
fDNSResolutionCheck
}
elseif ($vSCNextHopResult -contains $true)
{
fDNSResolutionCheck
}
}
}
Function fDNSResolutionCheck() {
Write-Host "---Testing DNS Resolution---"
Resolve-DnsName -Name "git.sdf.org"
$vSCDNSResolutionTestResult1 = $?
Resolve-DnsName -Name "customresolutionutility.b-cdn.net"
$vSCDNSResolutionTestResult2 = $?
Resolve-DnsName -Name "github.com"
$vSCDNSResolutionTestResult3 = $?
Resolve-DnsName -Name "www.ultimarc.com"
$vSCDNSResolutionTestResult4 = $?
Resolve-DnsName -Name "www.autohotkey.com"
$vSCDNSResolutionTestResult5 = $?
Resolve-DnsName -Name "eternallybored.org"
$vSCDNSResolutionTestResult6 = $?
Resolve-DnsName -Name "7-zip.org"
$vSCDNSResolutionTestResult7 = $?
#This took way too long to fix but apparently an array of text isn't actually a string by default thanks Microsoft
[string[]]$vSCDNSResolutionTestResultArray = $vSCDNSResolutionTestResult1,$vSCDNSResolutionTestResult2,$vSCDNSResolutionTestResult3,$vSCDNSResolutionTestResult4,$vSCDNSResolutionTestResult5,$vSCDNSResolutionTestResult6,$vSCDNSResolutionTestResult7
if ($vSCDNSResolutionTestResultArray -contains "False")
{
Write-Host "SC-NET-ERR003: CANNOT RESOLVE DNS"
Write-Host "Please check your DNS server. Sweetcade cannot continue with a broken DNS server"
Write-Host "This script will now exit in 60 seconds"
Start-Sleep -Seconds 60
Exit 1
}
else
{
Exit 0
}
}
fDefaultGatewayCheck
#Apparently the if clauses in fDefaultGatewayCheck don't actually work? Maybe cause the function isn't defined yet? Not sure how to resolve this and I just want to move on, cso we're gonna call this function here again
fDNSResolutionCheck