Advanced VBScript book open for pre-orders

According to Microsoft Press, Advanced VBScript for Microsoft Windows Administrators, will be available for order beginning January 18, 2006. Bookpool has a pre-order link here which will save you 37% and I think free shipping. Here is the link for Borders/Amazon.

Finding Service Uptime

Ever wonder how long a particular service has been running? With WMI you can come pretty close to getting a handle on this. We start with Win32_Service to get the current process handle. Once we have that, we can query the Win32_Process class and get the creation time for that particular process. You need to know the services display name (ie “Server”) or it’s service name (ie “LANMANSERVER”).

The following script uses a WMI query to find the service based on it’s display name then finds the ProcessID. We can next run another WMI query to find the process that corresponds to the ID. Once found, we get the CreationDate attribute, run it through a function to convert the WMI time format into something a little friendlier, and we’re done.

This won’t work for every service, especially if the service runs under the context of another process. For example, “Network Connections” runs under svchost. Even when you stop the service, the underlying process is still running so when you restart the service, you can’t tell. Still for most services that run as a distinct process, these steps should work.

The script doesn’t check to see if the service is running or not so if you get a result of 0, the service probably couldn’t be found or isn’t running.


strService=”Server”
strSrv=”.”
strUserName=””
strPassword=””

WScript.Echo strService & ” service started ” &_
GetServiceUptime(strSrv,strService,strUsername,strPassword)

Function GetServiceUptime(strSrv,strService,strUsername,strPassword)
On Error Resume Next
Dim SWBemlocator,objWMI,objRef

strQuery=”Select Name,ProcessID,displayname from win32_service “&_
“where displayName='” & strService & “‘”

Set SWBemlocator = CreateObject(“WbemScripting.SWbemLocator”)
Set objWMI = SWBemlocator.ConnectServer(strSrv,”\root\cimV2″,_
strUserName,strPassword)
Set objRef=objWMI.ExecQuery(strQuery,”WQL”,48)
For Each svc In objRef
iHandle=svc.ProcessID
Next

If iHandle=”” Then
GetServiceUptime=0
Else
strQuery=”Select Handle,CreationDate from win32_process where handle='” &_
iHandle & “‘”
Set objRef=objWMI.ExecQuery(strQuery,”WQL”,48)
For Each proc In objRef
dCreated=proc.CreationDate
Next
GetServiceUptime=ConvWMITime(dCreated)
End If

End Function

Function ConvWMITime(wmiTime)
On Error Resume Next

yr = left(wmiTime,4)
mo = mid(wmiTime,5,2)
dy = mid(wmiTime,7,2)
tm = mid(wmiTime,9,6)

ConvWMITime = mo&”/”&dy&”/”&yr & ” ” & FormatDateTime(left(tm,2) & _
“:” & Mid(tm,3,2) & “:” & Right(tm,2),3)
End Function

Primal Script 4.0 Enterprise Release

SAPIEN Technologies has finally released PrimalScript4 Enterprise. The product offers a wealth of scripting power including a great script packager (convert your scripts to an exe complete with alternate credentials), a database browser (including a Visual Query Builder), ADSI and WM wizards, and a remote script execution feature. With this you can execute scripts remotely on multiple servers or workstations and report the results back to your console. I’ve you’ve ever struggled trying to get WSH Remote to work, this will be a welcomed alternative.

You can download a trial version or purchase online. A pretty complete user’s manual is available in pdf format.

Techmentor 2006 Orlando

I will be co-teaching the Scripting track at the Orlando Techmentor conference with Don Jones (March 20-24,2006). The courses will cover topics such as command line scripting, Exchange 2003 scripting, web scripting, ADSI, WMI Virtual Server 2005 and more. If you can make it down, I hope you’ll stop by and say hello.

As an extra incentive, when you register online, use vendor promotion code JDHIT. Everyone who uses this promotional code will be entered into a drawing for a free, signed copy of Advanced VBScript for Microsoft Windows Administrators (Microsoft Press 2006). I’ll also find some other goodies to send along. Be sure to enter a valid email address when you register so I can contact the winners.

You can register online for the conference. Don’t forget the code!

Advanced VBScript for Windows Administrators

I’ve written a book with Don Jones on advanced VBScript topics. The book starts with a crash course on Windows scripting so even if you’ve only had a little exposure to VBScript, WMI, or ADSI you can get up to speed quickly. In the book we cover some advanced WMI scripting techniques, getting the most out of ADSI and LDAP queries, working with HTAs and how to leverage scripting with Microsoft Exchange 2003, Microsoft Operations Manager, Virtual Server 2005 and Group Policy.

The book should be available by the end of January 2006.