Whether you work in the IT field, as a web designer, or if you are just a student or regular office worker, Windows Scripting has something to offer you. Of course Applescript does as well, but my focus in this case is on Windows. When I refer to Windows Scripting, I’m referring to text files filled with code that are saved as .wsf files, which Windows is able to compile and run on the fly.
Almost a year ago now, I wrote an introduction to Windows Scripting which showed how much more powerful a .wsf script can be than the older style batch jobs that IT professionals have been scripting and running for years. WSF gives you the power of a structured language like Visual Basic. By default, you can create a VBScript or JScript WSF file on Windows and it will run just fine.
Beyond that introductory article, today I wanted to offer three typical tools that people often use in both a professional IT environment as well as at home. Those three tools include reading input from a text file, pinging various devices on your network, and sending email via script.
The Power Of Windows Scripting
What learning each of these smaller components will do is allow you to combine them into a larger, automated script. I’m going to step through small script samples that you can put together into a very cool automated script. What that script will do is take an input IP list from a text file, ping each of those devices, and then send an alert email if any of those devices are down.
Reading Input Files
The first step in this process is learning how to read and process information from an input text file. In this case, I’ve created a text file called IPlist.ini that resides in the same directory as the script. This file contains a list of all of the IP addresses I want to check. You can read in each line of a text file using the following script.
<job> <script language="VBScript"> Option Explicit On Error Resume NextDim strHost Dim strCommand Dim ReturnCode Dim strLine Dim oFSO, sFile, oFile, sTextSet Shell = wscript.createObject("wscript.shell") Set oFSO = CreateObject("Scripting.FileSystemObject") sFile = "c:\users\owner\scripts\IPlist.ini" If oFSO.FileExists(sFile) Then Set oFile = oFSO.OpenTextFile(sFile, 1) Do While Not oFile.AtEndOfStream sText = oFile.ReadLine If Trim(sText) <> - Then strCommand = sText wscript.echo "IP Address is: " & sText End If Loop oFile.CloseElse WScript.Echo "The file was not there." End If WScript.Quit </script> </job>
What this code does is use the Windows file system object to open a file, and then reads one line of text at a time until it reaches the end of the file.
Pinging A Host
Now that you know how to read each IP address out of the text file, how do you go about performing a Ping withing Windows Scripting?
Pinging is a bit more complicated than reading in a text file, because you have to make use of Windows Management Instrumentation scripting (WMI). Here’s what it looks like.
<job> <script language="VBScript"> Option Explicit On Error Resume NextDim colPingResults, objPingResult, strQuery Dim strIPtext strIPtext = "192.168.1.105"' WMI query strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & strIPtext & "'"Set colPingResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )' Translate query resultsFor Each objPingResult In colPingResults If Not IsObject( objPingResult ) Then Ping = False wscript.echo strIPtext & " is not pingable" ElseIf objPingResult.StatusCode = 0 Then Ping = True wscript.echo strIPtext & " is pingable" Else Ping = False wscript.echo strIPtext & " is not pingable" End If Next Set colPingResults = Nothing WScript.Quit </script> </job>
See how easy that was? When I run it, it provides a pop-up for whether the IP was pingable or not.
In this script, I’ve only pinged a single IP address, but all you have to do is embed that ping into the previous script after each IP address is read in from the text file, and you can ping each IP address in your list.
Sending An Email
Finally, while it’s nice to have a script that you can run that will check IP addresses and pop-up a window if there are any errors, wouldn’t it be nice to run the script daily, or multiple times a day and have it automatically email with problems?
To do that, you need to know how to send an email within script. If you research online, you’ll discover dozens (or more) ways people accomplish this. By far, the most popular is using the CDO approach.
<job> <script language="VBScript"> Option Explicit On Error Resume NextConst fromEmail = "firstname.lastname@example.org" Const password = "xxxxxxxx"Dim emailObj, emailConfig Set emailObj = CreateObject("CDO.Message") emailObj.From = email@example.com emailObj.To = "firstname.lastname@example.org" emailObj.Subject = "Test Email" emailObj.TextBody = "It Works!!"Set emailConfig = emailObj.Configuration emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com" emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465 emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = fromEmail emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = password emailConfig.Fields.Update emailObj.Send Set emailobj = nothing Set emailConfig = nothing WScript.Quit</script> </job>
The above script will let you send any text for the message body as an email to any address using your Gmail credentials. You could modify the parameters to use any other SMTP mail server you like.
Now all you have to do is put those three pieces of code together. When you do so, the script will read in each list of IP addresses, ping each one, and then send that string as the message body to a notification email. Here’s what that email will look like.
As you can imagine, this becomes pretty useful in the IT world, where you have an endless list of devices and servers to keep an eye on, and only 24 hours in the day. Any time you can have automated scripts that can check things for you, do it.
Give these Windows Scripts a try and see if it helps you optimize your work and make things more efficient. Do you know of any other cool things you can do with Windows Scripting? Share your thoughts in the comments section below.