We use this script to try and get information from some of our servers. Issue that I have is the script somehow "saves" the last server IP, so when the next server in the list is unreachable we still get a "success" response from the previous IP. I have tried using the Clear-Item variable: command
after every test but it doesn't seem to release the value.
$enddate = (Get-Date).tostring("yyyyMMdd")
$filename_SRM = 'C:\Scripts\' + $enddate + '_SRM_Scan.csv'
$compArray = get-content C:\Scripts\Servers.txt
foreach($strComputer in $compArray)
{
$ErrorActionPreference = “SilentlyContinue”
$ping = new-object system.net.networkinformation.ping
$ping.send(“$strComputer”) | Where-Object{$_.Name -eq 'Address'}
$pingreturns = $ping.send(“$strComputer”).Status
$socket = new-object Net.Sockets.TcpClient
$port = 135 #445
$socket.Connect($strComputer, $port)
$Network = Get-WMIObject -class Win32_NetworkAdapterConfiguration -Computername $strComputer | `
Where-Object {$_.IPEnabled -match "True"} | `
Select-Object -property DNSHostName,@{N="DNSServerSearchOrder";
E={"$($_.DNSServerSearchOrder)"}},
@{N='IPAddress';E={$_.IPAddress}}
$IP = [System.Net.Dns]::GetHostAddresses(“$strComputer”)
if ($socket.Connected)
{
$Netlogon = Get-WMIObject Win32_Service -ComputerName $strComputer | Where-Object{$_.Name -eq 'Netlogon'}
if ($Netlogon) {
$OS = Get-WmiObject -class Win32_OperatingSystem -computername $strComputer
$services = Get-WMIObject -class Win32_Service -ComputerName $strComputer
"$strComputer ; $pingreturns ; connected ; $($OS.CSName) ; $($Network.IPAddress) "
}else{
"$strComputer ; $pingreturns ; WMI Unavailable ; ; $IP"
}
}
if (!$socket.Connected)
{
echo "$strComputer ; $pingreturns ; Unable to connect ; ; $IP"
}
$socket.Close()
}