Windows Scripting: 3 Awesome Things You Can Do

The Versatility of Windows Scripting
Windows Scripting provides benefits for a wide range of users, including IT professionals, web designers, students, and office workers. While AppleScript serves a similar purpose, this discussion concentrates specifically on the Windows environment.
Windows Scripting utilizes text files containing code, saved with the .wsf extension. Windows can then compile and execute these files directly.
WSF vs. Traditional Batch Jobs
Approximately a year ago, an introductory article demonstrated the enhanced capabilities of .wsf scripts compared to traditional batch jobs. These older methods have long been employed by IT professionals for automation.
WSF empowers users with the functionality of a structured programming language, such as Visual Basic. By default, Windows can readily execute WSF files written in either VBScript or JScript.
Three Common Windows Scripting Tools
Building upon the initial introduction, this article highlights three frequently used tools applicable in both professional and personal settings. These include text file input, network device pinging, and scripted email transmission.
Reading Input from a Text File
A common scripting task involves processing data from text files. This allows for automation of tasks that would otherwise require manual data entry or manipulation.
Scripts can be designed to read specific lines, extract data based on patterns, or perform calculations using the information contained within the text file.
Pinging Network Devices
Network administrators often need to verify the availability of devices on a network. Pinging is a fundamental method for testing network connectivity.
Windows Scripting allows for automated pinging of multiple devices, providing a quick assessment of network health. Results can be logged for analysis and troubleshooting.
Sending Email via Script
Automating email notifications is a valuable capability for system administrators and developers. Scripts can be configured to send emails based on specific events or conditions.
This functionality can be used for alerts, reports, or automated responses, streamlining communication and improving efficiency. Email can be sent directly from the script without manual intervention.
The Capabilities of Windows Scripting
Gaining proficiency in these individual components empowers you to integrate them into a more extensive, automated script. I will demonstrate this by presenting concise script examples that can be assembled into a highly effective automated solution. This script will accept an IP address list from a text file, verify the reachability of each device via ping, and subsequently dispatch an alert email if any devices are unresponsive.
Processing Input Files
The initial phase of this procedure involves mastering the technique of reading and interpreting data from an input text file. For this purpose, a text file named IPlist.ini has been created, located in the same directory as the script itself. This file contains a comprehensive list of the IP addresses that require verification. The following script facilitates reading each line within the text file.
<job><script language="VBScript">
Option Explicit
On Error Resume Next
Dim strHostDim 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
ElseWScript.Echo "The file was not there."
End If
WScript.Quit
</script>
</job>
This code utilizes the Windows file system object to open a file and then sequentially reads each line of text until the end of the file is reached.
Performing a Ping Test
Now that you understand how to extract each IP address from the text file, let's explore how to execute a Ping operation within Windows Scripting?
Initiating a ping is somewhat more complex than reading a text file, as it necessitates the utilization of Windows Management Instrumentation scripting (WMI). The process is outlined below.
<job><script language="VBScript">
Option Explicit
On Error Resume Next
Dim colPingResults, objPingResult, strQueryDim strIPtext
strIPtext = "192.168.1.105"
' WMI querystrQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & strIPtext & "'"
Set colPingResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )' Translate query resultsFor Each objPingResult In colPingResultsIf 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>
As you can observe, the process is remarkably straightforward. Upon execution, the script generates a pop-up message indicating whether the IP address is reachable or not.

Currently, this script pings only a single IP address. However, integrating this ping functionality into the preceding script, after each IP address is read from the text file, will enable pinging each address in your list.
Dispatching an Email Notification
Finally, while a script that checks IP addresses and displays a window upon detecting errors is beneficial, it would be even more advantageous to schedule the script to run daily or multiple times a day and automatically receive email notifications regarding any issues.
To achieve this, you need to understand how to send emails within a script. Extensive research online will reveal numerous methods for accomplishing this. The most prevalent approach involves utilizing the CDO (Collaboration Data Objects) method.
<job><script language="VBScript">
Option Explicit
On Error Resume Next
Const fromEmail = "rdxxxx@gmail.com"Const password = "xxxxxxxx"
Dim emailObj, emailConfigSet 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.ConfigurationemailConfig.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>
This script enables you to send any text as the message body in an email to any address using your Gmail credentials. You can modify the parameters to utilize any other SMTP mail server of your preference.
The final step involves combining these three code segments. Upon integration, the script will read each IP address from the list, ping each one, and then transmit the resulting string as the message body to a notification email. The resulting email will appear as follows.

This functionality proves particularly valuable in IT environments where managing a vast number of devices and servers is commonplace, and time is limited. Automating tasks with scripts that can proactively check system status is highly recommended.
Experiment with these Windows Scripts to determine if they can streamline your workflow and enhance efficiency. Are you aware of any other innovative applications of Windows Scripting? Share your insights in the comments section below.
Image credit: Mario Alberto Magallanes Trejo
Related Posts

Touchscreen on Windows PC: Do You Need It?

Find Lost Windows or Office Product Keys - Easy Guide

Windows 10 Resetting Settings: Why It Happens & How to Fix

Monitor FPS in UWP Games on Windows 10 - A Simple Guide
Remove 'Get Windows 10' Icon & Stop Upgrade Notifications
