站长资源脚本专栏

vbs获取操作系统及其版本号

整理:jimmy2024/12/28浏览2
简介vbs获取操作系统及其版本号'**************************************'*by r05e'*操作系统及其版本号'**************************************strComputer = "."Set objWMIServic

vbs获取操作系统及其版本号

'**************************************
'*by r05e
'*操作系统及其版本号
'**************************************
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查询系统信息(名称,版本,系统盘,32or64)-wmi

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 判断系统级别版本和类型

cmd脚本:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
rem 本脚本用于确定当前nt5以上框架工作站系统的级别版本和类型,需vbs/wmi 支持
>%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!

用 vbs 调用 wmi 显示 系统 name 目的是 为了 与 通过 version 得出的系统name对比。
其实单独 获取 version 只需要 ver 命令 配套 for 就可以了,不用再有 第三个外部命令。

而且 version 确定系统name 的方法本身需要参考 ms 的系统name与version列表,有的 server 版与 workstation 版 用一个version,而 xp 更是 有 5.1 5.2 两个version
更乱的是,还有 内部 BuildNumber

cmd 下 调用 wmic 的时候 ,wmic 有一个 bug 就是 在输出 文本流 时,每一行结尾都用 软回车+硬回车 两个符号表示换行,而 软回车符号 会在 脚本中参与 set 的变量设置。
所以,我在用 wmic 的 get 的时候 都会附带一个无用的字段同时 加 /format:csv参数,让无用字段来包含换行符号,避免要采集的数据字段包含换行符。
看来 你用 findstr 正则的方式解决 还不错,就是如果涉及 其他非数字的字段就要另想办法。

在 没有 wmi 的情况下 ,通过 vbs 获取系统版本,可以通过 vbs 获取 系统 explorer.exe 的文件版本 来确定系统version ,通过 明确系统是否存在 SysWOW64 文件夹 来确定 是否为 x64.

VBS 获取指定电脑的部分信息

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()