http://dieseyer.de • all rights reserved • © 2011 v11.4

'*** v9.A *** www.dieseyer.de ******************************
'
' Datei: scriptenv.vbs
' Autor: Vers 08/10/09 by M. O'Neal @ BPA
' Auf: www.dieseyer.de
'
' Gets versions of OS, WSH, WMI & ADSI on local machine
' and determines which are up-to-date.
'
'***********************************************************
' http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.scripting.vbscript&tid=9d76517b-abb2-4bcb-8f89-74d0e267d305
' http://www.tek-tips.com/viewthread.cfm?qid=641200&page=7

' Option Explicit ' Siehe http://dieseyer.de/dse-wsh-lernen.html#OptionExpl

On Error Resume Next

Const MAXIMIZE_WINDOW = 3

strComputer = "." ' local computer
strNamespace = "\root\cimv2" ' default namespace
blnWSHUpToDate = False
blnWMIUpToDate = False
blntADSIUpToDate = False

'******************************************************************************
'* Call functions to get information on default WSH host.
'* If Wscript.exe, temporarily change to Cscript.exe
'******************************************************************************

strWshHost = GetWshHost
ChangeToCscript(strWshHost)

'******************************************************************************
'* Connect to WMI Service.
'******************************************************************************

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & strNamespace)

If Err.Number <> 0 Then
Txt = Txt & vbCRLF & "Error 0x" & hex(Err.Number) & " " & _
Err.Description & ". " & VbCrLf & _
"Unable to connect to WMI. WMI may not be installed."
Err.Clear
WScript.Quit
End If


'******************************************************************************
'* Call functions to get information on OS, WSH, WMI and ADSI.
'******************************************************************************

intOSVer = GetOSVer
WScript.Echo "0046 :: " & "GetOSVer" & vbCRLF & Txt : MsgBox Txt, , "55:: " : Txt = ""

blnWSHUpToDate = GetWSHVer(intOSVer, strWshHost)
WScript.Echo "0049 :: " & "GetWSHVer" & vbCRLF & Txt : MsgBox Txt, , "55:: " : Txt = ""

blnWMIUpToDate = GetWMIVer(intOSVer)
WScript.Echo "0052 :: " & "GetWMIVer" & vbCRLF & Txt : MsgBox Txt, , "55:: " : Txt = ""

blnADSIUpToDate = GetADSIVer(intOSVer)
WScript.Echo "0055 :: " & "GetADSIVer" & vbCRLF & Txt : MsgBox Txt, , "55:: " : Txt = ""

'******************************************************************************
'* Call sub to list which versions are current.
'******************************************************************************

ListUpToDate blnWSHUpToDate, blnWMIUpToDate, blnADSIUpToDate

WScript.Echo "0063 :: " & "blnWSHUpToDate, blnWMIUpToDate, blnADSIUpToDate" & vbCRLF & Txt : MsgBox Txt, , "55:: " : Txt = ""

Txt = "ENDE"
WScript.Echo "0063 :: " & "ENDE" & vbCRLF & Txt : MsgBox Txt, , "55:: " : Txt = ""


'******************************************************************************
'* Determines WSH script host.
'******************************************************************************

Function GetWshHost()

strErrorMessage = "Could not determine default script host."
strFullName = WScript.FullName

If Err.Number <> 0 Then
Txt = Txt & vbCRLF & "Error 0x" & hex(Err.Number) & " " & _
Err.Description & ". " & VbCrLf & strErrorMessage
Err.Clear
Exit Function
End If

If IsNull(strFullName) Then
Txt = Txt & vbCRLF & strErrorMessage
Exit Function
End If

strWshHost = Right(LCase(strFullName), 11)
If Not((strWshHost = "wscript.exe") Or (strWshHost = "cscript.exe")) Then
Txt = Txt & vbCRLF & strErrorMessage
Exit Function
End If

GetWshHost = strWshHost

End Function

'******************************************************************************
'* If default Windows Script Host is Wscript, change to Cscript.
'******************************************************************************

Sub ChangeToCscript(strWshHost)

If strWshHost = "wscript.exe" Then
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /k ""cscript //h:cscript&&cscript scriptenv.vbs""", MAXIMIZE_WINDOW
If Err.Number <> 0 Then
Txt = Txt & vbCRLF & "Error 0x" & hex(Err.Number) & " occurred. " & Err.Description & ". " & VbCrLf & "Could not temporarily change the default script host to Cscript."
Err.Clear
WScript.Quit
End If
WScript.Quit
End If

End Sub

'******************************************************************************
'* Get OS information.
'******************************************************************************

Function GetOSVer()

intOSType = 0
intOSVer = 0
strOSVer = ""

Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

For Each objOperatingSystem In colOperatingSystems
Txt = Txt & vbCRLF & vbCrLf & "Operating System" & vbCrLf & _
"================" & vbCrLf & _
"Caption: " & objOperatingSystem.Caption & VbCrLf & _
"OSType: " & objOperatingSystem.OSType & VbCrLf & _
"Version: " & objOperatingSystem.Version & VbCrLf & _
"Service Pack: " & _
objOperatingSystem.ServicePackMajorVersion & _
"." & objOperatingSystem.ServicePackMinorVersion & VbCrLf & _
"Windows Directory: " & objOperatingSystem.WindowsDirectory & VbCrLf
intOSType = objOperatingSystem.OSType
strOSVer = Left(objOperatingSystem.Version, 3)
Next

Select Case intOSType
Case 16 'Windows 95
intOSVer = 1
Case 17 'Windows 98
intOSVer = 2
' Note: Windows Millennium is not included as an OSType by WMI.
Case 18
Select Case strOSVer
Case 4.0
intOSVer = 4 'Windows NT 4.0
Case 5.0
intOSVer = 5 'Windows 2000
Case 5.1
intOSVer = 6 'Windows XP
Case 5.2
intOSVer = 7 'Windows Server 2003
Case Else
intOSVer = 0 'Older or newer version
End Select
Case Else
intOSVer = 0 'Older or newer version
End Select

GetOSVer = intOSVer

End Function

'******************************************************************************
'* Display WSH information.
'******************************************************************************

Function GetWSHVer(intOSVer, strWshHost)

Txt = Txt & vbCRLF & "Windows Script Host" & vbCrLf & _
"==================="

If Not strWshHost = "" Then
strVersion = WScript.Version
strBuild = WScript.BuildVersion
Txt = Txt & vbCRLF & _
"WSH Default Script Host: " & strWshHost & VbCrLf & _
"WSH Path: " & WScript.FullName & VbCrLf & _
"WSH Version & Build: " & strVersion & "." & strBuild & VbCrLf
Else
Txt = Txt & vbCRLF & "WSH information cannot be retrieved."
End If

sngWSHVer = CSng(strVersion)
intBuild = CInt(strBuild)

If (sngWSHVer >= 5.6 And intBuild >= 8515) Then
GetWSHVer = True
Else
GetWSHVer = False
End If

End Function

'******************************************************************************
'* Display WMI information.
'******************************************************************************

Function GetWMIVer(intOSVer)

dblBuildVersion = 0

If (intOSVer >= 1 And intOSVer <= 5) Then
strWmiVer = "1.5"
ElseIf intOSVer = 6 Then
strWmiVer = "5.1"
ElseIf intOSVer = 7 Then
strWmiVer = "5.2"
Else
strWmiVer = "?.?"
End If

Set colWMISettings = objWMIService.ExecQuery _
("Select * from Win32_WMISetting")

For Each objWMISetting In colWMISettings
Txt = Txt & vbCRLF & "Windows Management Instrumentation" & vbCrLf & _
"==================================" & vbCrLf & _
"WMI Version & Build: " & _
strWmiVer & "." & objWMISetting.BuildVersion & vbCrLf & _
"Default scripting namespace: " & _
objWMISetting.ASPScriptDefaultNamespace & vbCrLf
dblBuildVersion = CDbl(objWMISetting.BuildVersion)
Next

