Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script that determines the OS type and service pack

Status
Not open for further replies.

redlair

Technical User
Jun 29, 2004
54
0
0
US
Hello All,
For some time I have been using a script that determines the OS type then runs the correct code for the OS. I now need to deploy several updates that are Service Pack specific. The updates I need to deploy for example: Update 1 can run if 2k has sp3 or better, if the service pack is less then sp3 then a supporting file needs to be ran prior to running update 1. I will be working with 98, 2k, XP, 2003. Here is an example of the script I have.

------------------------------------------------------

'========= Declarations =================
Option Explicit

'Define variables
Dim sMessage
Dim sOStype
Dim sComputerRole
Dim iRetVal
Dim Mbox
Dim objEnv
Dim WshShell

'========= Main code =================

'Display a string for OS type rather than a number
iRetVal = GetOSType
Select Case iRetVal
Case 0
sOStype = "Not found"
Case 1
sOStype = "Windows 95"
Case 2
sOStype = "Windows 98"
Case 3
sOStype = "Windows Me"
Case 4
sOStype = "Windows NT"
Case 5
sOStype = "Windows 2000"
Case 6
sOStype = "Windows XP"
End Select

'Construct the info we want to show

'sMessage = sOStype & vbCr

'Display to screen
'Call MsgBox(sMessage, vbOkOnly, "OS info")

Mbox = MsgBox("Pilot VNC Install", 4, "Do you wish to install VNC ?")

If Mbox = 7 Then

WScript.Quit

End If
'Promt user about need to use District Image

Set WshShell = WScript.CreateObject("WScript.Shell")

'Set objEnv = WshShell.Environment("System")

'WScript.echo GetOSType

If GetOSType = 6 Then

WshShell.Run "SetupFiles\PilotVNC-XpSetup.exe"

Else

If GetOSType = 5 Then

WshShell.Run "SetupFiles\PilotVNC-Win2kSetup.exe"

Else

MsgBox("This is not a valid Operating System.")

End If
End If

'========= Procedures and functions ==============


Function GetOSType
'Version 1.3
'1.2 - 1.3 Renamed object variables
'1.1 - 1.2 Supports Win XP
'1.0 - 1.1 Functions now returns 0 when OS info is not found.
'Returns a integer indicating the operating system.
'Note: Win 3.11, Win NT 3.5 and Win NT 3.51 does not run WSH scripts.
'0 = Not able to find OS Type
'1 = Windows 95
'2 = Windows 98
'3 = Windows Me
'4 = Windows NT 4.0
'5 = Windows 2000
'6 = Windows XP

'Declare local variables
Dim sOStype 'Temp variable to store values from registry
Dim oWshShell 'Needs the Shell object to access registry

'Initialize
Set oWshShell = WScript.CreateObject("WScript.Shell")
GetOSType = 0

'Get OS Info from registry
On Error Resume Next
'Initially assume Win9x platform
sOStype = oWshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Version")

If sOStype = Empty Then
'If variable still empty then NT platform is used
'Read version from registry to find type of NT platform
' "CurrentVersion"="4.0"
' "CurrentVersion"="5.0"
sOStype = Trim(oWshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion"))
If sOStype = "5.1" Then
'Windows XP
GetOSType = 6
ElseIf sOStype = "5.0" Then
'Windows 2000
GetOSType = 5
ElseIf sOStype = "4.0" Then
'Windows NT 4.0
GetOSType = 4
End If
Else
'Find type of Win9x platform
' "Version"="Windows 98"
' "Version"="Windows 95"
' "Version"="Windows Millennium Edition"
If InStr(1,sOStype,"Millennium Edition",1) > 0 Then
'Windows Me
GetOSType = 3
ElseIf InStr(1,sOStype,"98",0) > 0 Then
'Windows 98
GetOSType = 2
ElseIf InStr(1,sOStype,"95",0) > 0 Then
'Windows 95
GetOSType = 1
End If
End If
On Error GoTo 0

'Clean up
Set oWshShell = Nothing
End Function
 
The way I see this, is I need to find the reg key that defines the service pack and add this to the case selection. Does this sound like I may be on the right track.

----------------------------------------------------

Set oWshShell = WScript.CreateObject("WScript.Shell")
GetOSType = 0

'Get OS Info from registry
On Error Resume Next
'Initially assume Win9x platform
sOStype = oWshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Version")

If sOStype = Empty Then
'If variable still empty then NT platform is used
'Read version from registry to find type of NT platform
' "CurrentVersion"="4.0"
' "CurrentVersion"="5.0"
sOStype = Trim(oWshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion"))
If sOStype = "5.1" Then
'Windows XP
GetOSType = 6
ElseIf sOStype = "5.0" Then
'Windows 2000
GetOSType = 5
ElseIf sOStype = "4.0" Then
'Windows NT 4.0
GetOSType = 4
End If
Else
'Find type of Win9x platform
' "Version"="Windows 98"
' "Version"="Windows 95"
' "Version"="Windows Millennium Edition"
If InStr(1,sOStype,"Millennium Edition",1) > 0 Then
'Windows Me
GetOSType = 3
ElseIf InStr(1,sOStype,"98",0) > 0 Then
'Windows 98
GetOSType = 2
ElseIf InStr(1,sOStype,"95",0) > 0 Then
'Windows 95
GetOSType = 1
End If
End If
On Error GoTo 0
 
redlair,

The effort you've put into it is not worthless. Essentially it comes back to reading registry etc. But, there are ways for quite a long while to facilitate these tasks in the modern os which present themselves as more advanced. One way is to use wmi querying win32_operatingsystem class.
Code:
set svc=getobject("winmgmts:root\cimv2")
for each os in svc.execquery("select * from win32_operatingsystem")
    msg="os caption : " & os.caption & vbcrlf & _
        "os : " & os.name & vbcrlf & "version : " & os.version & vbcrlf & _
        "service pack : " & os.servicepackmajorversion & "." & os.servicepackminorversion
next
set svc=nothing
wscript.echo msg
You have all the freedom in presentation and making use of each piece of data. For more technical detail of win32_operatingsystem class, ref to msdn documentation:

regards - tsuji
 
Ok this is what I have. The problem I have is in the If statement. I need it to compare to valuesb ut I is not working. It apears to be only looking at the first value sOSType and not sSPtype. I am so close but then so far.

If sOStype = "5.1" & sSPtype = "Service Pack 1" Then
'Windows XP SP1
GetOSType = 10
ElseIf sOStype = "5.1" & sSPtype = "Service Pack 2" Then
'Windows XP SP2
GetOSType = 9
 
Do it like this.
[tt]If sOStype = "5.1" [red]and[/red] sSPtype = "Service Pack 1" Then
'Windows XP SP1
GetOSType = 10
ElseIf sOStype = "5.1" [red]and[/red] sSPtype = "Service Pack 2" Then
'Windows XP SP2
GetOSType = 9
[blue]Else
'any other cases uncatched
End If[/blue][/tt]
 
tsuji, Thank-You. That worked great. I was able to include a case selection for every OS and Service Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top