Monday, August 20, 2018

Powershell - Find IP range for device

This was a pre-check for client computers about the be ADMT'ed to a new domain. The back story is a computer could be on a wired connection, a few WIFI networks which some had domain connectivity and some did not, and 4g/Home connection and direct access. At the point of migrate we bumped out the laptops that were going to fail, this was just a pre-check.

This was just a report so we could chase the user to correct the problem.

This script could be much easier in Windows 10 due to new functions (ip v6), but I did not have the option.


$computers = Import-Csv C:\temp\computerlist.txt
ipconfig /flushdns > null
#cls

foreach ($computers in $computers){
$ThisComputer=$computers.computername.trim()
$NoMigComputer=$ThisComputer + ".old.gov.au"

$testcon = " NOT reachable"
$didit = Test-Connection $NoMigComputer -count 1 -quiet
if ($didit -ne $false)
{$testcon = "and is contactable"}

#write-host Attempt to look at $NoMigComputer
try {

$result = $null
$JustIP = $null
$result = [System.Net.Dns]::GetHostAddresses($NoMigComputer)
$JustIP = $result.ipaddresstostring

if ($result -eq $null)
    {    write-host $NoMigComputer " is not in DNS"    }
else
    {
    #write-host $NoMigComputer" IP is" $JustIP
        if
        (
        $JustIP -like '192.22.124.*' -or
        $JustIP -like '192.28.126.*' -or
        $JustIP -like '192.22.234.*'
        )
        {
        write-host $NoMigComputer $JustIP  "wired" $testcon

        }
        else
        {
        write-host $NoMigComputer $JustIP "is on WIFI" $testcon
        } } }
catch {

$YesMigComputer=$ThisComputer + ".ic.nsw.gov.au"
try {
$Yesresult = [System.Net.Dns]::GetHostAddresses($YesMigComputer)
}
catch {
#$YesMigComputer + " not in IC domain EITHER"
}

$JustIP = $result.ipaddresstostring

if ($JustIP -eq $null)
    {
    write-host $NoMigComputer "is not in DNS *V4"
    }

if ($NoMigComputer -eq $null)
{
Write-host $NoMigComputer "On 4G connection"
} } }
Post a Comment

Blog Archive