November 3, 2011 at 9:26 am
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}
}
}
November 3, 2011 at 3:01 pm
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
November 3, 2011 at 3:18 pm
just based on some examples I found. tried your approach. Worked great.
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply