1

I was working tonight to re-write an existing server health check script to store its values in a hashtable, and that part is working fine. However, I want the results to go to a CSV file, and that file only to be populated with servers where I've tagged them as requiring action. Currently those are generating event ID 7011, or failing a ping test by Test-Connection.

Here's the code:

$CheckServer = @{}
$Servers = (Get-Content $Dir\Test.txt)
foreach ($Server in $Servers) {
    $CheckServer.EventID7011 = Get-Eventlog -LogName System -ComputerName $Server -Newest 1 |
        Where-Object {$_.EventId -eq 7011} | select Message
    if ($CheckServer.EventID -ne $Null) {
        $CheckServer.Server = "$Server"
        $CheckServer.ActionReq = "Yes"
    }

    $CheckServer.Ping = Test-Connection -ComputerName $Server -Count 1 -Quiet
    if (! $CheckServer.Ping) {
        $CheckServer.Server = "$Server"
        $CheckServer.ActionReq ="Yes"
        $CheckServer.Ping = "Offline"
    } else {
        $CheckServer.Server = "$Server"
        $CheckServer.ActionReq = "No"
        $CheckServer.Ping = "Online"
    }

    New-Object -TypeName PSObject -Property $CheckServer |
        Export-Csv "ScanResults.csv" -NoTypeInformation -Append
}

I need the correct code at the end, as it stands, the script works fine for collecting/storing the data in the hashtable array $CheckServer, but I'd like to only select those servers that require action. So, if I'm scanning 100 servers, and 2 of them are in a ping fail state, I want only those selected and sent to Export-Csv.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Kenny
  • 141
  • 1
  • 2
  • 13
  • Where do you stuck? You can Filter using `Where-Object` and export it to csv using `Export-Csv` – Martin Brandl Feb 09 '16 at 06:27
  • Thx for your fast reply - I've not included the code I've attempted to use over the past few hours - i've just left it unfinished. It's working fine now to display ALL the results, but i want to only show those servers where i'd previously set "ActionReq=Yes' when they fail a test-connection, or log an sys event ID of 7011. – Kenny Feb 09 '16 at 06:34

2 Answers2

1

If you want only servers that don't respond to Test-Connection in the output anyway it would be much simpler to just use a Where-Object filter on the server list:

Get-Content "$Dir\Test.txt" |
    Where-Object { -not (Test-Connection -Computer $_ -Count 1 -Quiet) } |
    Select-Object @{n='Server';e={$_}}, @{n='ActionReq';e={'Yes'}},
                  @{n='Ping';e={'Offline'}} |
    Export-Csv 'ScanResults.csv' -NoType -Append
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

You need to store the objects into a list before you can filter and export them. See the lines with comments in your code:

$CheckServer = @{}
$serverObjects = @() # create a list of server objects
$Servers = (get-content $Dir\Test.txt)
ForEach ($Server in $Servers) {

$CheckServer.EventID7011 = get-eventlog -LogName System -ComputerName
$Server -newest 1 | where-object {$_.eventID -eq 7011} |select message
If ($CheckServer.EventID -ne $Null) {
$CheckServer.Server="$Server"
$CheckServer.ActionReq = "Yes"}

$CheckServer.Ping = Test-Connection -ComputerName $Server -count 1 -quiet
if (! $CheckServer.Ping) {
    $CheckServer.Server="$Server"
    $CheckServer.ActionReq ="Yes"
    $CheckServer.Ping= "Offline"}

Else {
    $CheckServer.Server="$Server"
    $CheckServer.ActionReq ="No"
    $CheckServer.Ping= "Online"}

    # Add the server object to the list
        $serverObjects += New-Object -TypeName PSObject -Property $CheckServer 
    }
}
# now filter it:
$serverObjects | where ActionReq -eq "Yes" | Export-Csv -Path "...."
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172