1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<# .Synopsis Function to validate a IP address .EXAMPLE Test-IPAddress "10.0.0.1" Test-IPAddress "fe80::38f3:ecce:1907:a128%18" .OUTPUT True or False #> Function Test-IPAddress { Param ([Parameter(Mandatory=$true, ValueFromPipeline=$true)] [string]$IP) [System.Net.IPAddress]::TryParse($IP,[ref]$null) } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<# .Synopsis Function to get a IP address using NSlookup .EXAMPLE Get-IPviaNSlookup -hostname pc001 Get-IPviaNSlookup -hostname pc001 -server dns3 .OUTPUTS Output from this function is a IP or IPs returned from the DNS server via nslookup #> Function Get-IPviaNSlookup { Param ([Parameter(Mandatory=$true, ValueFromPipeline=$true)] [string]$hostName, [Parameter(Mandatory=$false, ValueFromPipeline=$true)] [string]$server = "") $nslookupCommand = nslookup "$hostname" 2> null if ($server -ne "") { if (Test-Connection $server -Quiet -Count 1){ $nslookupCommand = nslookup "$hostname" "$server" 2> null } else { Write-Host "Can't find server $server" -ForegroundColor Red Exit } } [string]$rawData = $nslookupCommand $regex = "\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" $counter = 0 $rawData | Select-String -Pattern $regex -AllMatches | foreach {$_.Matches} | foreach { if ($counter -gt 0) { $_.Value } $counter++ } } |