OS name

There are several ways one can get the name of the OS you are working with. One rather unusual method is from win32_process. The property is called simply, osname. The bad news, is it also returns the path to where this "instance" of the OS resides. No problem, I thought. I will simply parse out the extra info, and have a cute script.

  1. After making the connection into WMI, we use the execquery method to return the OSname.
  2. I then go into a function to find the | character. The instr function will return the position the | character was found.
  3. Then I use the mid command to trim it out.


Option Explicit
'On Error Resume Next
dim strComputer
dim wmiNS
dim wmiQuery
dim objWMIService
dim colItems
dim objItem
Dim objOSname, strResult, strLen, strTrim

strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Select osname from win32_process"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.execquery(wmiQuery)

for Each objItem in colItems
'Wscript.Echo ": " & objItem.osname
objOSname = objItem.osname

WScript.echo strOSname(objOSname)

Function strOSname (objOSname)
strResult = InStr(objOSname, "|")
strTrim = strResult -1
strOSname = Mid(objOSname, 1, strTrim)
End function