Logon Script to Send Email Notifications - Gathering logon details
(Page 2 of 4 )
You begin by creating functions that gather the required information, such as account name, computer name, and IP addresses.
With scripting techniques, such as WMI, there are literally dozens of ways of retrieving this information. I’ve elected to use the simplest and most cross-platform compliant variations in order to make this script as universal as possible. If you have an alternative method that you prefer, you can surely use it as a substitute. I encourage you to try and find other ways to perform these tasks.
Set WshNetwork = CreateObject("WScript.Network")
Like all of my scripts, I begin with an area to establish global objects, variables, and constants. This script will have many of each when we’re finished. For the moment, let’s instantiate the WshNetwork object since we’ll be using it in several of our methods.
Function AccountName
If IsNull(WshNetwork) Then Set WshNetwork = CreateObject _
("WScript.Network")
AccountName = WshNetwork.UserName
End Function
Our first function is quite simple. We use the WshNetwork object’s UserName property to return the account name for the currently logged on user. This text string is set as the return value for our function.
Creating reusable code snippets is a great way to save time developing and writing code. Always keep a repository of frequently used functions and snippets. Think twice, write once!
Notice how the first line in my function checks for the existence of the WshNetwork object and creates it if it doesn’t find it. Since we’ve already added that to the beginning of our script, this isn’t necessary. However, I wanted to ensure that my functions were easily reusable in other scripts. This allows me to copy and paste this function into another script without having to worry about conflicting with any existing objects. This is the same reason that I chose to perform each of these tasks in a separate function.
Function ComputerName
If IsNull(WshNetwork) Then Set WshNetwork = CreateObject _
("WScript.Network")
ComputerName = WshNetwork.ComputerName
End Function
Just like the AccountName function, a call to the WshNetwork object’s ComputerName property returns the local machine name. Once again, we set this as your function’s return value.
Now it’s time to get IP addresses. This can be a little tricky. Assuming that the local machine is connected to a network, there could be two different IP addresses: one local network address and a WAN address, if this is a remote workstation. By retrieving both, we are able to satisfy the needs of nearly any network configuration.
In order to retrieve IP addresses, we’re forced to resort to using WMI. WMI provides much better access to hardware information than WSH is able to provide natively.
There is one slight caveat to the code I’m providing below. It only works if the IP address for the network connection is being issued dynamically using DHCP. This will not work for statically assigned addresses.
If you would still like to see the function for finding a static IP address, please find the link at the bottom of this article to join the discussion. I’ll be more than happy to provide it there.
This does pose a slight limitation that could be worked around with a function to retrieve static IPs as well. However, I deemed it a waste of time considering that most networks are using DHCP, and the fact that a network administrator should already know a statically assigned address given a computer name.
Function LAN_IP
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& strComputer & "rootcimv2")
Set colItems = objWMIService.ExecQuery _
("Select IPAddress from Win32_NetworkAdapterConfiguration” _
& ” WHERE IPEnabled=TRUE",,48)
For Each objItem In colItems
If Not IsNull(objItem.IPAddress) Then
LAN_IP = objItem.IPAddress(0)
Exit For
End If
Next
End Function
This function will return any DHCP-assigned LAN address with the help of WMI. We first connect to the cimv2 root and then query the Win32_NetworkAdapterConfiguration class. This class returns objects that represent currently enabled network adapters.
Note that I’ve written this script to return only those adapters using the TCP/IP protocol. If your network uses some other protocol such as IPX, you’ll need to remove the WHERE IPEnabled=TRUE clause from this query.
A For Each loop processes each object and returns the first listed IP address for each adapter. This only returns the first IP address for the last adapter found. This isn’t generally a problem, since there will only be one adapter in most cases.
In the event that you are using more than one adapter, you may wish to add the IP addresses to an array and have the function return as a string of those array elements instead.
Function WAN_IP
Set objxmlHTTP = CreateObject("Microsoft.XMLHTTP")
Call objxmlHTTP.open("get", "http://checkip.dyndns.org", False)
objxmlHTTP.Send()
strHTMLText = objxmlHTTP.ResponseText
Set objxmlHTTP = Nothing
If strHTMLText <> "" Then
varStart = InStr(1, strHTMLText, "Current IP Address:", _
vbTextCompare) + 19
If varStart Then varStop = InStr(varStart, strHTMLText, _
"</body>", vbTextCompare)
If varStart And varStop Then strIP = Mid(strHTMLText, varStart, _
varStop - varStart)
Else
strIP = "Unavailable"
End If
WAN_IP = Trim(strIP)
End Function
The function that returns the Wide Area Network address, or public address, is much more involved. Since there is no way to see this address locally, we must consult an outside source.
DynDNS provides a free web service that identifies the WAN IP address of the machine accessing their web page. This function requests that web page and then uses a technique known as screen-scraping to read the IP address that was sent back.
For more information about how this works, please refer to part one of my Handling Live Web Content in WSH series here on ASP Free.
Next: Creating and sending the notification email >>
More Windows Scripting Articles
More By Nilpo