Pinterest Stumbleupon Whatsapp
Ads by Google

windows scriptingWhether 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 How To Use Applescript's UI Scripting [Mac] How To Use Applescript's UI Scripting [Mac] Read More 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 Better Than Batch: A Windows Scripting Host Tutorial Better Than Batch: A Windows Scripting Host Tutorial Read More 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 Next
Dim strHost
Dim strCommand
Dim ReturnCode
Dim strLine
Dim oFSO, sFile, oFile, sText
Set 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.Close
Else
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.

Ads by Google

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 Next
Dim 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 results
For 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.

windows scripting

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 Next
Const fromEmail = "rdxxxx@gmail.com"
Const password = "xxxxxxxx"
Dim emailObj, emailConfig
Set emailObj = CreateObject("CDO.Message")
emailObj.From = alert@topsecretwriters.com
emailObj.To = "rdxxxxx@gmail.com"
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.

windows scripting

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.

Image credit: Mario Alberto Magallanes Trejo

  1. neel thomas
    September 16, 2015 at 12:41 pm

    What change has to be made if the email has to be send with an attachment, please comment

  2. Anonymous
    September 10, 2011 at 3:48 am

    dont know about writing down scripts but the Sikuli thing is so awesome just read about it and will try it soon, thanks mate :D

    • Ryan Dube
      September 10, 2011 at 12:38 pm

      Awesome - thanks for your comment.

  3. Anonymous
    September 10, 2011 at 3:03 am

    did you write rdxxxxx@gmail.com on purpose?? cuz in the screenshot it reveals the id *evil laugh*

    • Ryan Dube
      September 10, 2011 at 12:38 pm

      I do that occasionally - usually my biggest concern is scraping so I make sure text will have the address x'd out, but it's hard to catch every spot in the screenshots that display it. :-)

  4. guest
    September 9, 2011 at 10:57 pm

    What versions of windows can you run windows scripting on?

    • Ryan Dube
      September 10, 2011 at 12:37 pm

      Good question - from my own experience I know it works on Windows 7 and Windows XP. Beyond that I really can't say - but I think that covers a fairly wide range of what people would need?

Leave a Reply

Your email address will not be published. Required fields are marked *