If (intOSVer = 7 And dblBuildVersion >= 3790.0000) Or _
(intOSVer = 6 And dblBuildVersion >= 2600.0000) Or _
(intOSVer <= 5 And dblBuildVersion >= 1085.0005) _
Then
GetWMIVer = True
Else
GetWMIVer = False
End If

End Function

'******************************************************************************
'* Display ADSI provider and version information.
'******************************************************************************

Function GetADSIVer(intOSVer)

Txt = Txt & vbCRLF & "Active Directory Service Interfaces" & VbCrLf & _
"===================================" & vbCrLf

Set objShell = CreateObject("WScript.Shell")
strAdsiVer = _
objShell.RegRead("HKLM\SOFTWARE\Microsoft\Active Setup\Installed " & _
"Components\{E92B03AB-B707-11d2-9CBD-0000F87A369E}\Version")

If strAdsiVer = vbEmpty Then
strAdsiVer = _
objShell.RegRead("HKLM\SOFTWARE\Microsoft\ADs\Providers\LDAP")
If strAdsiVer = vbEmpty Then
strAdsiVer = "ADSI is not installed."
Else
strAdsiVer = "2.0"
End If
ElseIf Left(strAdsiVer, 3) = "5,0" Then
If intOSVer = 5 Then
strAdsiVer = "5.0.2195"
ElseIf intOSVer = 6 Then
strAdsiVer = "5.1.2600"
ElseIf intOSVer = 7 Then
strAdsiVer = "5.2.3790"
Else
strAdsiVer = "?.?"
End If
End If

Txt = Txt & vbCRLF & "ADSI Version & Build: " & strAdsiVer & VbCrLf

If strAdsiVer <> "ADSI is not installed." Then
Set colProvider = GetObject("ADs:")
Txt = Txt & vbCRLF & "ADSI Providers" & VbCrLf & _
"--------------"
For Each objProvider In colProvider
Txt = Txt & vbCRLF & objProvider.Name
Next
Wscript.Echo
End If

intAdsiVer = CInt(Left(strAdsiVer, 1))

If (intOSVer = 7 And intAdsiVer >= 5) Or _
(intOSVer = 6 And intAdsiVer >= 5) Or _
(intOSVer = 5 And intAdsiVer >= 5) Or _
(intOSVer = 4 And intAdsiVer >= 2) Or _
(intOSVer <= 3 And intAdsiVer >= 2) _
Then
GetADSIVer = True
Else
GetADSIVer = False
End If

End Function

'******************************************************************************
'* Test for current versions and display results.
'******************************************************************************

Sub ListUpToDate(blnWSHUpToDate, blnWMIUpToDate, blnADSIUpToDate)

Txt = Txt & vbCRLF & "Current Versions" & vbCrLf & _
"================"

If blnWSHUpToDate Then
Txt = Txt & vbCRLF & "WSH version: most recent for OS version."
Else
Txt = Txt & vbCRLF & "WSH version: not most recent for OS version."
If intOSVer = 0 Then
Txt = Txt & vbCRLF & "Windows Script not available for this OS"
Else
Txt = Txt & vbCRLF & "Get Windows Script 5.6, Build 8515"
End If
End If

If blnWMIUpToDate Then
Txt = Txt & vbCRLF & "WMI version: most recent for OS version."
Else
Txt = Txt & vbCRLF & "WMI version: not most recent for OS version."
If intOSVer = 0 Then
Txt = Txt & vbCRLF & "WMI not available for this OS"
ElseIf intOSVer >= 1 And intOSVer <= 4 Then
Txt = Txt & vbCRLF & "Get WMI CORE 1.5"
Else
End If
End If

If blnADSIUpToDate Then
Txt = Txt & vbCRLF & "ADSI version: most recent for OS version."
Else
Txt = Txt & vbCRLF & "ADSI version: not most recent for OS version."
If intOSVer = 0 Then
Txt = Txt & vbCRLF & "ADSI not available for this OS"
ElseIf intOSVer >= 1 And intOSVer <= 4 Then
Txt = Txt & vbCRLF & "Get Active Directory Client Extensions"
Else
End If
End If

End Sub

http://dieseyer.de • all rights reserved • © 2011 v11.4