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 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy