VBS obtains the operating system and its version number

Time:2021-9-19

VBS obtains the operating system and its version number

?
1
2
3
4
5
6
7
8
9
10
11
12
'**************************************
'*by r05e
'* operating system and its version number
'**************************************
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
 ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
 Wscript.Echo objOperatingSystem.Caption & " " & objOperatingSystem.Version
Next

VBS query system information (name, version, system disk, 32or64) – WMI

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Function X86orX64()
 On Error Resume Next
 strComputer = "."
 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
 For Each objItem in colItems
        
  If InStr(objItem.SystemType, "64") <> 0 Then
   X86orX64 = "x64"
  Else
   X86orX64 = "x86"
  End If
 Next
 
End Function
Function showOsInfo()
 Dim res
 On Error Resume Next
 strComputer = "."
 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
 res ="infoStart"
 For Each objItem in colItems
        res =res & "_" & objItem.Caption       
        res =res & "_" & objItem.SystemDrive
        res =res & "_" & objItem.Version
        'WScript.Echo objItem.OSArchitecture 
 Next
 
    res =res & "_" & X86orX64()
    res =res & "_infoEnd"
    WScript.Echo res
End Function
call showOsInfo()
WScript.Echo "complete!"

CMD + VBS + WMI judge system level version and type

CMD script:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM this script is used to determine the level, version and type of the current NT5 or above framework workstation system, which needs VBS / WMI support
>%temp%\verx.vbs echo Set wmi=GetObject("winmgmts:\\.")
>>%temp%\verx.vbs echo Set operation=wmi.instancesof(^"win32_Operatingsystem^")
>>%temp%\verx.vbs echo For Each sys In operation
>>%temp%\verx.vbs echo   WScript.Echo sys.caption^&","^&sys.Version^&","^&sys.OSArchitecture
>>%temp%\verx.vbs echo Next
set "winver=Unkonw"
for /f "skip=2 delims=, tokens=1,2,3" %%c in ('cscript "%temp%\verx.vbs"') do (
 echo %%c,%%d,%%e
 echo ===========================================================
 for /f "delims=. tokens=1,2" %%i in ("%%d") do (
 if %%i.%%j equ 10.0 set "winver=Win_10"
 if %%i.%%j equ 6.2 set "winver=Win_8"
 if %%i.%%j equ 6.1 set "winver=Win_7"
 if %%i.%%j equ 6.0 set "winver=Win_Vista"
 if %%i.%%j equ 5.2 set "winver=Win_XP"
 if %%i.%%j equ 5.1 set "winver=Win_XP"
 if %%i.%%j equ 5.0 set "winver=Win_2K"
 )
 for /f "delims=- tokens=1" %%i in ("%%e") do (
 set "winver=!winver!.%%i"
 if %%i equ 32 set "winver=!winver:.%%i=.x86!"
 if %%i equ 64 set "winver=!winver:.%%i=.x64!"
 )
)
echo !winver!

Using VBS to call WMI to display the system name is to compare with the system name obtained through version.
In fact, to obtain version separately, you only need to use the ver command together with the for command, and there is no need for a third external command.

Moreover, the method of determining the system name by version itself needs to refer to the system name and version list of Ms. some server versions and workstation versions use one version, while XP has two versions of 5.1 and 5.2
To make matters worse, there is an internal buildnumber

When CMD calls wmic, there is a bug in wmic, that is, when outputting the text stream, the end of each line is represented by soft carriage return + hard carriage return, and the soft carriage return symbol will participate in the variable setting of set in the script.
Therefore, when I use wmic get, I will attach a useless field and add the / format: CSV parameter to make the useless field contain newline symbols to avoid that the data field to be collected contains newline characters.
It seems that it’s good for you to solve it in the regular way of findstr, that is, if other non numeric fields are involved, you have to find another way.

If there is no WMI, you can obtain the system version through VBS. You can obtain the file version of system explorer.exe through VBS to determine the system version. You can determine whether it is x64 by specifying whether there is a syswow64 folder in the system

VBS gets some information about the specified computer

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Program Name: Get_Remote_PC_Partial_Information.vbs
----The Code as Below
'--------------------------
'Author By: Wei_Zhu
'Creation Date: 2010-03-08
'--------------------------
 
On Error Resume Next
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
 
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook=objExcel.Workbooks.Add()  'This is add new
  Set objRange = objExcel.Range("A1","E1"
      objRange.Font.Size = 10
      objrange.Font.Bold = True
      objrange.Font.Name = "Times New Roman"
      objrange.Cells(1).Value="Domain"
      objrange.Cells(2).Value="IP"
      objrange.Cells(3).Value="Manufacturer"
      objrange.Cells(4).Value="Model"
      objrange.Cells(5).Value="Serial Number"
      objrange.Interior.ColorIndex = 34   'Set BackColor
       objRange.Borders.LineStyle  =  1
      Set objRange = objExcel.ActiveCell.EntireColumn
 
      objRange.AutoFit()    
 
'----Auto Fill The Column Width---
      Set objcol = objExcel.Columns("A:E").EntireColumn
      objcol.AutoFit
 
set ws=createobject("wscript.shell")
set fso=createobject("scripting.filesystemobject")
set folder=fso.getfolder(ws.CurrentDirectory&"/Computer")
 
 Set fc = folder.Files
  For Each f1 in fc
   s = folder&"/" & f1.name
  set ts = fso.OpenTextFile(s, ForReading)
   lint_line =2
   Do While Not ts.AtEndOfStream
  l_ip = ts.ReadLine
  GetPCInfo l_ip,f1.Name,lint_line
  lint_line = lint_line +1
   Loop
   lint_line=0
   ts.Close
  Next
 
   objExcel.DisplayAlerts  =  False   'Close the Alert
'objExcel.ActiveWorkBook.Saveas lstg_to_f,-4143
'msgbox lstg_to_f
   objExcel.ActiveWorkBook.Saveas folder&".xls",-4143
   objExcel.DisplayAlerts = False     ''Close the Save Alert
   objExcel.ActiveWorkbook.Close
   objExcel.DisplayAlerts = False
   objExcel.Application.Quit
 
 
Function GetPCInfo(ByVal ip, ByVal l_fn, ByVal l_line)
  'Function GetPCInfo(l_fn,l_line)
  strComputer = ip
  objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!//" & strComputer & "/root/cimv2")
 
  colItems = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")
 
  l_Array = Split(l_fn, ".", -1, 1)
 
  For Each objItem In colItems
    'MsgBox "Manufacturer: " & objItem.Manufacturer
    '   Msgbox "Product: " & objItem.Product
    '   Msgbox "Serial Number: " & objItem.SerialNumber
 
    objRange = objExcel.Range("A" & l_line, "E" & l_line)
    objRange.Cells(1).value = l_Array(0)
    objRange.Cells(2).value = ip
    objRange.Cells(3).value = objItem.Manufacturer
    objRange.Cells(4).value = objItem.Model
    objRange.Cells(5).value = objItem.SerialNumber
  Next
  '----Auto Fill The Column Width---
  objcol = objExcel.Columns("A:E").EntireColumn
  objcol.AutoFit()

This is the end of this article about VBS obtaining the operating system and its version number. For more information about VBS operating system, please search the previous articles of developeppaper or continue to browse the relevant articles below. I hope you will support developeppaper in the future!

Recommended Today

Seven Python code review tools recommended

althoughPythonLanguage is one of the most flexible development languages at present, but developers often abuse its flexibility and even violate relevant standards. So PythoncodeThe following common quality problems often occur: Some unused modules have been imported Function is missing arguments in various calls The appropriate format indentation is missing Missing appropriate spaces before and after […]