Functions/Network/Get-NSLookup.ps1
Function Get-NSLookup { [cmdletbinding()] Param ( # IP or Hostname Input [Parameter(mandatory=$false,ValueFromPipeline=$true)] [string] $InputObject, # Format to Output [Parameter(mandatory=$false)] [ValidateSet("IP","HOSTNAME","Object")] [string] $OutputType = "IP" ) Process { # Determine method to use based on pattern of inputobject $Pattern = if ($InputObject -like "*.*.*.*" -and (($InputObject -split '\.')[0] -as [int])){"IP"}else{"Hostname"} $Lookup = switch ($Pattern) { "IP" {try{[System.Net.Dns]::GetHostByAddress($InputObject)}catch{$null}} "Hostname" {try{[System.Net.Dns]::GetHostByName($InputObject)}catch{$Null}} } switch ($OutPutType) { "IP" {$Lookup.AddressList.ipaddresstostring} "Hostname" {$lookup.HostName} "Object" { [pscustomobject]([ordered]@{ Hostname = $lookup.HostName IPAddress = $Lookup.AddressList.ipaddresstostring }) } } } } |