1
votes

Hi I'm having trouble understanding this code. Can someone help me explain what it does?

GetOperatingSystemVersion
If OSVersion > 6.0 Then
strCMD = "c:\Windows\System32\manage-bde.exe "
Else
strCMD = "cscript.exe c:\Windows\System32\manage-bde.wsf "
End If`

Sub GetOperatingSystemVersion

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set oss = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")`
For Each os in oss
OSVersion = Left(os.Version,3)
OSVersion = Round(OSVersion,3)
Next

End Sub

From my understanding, it will call GetOperatingSystemVersion and check the for loop. That's the part I don't understand. Thank you in advance :)

1

1 Answers

2
votes

Basically, it uses WMI to retrieve the operating system version.

The first two lines are just standard WMI-from-VBScript idioms. You'll find plenty of sample code and information on this online if you run a keyword search. The important part is Win32_OperatingSystem, which specifies the WMI class to query. Plugging that into MSDN, you'll find the documentation, including a list of the properties and methods exposed by that class.

Putting it all together, then, the syntax Select * from Win32_OperatingSystem is a query that retrieves all instances of the Win32_OperatingSystem class, including all members of that class.

The For Each loop loops through all of the items in the returned collection, one at a time. For the Win32_OperatingSystem class, this is not strictly necessary, as there should only be one global object returned. But VBScript (at least prior to Vista) didn't have any way of accessing a particular item in a collection, so you had to do it this way.

Inside of the For Each loop, the os object variable refers to an instance of the Win32_OperatingSystem class, which as the documentation reveals, exposes a Version member, which returns a string. This member is queried, and the Left function extracts the first 3 characters from its left-hand side. These 3 characters are stored in the OSVersion variable.

This is necessary because modern versions of Windows include the build number as part of the version number, resulting in a string that looks something like 6.1.7601 or 5.1.2600. The code that's going to use the version number apparently doesn't care about the build number; it just wants the major and minor version number, so it only extracts the first 3 characters from the version field.

Finally, the 3 characters stored in the OSVersion variable are rounded to 3 decimal places using the Round function, and the result is saved back into the OSVersion variable. Since it only extracted the first 3 characters from the version field, I have no idea why it's doing this.

So when the function finishes, the OSVersion variable contains the current version of the operating system, rounded to 3 decimal places. The code is fragile, though, and must be carefully maintained. What happens when Microsoft releases a version of Windows with the version number 6.10.xxxx? (Answer: This code calls it version 6.1.)