Home Forums Programming Powershell Wanted to check good / alive SQL Servers through script. RE: Wanted to check good / alive SQL Servers through script.

  • There isn't much wrong with the code actually. You were close. Actually, you need an array of hashtables, one of which needs an array to hold the services. This object, '$services' is defined in the beginning as an empty array, and we just add the properties and values to the empty 'status' hashtable as we find stuff out. It is rather neat. I might copy it myself. By the way, to take a good look at the object you've created, you need ConvertTo-JSON (or my own ConvertTo-PSON or ConvertTo-YAML). I've only included the relevant parts of the routine in the following snippet. I didn't test the rest, but it should be enough to get you up and running again.

    $Services=@()

    foreach ($server in $newset)

    {

    $status = @{ "ServerName" = $server; "TimeStamp" = (Get-Date -f s) }

    if (!(Test-Connection $server -Count 1 -ea 0 -Quiet))

    {

    #Server is DOWN

    $status.Results = "DOWN"

    }

    else

    {

    #Server is UP

    $status.Results = "UP"

    $status.Services=Get-Service -ComputerName $server |

    Where-Object {$_.ServiceName -like "MSSQL`$*"}|

    Select-Object Name, DisplayName, Status

    $Services+=$Status

    }

    }

    Best wishes,
    Phil Factor