Script does not abend SQL Agent job

  • I need the following powershell script to stop the SQL Agent job when it does not find a server it pings.

    FOREACH

    ($HOSTNAME IN GET-CONTENT "D:\CHECKSQLSERVER\SERVERLIST.TXT")

    {

    $status=get-wmiobject win32_pingstatus -Filter "Address='$hostname' "

    if($status.statuscode -eq 0)

    {

    write-host $hostname is reachable

    }

    else

    {

    write-host Computer $HOSTNAME not found

    trap {$_.Execption.Message; exit 1; break}

    }

    }

  • Why the trap?

    foreach($HOSTNAME IN GET-CONTENT "C:\@\SQL_Scripts\DBA\SQL Server\PowerShell\Servers.txt")

    {

    $status=get-wmiobject win32_pingstatus -Filter "Address='$hostname' "

    if($status.statuscode -eq 0)

    {

    write-host $hostname is reachable

    }

    else

    {

    write-host Computer $HOSTNAME not found

    exit 1

    }

    }

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • just based on some examples I found. tried your approach. Worked great.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply