sogeniusio
2/15/2018 - 3:06 PM

[SPLUNK] - Powershell Scripts

$ServerName = Get-Content "[INSERT_FULL_FILEPATH]" #File should have list of servers (one server per line)
$UpCount = 0
$DownCount = 0
$Counter = 1

""

function MakeData($Counter, $Date, $Server, $Status) {
    $data1 = $Counter
    $data2 = $Date
    $data3 = $Server
    $data4 = $Status

    $out = new-object psobject
    $out | add-member noteproperty Num $data1
    $out | add-member noteproperty Date $data2
    $out | add-member noteproperty Hostname $data3
    $out | add-member noteproperty Status $data4

    write-output $out

}

foreach ($Server in $ServerName) {

    if (test-Connection -ComputerName $Server -Count 1 -Quiet ) {
        #"$Counter | $Date | $Server | UP "

        $Date = Get-Date -Format g

        $Status = "UP"

        $UpCount++

    } else {              
        $Date = Get-Date -Format g

        $Status = "DOWN"

        $DownCount++
    }

    makedata $Counter $Date $Server $Status

    $Counter++

}
""

$COUNTER = $COUNTER - 1
"TOTAL: $UpCount UP / $DownCount DOWN - $COUNTER TOTAL SERVERS"