Share via

Script to pull if server is up or down

Eric Orosz 131 Reputation points
2022-08-19T19:37:36.327+00:00

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
0 comments No comments

Answer accepted by question author

Rich Matheisen 48,116 Reputation points
2022-08-19T20:12:44.533+00:00

Change the last line of your script to: } | Export-CSV .\YourFile.csv -NoTypeInformation

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.