Created
July 19, 2024 17:10
-
-
Save noahpeltier/e7b653f626d910835c6f6f875b0687b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-IPRangeFromCIDR { | |
param( | |
$Range | |
) | |
# Set up empty list to contain the calculated range | |
$IPList = [System.Collections.Generic.list[object]]::new() | |
# split out input to IP and CIDR and calculate the num,ber of addresses with the power of MATH | |
$startIP, [int]$CIDR = $Range -split "/" | |
$NumberOfAddresses = [Math]::Pow(2, (32 - $CIDR)) | |
# Get IP bytes, reverse it and convert bytes it to UInt32 for start and end addresses | |
$StartIPBytes = ([ipaddress]$startIP).GetAddressBytes() | |
[array]::Reverse($StartIPBytes) | |
$StartIP = [BitConverter]::ToUInt32($StartIPBytes, 0) | |
$EndIPBytes = [ipaddress]::Parse(($startIP) + ($NumberOfAddresses)).GetAddressBytes() | |
[array]::Reverse($EndIPBytes) | |
$EndIP = [BitConverter]::ToUInt32($EndIPBytes, 0) | |
# Increase the start address int untill it equals the end INT parsing the IP to string and adding it to the list | |
while ($startIP -lt $endIP) { | |
$IPList.Add(([IPaddress]::Parse($startIP)).IPAddresstoString) | |
$startIP++ | |
} | |
return $IPlist | |
} | |
function Invoke-PingSweep { | |
[CmdletBinding()] | |
param( | |
[ValidatePattern('^(?:\d{1,3}\.){3}\d{1,3}/(0[1-9]|[12][0-9]|3[0-2])$')] | |
$CIDRRange | |
) | |
$allMachines = Get-IPRangeFromCIDR -Range $CIDRRange | |
$Results = $allMachines | % { | |
[pscustomObject]@{ | |
Name = $_ | |
DNS = [system.net.Dns]::GetHostEntryAsync($_) | |
Ping = (New-Object System.Net.NetworkInformation.Ping).SendPingAsync($_, 600) | |
} | |
} | |
$HealthReport = $results | % { | |
[pscustomObject]@{ | |
Name = $_.Name | |
DNSName = $_.DNS.Result.HostName | |
IPAddress = $_.Ping.Result.Address.IPAddressToString | |
Status = $( | |
switch ($_.Ping.Result.Status) { | |
"Success" {"Online"} | |
"TimedOut" {"Offline"} | |
default {"Unknown"} | |
} | |
) | |
LastSeen = $( | |
switch ($_.Ping.Result.Status) { | |
"Success" {(((Get-Date).AddHours(2)).ToString("MM.dd.yyy h:mm:ss tt"))} | |
} | |
) | |
} | |
} | |
return $HealthReport | where {$_.status -ne "Offline"} | |
} | |
Write-Host "[$(get-date)] Started ping scan on $($env:CIDR)" | |
Invoke-PingSweep $env:CIDR | |
Write-Host "[$(get-date)] Finished ping scan on $($env:CIDR)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment