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
#$IP=$null
}
$serverlist $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=$null
$Subnet=($Server."IP Address").split(".")
#Lazy assuming class c set 4th octet to 0
$Subnet[3]=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
([System.Net.Dns]::GetHostAddresses("mypc")).count
([System.Net.Dns]::GetHostAddresses("www.google.com")).count 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
} Get FQDN [System.Net.Dns]::GetHostByName("www.google.com") Get FQDN [System.Net.Dns]::GetHostEntry("www.google.com")
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 {
[CmdletBinding()]
Param(
[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
If($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
If($?)
{
$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)
{$true}
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"}
Else
{
#return the errorstring to MathingName variable and write the new WMI error to a log file
$ErrorString
#$WMIError | Out-File .\$($ComputerName)_error.txt
}
}
Else {$false}
}
$HostInfo
<#
.synopsis
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
.description
Check host
.PARAMETER ComputerName
specify the Computer Name
.example
Check-Host -ComputerName Server01
.example
Check-Host Server01
.Link
http://www.google.com
#>
}
Import remote moduleshttp://blogs.technet.com/b/ashleymcglone/archive/2013/06/27/how-to-use-the-2012-active-directory-cmdlets-from-windows-7.aspx
$ses = New-PSSession -ComputerName Server1
Import-Module DNSServer,DNSClient,NetTCPIP -PSSession $ses DNS Querriesfunction Get-HostLookup
{
[CmdletBinding()]
Param(
[parameter(Mandatory = $true)]
[string] $DomainName=$(throw '$DomainName is required')
)
$HostLookup= $([System.Net.Dns]::GetHostentry("$($DomainName)"))
Return $HostLookup
} View Client DNS Cachehttps://gallery.technet.microsoft.com/scriptcenter/ad12dc1c-b0c7-44d6-97c7-1a537b0b4fefFunction 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
$socket.Connect('google.com',80)
$socket.Connected
}
catch [system.exception] {
'False'
}
|
|