Powershell‎ > ‎


Get IPs for a list of hostnames (nslookup)

$serverList= Get-QADComputer blah* | select name
foreach ($server in $serverlist)
	$IP= $([System.Net.Dns]::GetHostAddresses("$($server.name)")).IPAddressToString
	$Server | Add-Member -membertype noteproperty -name IPAddress -Value $IP
$serverlist | where {$_.IPAddress -like "192.168.3.*"}

Get Subnet ID split IP Address

$ServerList = Import-Csv C:\scripts\AllServers.csv
foreach ($Server in $ServerList)
    $Subnet=($Server."IP Address").split(".")
    #Lazy assuming class c set 4th octet to 0
    $Subnet = $Subnet -join "."
    $Server | Add-Member -MemberType NoteProperty -Name "Subnet" -Value $subnet

$ServerList | select "device name", subnet | group -Property subnet

Count IP Address returned by dns

Count only IPV4 Addresses
([System.Net.Dns]::GetHostAddresses("www.google.com") | where {$_.AddressFamily -eq "InterNetwork"}).count

Count for each name in a list
$machinelist = "www.google.com","www.yahoo.com","www.cnn.com"
foreach ($machine in $machinelist)
	#Sometimes count does not return a value for the line bellow I don't understand why seems like 
	#it does not work if only 1 IP is returned
	#$DNSIPV4Count = ([System.Net.Dns]::GetHostAddresses($machine) | where {$_.AddressFamily -eq "InterNetwork"}).count
	$DNSIPV4Count = [System.Net.Dns]::GetHostAddresses($machine) | where {$_.AddressFamily -eq "InterNetwork"} | measure
	write-host $machine $DNSIPV4Count.Count


Check-Host - modified to create an object instead of write-host
Why does my new object have extra blank lines that I don't want?
function Check-Host {
        [Parameter(Mandatory=$True, HelpMessage="Computer Name")][String]$ComputerName
    $HostInfo = {} | Select "FQDN","IP Addresses","Pingable","WMI Computer Name","WMI Computer Domain",
            "WMIOSInstallDate","MatchingNames","Uptime Text","Uptime"
    $DNS = [System.Net.Dns]::GetHostByName($ComputerName)
    $IPAddress = $DNS.AddressList[0].IPAddressToString
    $Pingable=$(Test-Connection -Quiet -count 2 -ComputerName $IPAddress)
    $HostInfo.FQDN = $($DNS.HostName)
    $HostInfo."IP Addresses" = $($DNS.AddressList)
    $HostInfo.Pingable = $Pingable
        $WMIOS = Get-WmiObject -ErrorVariable WMIError -ErrorAction SilentlyContinue -Class win32_operatingsystem -ComputerName $IPAddress
        #If there was no error running the previouse line (wmi connection) do this wmi connection too
            $HostInfo."WMI Computer Name" = $($WMIOS.CSName)
            $HostInfo."WMI Computer Domain" = $(Get-WmiObject -Class win32_Computersystem -ComputerName $IPAddress).Domain
            $Uptime = $WMIOS.ConvertToDateTime($WMIOS.LocalDateTime) - $WMIOS.ConvertToDateTime($WMIOS.LastBootUpTime)
            $HostInfo.WMIOSInstallDate = (([WMI]'').ConvertToDateTime($WMIOS.InstallDate)).tostring()
            $HostInfo."Uptime Text"= "$($Uptime.Days) Days $($Uptime.Hours) Hours $($Uptime.Minutes) Minutes"
            $HostInfo.Uptime = $Uptime
        #Test if the original computer name matches the name returned by WMI at that IP address if so "True" otherwise
        #return the WMI errer if it exists or False if not
        $HostInfo.MatchingNames = IF ($ComputerName -eq $WMIOS.CSName) 
            ElseIf ($WMIError)
                $ErrorString = "$WMIError"
                If($ErrorString -like '*Access is denied*'){"WMI Error - Access is denied"}
                ElseIf($ErrorString -like '*OutOfMemoryException*'){"WMI Error - OutOfMemoryException"}
                ElseIf($ErrorString -like '*Server execution failed*'){"WMI Error - Server execution failed"}
                ElseIf($ErrorString -like '*RPC server is unavailable*'){"WMI Error - RPC server is unavailable"}
                ElseIf($ErrorString -like '*Not Found*'){"WMI Error - Not Found"}
                    #return the errorstring to MathingName variable and write the new WMI error to a log file
                    #$WMIError | Out-File .\$($ComputerName)_error.txt
            Else {$false}
    quick health check on a host validate it is in DNS, Pingable and the Name of that machine 
    matches the input computername and checks host uptime
    Check host
.PARAMETER ComputerName
    specify the Computer Name
    Check-Host -ComputerName Server01
    Check-Host Server01

Import remote modules


$ses = New-PSSession -ComputerName Server1
Import-Module DNSServer,DNSClient,NetTCPIP -PSSession $ses


DNS Querries

function Get-HostLookup
        [parameter(Mandatory = $true)]
        [string] $DomainName=$(throw '$DomainName is required')
    $HostLookup= $([System.Net.Dns]::GetHostentry("$($DomainName)"))
    Return $HostLookup

View Client DNS Cache


Function Get-DNSClientCache{ 
$DNSCache = @() 
Invoke-Expression "IPConfig /DisplayDNS" | 
Select-String -Pattern "Record Name" -Context 0,5 | 
        $Record = New-Object PSObject -Property @{ 
        Name=($_.Line -Split ":")[1] 
        Type=($_.Context.PostContext[0] -Split ":")[1] 
        TTL=($_.Context.PostContext[1] -Split ":")[1] 
        Length=($_.Context.PostContext[2] -Split ":")[1] 
        Section=($_.Context.PostContext[3] -Split ":")[1] 
        HostRecord=($_.Context.PostContext[4] -Split ":")[1] 
        $DNSCache +=$Record 
    return $DNSCache 

Test port access

try { 
    $socket = new-object Net.Sockets.TcpClient
catch [system.exception] {


Subpages (1): DHCP