Test-connection in a Try/Catch block as a function passing the target IP, 
setting a gvar to hold the result

https://technet.microsoft.com/en-us/library/hh849808.aspx

> From: [email protected]
> To: [email protected]
> Subject: RE: [NTSysADM] Dirty batch for up servers
> Date: Fri, 6 Feb 2015 17:13:11 +0000
> 
> If using WMI is within scope, then there is also the Win32_PingStatus class.
> 
> Example usage with PowerShell:
> 
>     PS U:\> Get-WmiObject -Class Win32_PingStatus -Filter 
> 'Address="localhost"' | Format-Table -AutoSize
> 
>     Source     Destination IPV4Address IPV6Address Bytes Time(ms)
>     ------     ----------- ----------- ----------- ----- --------
>     BERNEREWIN localhost   127.0.0.1   ::1         32    0
> 
> 
> See also:
> 
>     Pinging With WMI
>     http://www.powertheshell.com/pinging-with-wmi/
> 
> 
> I don't know much about WMIC, but here's an example command from another 
> website:
> 
>     U:\>wmic path Win32_PingStatus where "Address='localhost'"  get 
> statuscode /format:value
> 
>     StatusCode=0
> 
>     Useful commands for Windows administrators
>     http://www.robvanderwoude.com/ntadmincommands.php
> 
> 
> -- 
> Edward Berner
> 
> 
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] 
> On Behalf Of Hilderbrand, Doug
> Sent: Thursday, February 05, 2015 5:54 PM
> To: [email protected]
> Subject: RE: [NTSysADM] Dirty batch for up servers
> 
> Here is a pair of short scripts I use for automating some tasks I do. It 
> pings and then does an action only if the ping succeeds. And it properly 
> fails on the "destination host unreachable" ping result.
> 
> You use it by including a command on the same line. i.e.: 
>     ifpingsok.bat myserver echo something
> or  ifpingsok myotherserver copy something somewhere
> or  for /f %i in (filefullofservernames.txt) do ifpingsok %i 
> RunSomeImportantCommand
> 
> It has worked for me except for extremely complex commands. 
> Oops, almost forgot to include stderr.bat. Maybe I need to rework this in 
> PowerShell, but it works and I use it almost daily.
> 
> ::::::::::::::
> :: ifpingsok.bat
> @echo off
> setlocal
> set pingsOKtemp=c:\temp\pingsOK.%random%.tmp
> 
> if {%1}=={} exit /b 5
> if {%2}=={} exit /b 5
> 
> ping -4 -n 2 %1 | findstr Reply > %pingsOKtemp%
> 
> findstr bytes %pingsOKtemp% > nul
> set exitcode=%errorlevel%
> 
> for /f "tokens=1,*" %%i in ('echo %*') do set stuff2do=%%j
> if not %exitcode%==0 call StdErr.bat "%1 doesn't ping" & goto :endfail
> %stuff2do%
> 
> :endok
> del /q %pingsOKtemp%
> endlocal
> exit /b 0
> 
> :endfail
> del /q %pingsOKtemp%
> endlocal
> exit /b 1
> ::::::::::::::
> :: stderr.bat
> @echo off
> setlocal
> if exist "%TEMP%\StdErr.vbs" goto out
> @echo dim err>"%TEMP%\StdErr.vbs"
> @echo Set objshell = CreateObject("WScript.Shell")>>"%TEMP%\StdErr.vbs"
> @echo Set objArgs = WScript.Arguments>>"%TEMP%\StdErr.vbs"
> @echo err=objArgs(0)>>"%TEMP%\StdErr.vbs"
> @echo WScript.StdErr.Writeline err>>"%TEMP%\StdErr.vbs"
> :out
> if {%1}=={} goto outNone
> cscript //nologo "%TEMP%\StdErr.vbs" %1
> endlocal
> goto :EOF
> :outNone
> cscript //nologo "%TEMP%\StdErr.vbs" "No Parameter Provided."
> endlocal
> 
> 
> Doug Hilderbrand | Systems Administrator Sr., Information Technology | Crane 
> Aerospace & Electronics
> 
> From: [email protected] [mailto:[email protected]] 
> On Behalf Of Micheal Espinola Jr
> Sent: Thursday, November 6, 2014 2:04 PM
> To: ntsysadm
> Subject: Re: [NTSysADM] Dirty batch for up servers
> 
> As an example of what you can scrape, you could do the following within the 
> parenthesis:
> 
> FOR /F "tokens=6 delims== " %%I IN ('findstr /I /C:"Average = " 
> %TEMP%\SpeedTest.txt') DO SET SpdTst=%%I
> 
> This capture the average ping round-trip time, and would allow you to 
> potentially flag latency problems, even if all the pings are successful.
> 
> 
> --
> Espi
>  
> 
> On Thu, Nov 6, 2014 at 12:38 PM, David McSpadden <[email protected]> wrote:
> Thanks guys.
> This helps.
>  
>  
> From: [email protected] [mailto:[email protected]] 
> On Behalf Of Micheal Espinola Jr
> Sent: Thursday, November 06, 2014 3:07 PM
> To: ntsysadm
> Subject: Re: [NTSysADM] Dirty batch for up servers
>  
> In its simplest form:
>  
> PING #.#.#.#>%TEMP%\SpeedTest.txt
> IF "%ERRORLEVEL%" NEQ "0" ()
>  
> Echoing to a text file gives you the opportunity to scrape for any verbose 
> output if you need to.  Within the parenthesis, you can do whatever you want 
> based on a ping failure.
> 
> 
> --
> Espi
>  
>  
> On Thu, Nov 6, 2014 at 8:13 AM, David McSpadden <[email protected]> wrote:
> I want to run a windows batch the just does a ping.
> If the ping returns good data do nothing but if the ping fails set an error 
> code I can look for and address it outside the batch.
> Anyone have or can point me to a . b a t script that does this already??
> Thanks
>  
> 
> 
> 
                                          

Reply via email to