Change the last line of your script to: } | Export-CSV .\YourFile.csv -NoTypeInformation
Script to pull if server is up or down
Eric Orosz
131
Reputation points
How would I add the Export-csv command to this script the data displays on the ISE screen but I can't seem to get it to export.
Get-Content "C:TEMP\Servers and DCs.txt" |
ForEach-Object{
$result = [ordered]@{
DNSName = $_.Trim()
HostName = ""
DNSIPv4 = ""
Up = ""
TestedIPv4 = ""
}
Try{
$entry = [System.Net.Dns]::GetHostEntry($_.Trim())
$result.HostName = $entry.HostName
$entry.AddressList |
Where-Object {$_.AddressFamily -eq 'InterNetwork'} |
ForEach-Object{
$result.DNSIPv4 = $_.IPAddressToString
[array]$x = Test-Connection -Delay 15 -ComputerName $result.DNSName -Count 1 -ErrorAction SilentlyContinue
if ($x){
$result.Up = "Yes"
$result.TestedIPv4 = $x[0].IPV4Address
}
else{
$result.Up = "No"
$result.TestedIPv4 = "N/A"
}
}
}
Catch{
$result.HostName = "Host Unknown"
$result.Up = "Unknown"
$result.TestedIPv4 = "N/A"
}
[PSCustomObject]$result
}
Windows for business | Windows Server | User experience | PowerShell
Answer accepted by question author
Rich Matheisen
48,116
Reputation points