Powershell‎ > ‎Stuff‎ > ‎

PingList


Pinglist


$StartTime = Get-Date
Write-Host "Starting $StartTime"
#Import a source list of machines to test must have a column header of DiscoveryComputerName
$pinglist = Import-Csv ".\ws.csv"
#Filter this list to exclude lines without a value for DiscoveryComputername
$pinglist = $pinglist | where {$_.DiscoveryComputername -ne ""}
$Counter = $null
foreach ($machine in $pinglist) 
{ 
    #Clear variable at the start of each iteration
    $DNS = $null
    $Domain = $null
    $pingable = $null
    $IPAddress = $null
    $WMIOS = $null
    $WMIOSInstallDate = $null
    $WMIDomain = $null
    $WMIError = $null
    $ErrorString = $null
    $MatchingNames = $null
    $Counter ++
    #add error trapping for DNS 
    #    Exception calling "GetHostByName" with "1" argument(s): "The requested name is valid, but no data of the requested type was found"
    $DNS = [System.Net.Dns]::GetHostByName($machine.DiscoveryComputerName)
    If ($DNS) 
    {
        #Check if hostname lenght is longer than computername if so set Domain to hostname - computername
        If ($dns.HostName.Length -gt $machine.DiscoveryComputerName.Length)
            {$Domain = $DNS.HostName.Substring(($dns.HostName.Split(".")[0].length + 1))}
        #get the First IP Address in the list
        $IPAddress = $DNS.AddressList[0].IPAddressToString
        $pingable=$(Test-Connection -Quiet -count 2 -ComputerName $IPAddress)
        If ($pingable)
        {
            #get the Computername from WMI via IP address
            $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($?)
            {
                $WMIOSInstallDate = (([WMI]'').ConvertToDateTime($WMIOS.InstallDate)).tostring()
                $WMIDomain = $(Get-WmiObject -Class win32_Computersystem -ComputerName $IPAddress).Domain
            }
            #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
            $MatchingNames = IF ($machine.DiscoveryComputerName -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 .\$($machine.DiscoveryComputerName)_error.txt
                    }
                }
                Else {$false}
        }
    }
    write-host "$Counter testing $($machine.DiscoveryComputerName) Pingable $pingable Names Match $MatchingNames"
    $machine | Add-Member -membertype noteproperty -name FQDN -Value $DNS.HostName
    $machine | Add-Member -membertype noteproperty -name DNSDomain -Value $Domain
    $machine | Add-Member -membertype noteproperty -name DNSIPCount -Value $DNS.AddressList.count
    $machine | Add-Member -membertype noteproperty -name FirstIPAddress -Value $IPAddress
    $machine | Add-Member -membertype noteproperty -name Pingable -Value $Pingable
    $machine | Add-Member -membertype noteproperty -name WMICSName -Value $WMIOS.CSName
    $machine | Add-Member -membertype noteproperty -name WMIInstallDate -Value $WMIOSInstallDate
    $machine | Add-Member -membertype noteproperty -name WMIDomain -Value $WMIDomain
    $machine | Add-Member -membertype noteproperty -name MatchingNames -Value $MatchingNames
}

$pinglist | Export-Csv -notype ".\Full_Output.csv"
#export a list where the name on the list does not match the actual name of the machine.
$pinglist | where {$_.MatchingNames -eq $False} | Export-Csv -notype ".\NonMatching_Output.csv"

$StopTime = Get-Date
$ElapsedTime =$StopTime - $StartTime 
Write-host "Script completed in $([Math]::Round($ElapsedTime.TotalMinutes,0)) Minutes"




Simplified 
$StartTime = Get-Date
Write-Host "Starting $StartTime"
#Import a source list of machines to test must have a column header of HostName
$InputFile = "c:\temp\serverlist.csv"
$OutputFile = "c:\temp\serverlist_ouput.csv"
$pinglist = Import-Csv $InputFile
#Filter this list to exclude lines without a value for HostName
$pinglist = $pinglist | where {$_.HostName -ne ""}
$Counter = $null
foreach ($machine in $pinglist) 
{ 
    #Write-host "$($machine.Hostname)"
    #Clear variable at the start of each iteration
    $DNS = $null
    $Domain = $null
    $pingable = $null
    $IPAddress = $null
    $Counter ++
    #add error trapping for DNS 
    #    Exception calling "GetHostByName" with "1" argument(s): "The requested name is valid, but no data of the requested type was found"
    $DNS = [System.Net.Dns]::GetHostByName($machine.Hostname)
    If ($DNS) 
    {
        #Check if hostname lenght is longer than computername if so set Domain to hostname - computername
        If ($dns.HostName.Length -gt $machine.HostName.Length)
            {$Domain = $DNS.HostName.Substring(($dns.HostName.Split(".")[0].length + 1))}
        #get the First IP Address in the list
        $IPAddress = $DNS.AddressList[0].IPAddressToString
        $pingable=$(Test-Connection -Quiet -count 2 -ComputerName $IPAddress)
    
    }
    write-host "$Counter testing $($machine.Hostname) Pingable $pingable"
    $machine | Add-Member -membertype noteproperty -name FQDN -Value $DNS.HostName
    $machine | Add-Member -membertype noteproperty -name DNSDomain -Value $Domain
    $machine | Add-Member -membertype noteproperty -name DNSIPCount -Value $DNS.AddressList.count
    $machine | Add-Member -membertype noteproperty -name FirstIPAddress -Value $IPAddress
    $machine | Add-Member -membertype noteproperty -name Pingable -Value $Pingable

}

$pinglist | Export-Csv -notype $OutputFile

$StopTime = Get-Date
$ElapsedTime =$StopTime - $StartTime 
Write-host "Script completed in $([Math]::Round($ElapsedTime.TotalMinutes,0)) Minutes"



  

Comments