通过脚本确定操作系统版本

时间:2013-04-30 14:33:22

标签: batch-file windows-scripting

我在另一个网站上发现了这个脚本,但我无法让它工作,我不知道批处理脚本那么好

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

strOSVersion = objWshShell.RegRead("HKLM\Software\Microsoft\Windows NT\CurrentVersion\CurrentVersion")

If strOSVersion = "5.1" Then
    Debug.WriteLine("Windows XP")
    Rem Insert Windows XP Statements
ElseIf strOSVersion = "6.0" Then
    Debug.WriteLine("Windows Vista")
    Rem Insert Windows Vista Statements
ElseIf strOSVersion = "6.1" Then
    Debug.WriteLine("Windows 7")
    Rem Insert Windows 7 Statements
End If

现在,如果我运行这个,我会在第二行得到错误

'strOSVersion' is not recognized as an internal or external command
operable program or batch file.
= was unexpected at this time.

我不知道为什么

3 个答案:

答案 0 :(得分:2)

这是一个VB脚本。您可以保存在名为test.vbs

的文件中

然后打开命令提示符,将目录更改为保存文件的位置。在提示符下键入cscript test.vbs

在此之前,我将Debug.WriteLine来电更改为WScript.Echo

答案 1 :(得分:2)

这是一个批处理脚本(.bat),我将它放在一起并经常用于确定操作系统。

@ECHO OFF
SetLocal

REM --------> EDIT BELOW <--------

REM Edit below if you would like to audit the pc's you run this on and store the information in a file, either T or F (for True or False)
set storeValue=T

REM Edit below the location on a network drive that you can write to
set sharePath=\\servername\sharepath

REM -----> DO NOT EDIT BELOW <-----

IF NOT EXIST C:\Temp MD C:\Temp
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" | findstr ProductName>C:\temp\osver.txt
set osver=
set tempLoc=C:\Temp
FOR /F "tokens=3* delims= " %%I IN (%tempLoc%\OSver.txt) DO SET osVer=%%I %%J
echo.-----------------------------------------
echo.  You are running: %osVer%
echo.-----------------------------------------

IF '%storeValue%'=='F' goto end
IF '%storeValue%'=='T' goto storeValue

:storeValue
ipconfig |findstr IPv4>c:\temp\ipadd.txt
REM FOR /F "tokens=12* delims=." %%A IN (%tempLoc%\IPAdd.txt) DO SET IPAdd=%%A.%%B
FOR /F "tokens=2* delims=:" %%A IN (%tempLoc%\IPAdd.txt) DO SET IPAdd=%%A
IF EXIST %sharePath%\PC_Audit_List.txt goto audit
echo.PC Audit List>%sharePath%\PC_Audit_List.txt
echo.------------------------------------------------------------->>%sharePath%\PC_Audit_List.txt
goto audit

:audit
echo.%computername%  -  %IPAdd%  -  %osVer%>>%sharePath%\PC_Audit_List.txt
goto end

:end
IF EXIST %tempLoc%\OSver.txt del %tempLoc%\OSver.txt /f /q
IF EXIST %tempLoc%\IPAdd.txt del %tempLoc%\IPAdd.txt /f /q
EndLocal
pause
exit

我确信这会满足您的需求,我已经为您提供了一个选项,您可以将IP,名称和版本写入文件。

答案 2 :(得分:0)

@echo off
setlocal EnableDelayedExpansion

::Identify OS
for /F "delims=" %%a in ('ver') do set ver=%%a
set Version=
for %%a in (95=95 98=98 ME=ME NT=NT 2000=2000 5.1.=XP 5.2.=2003 6.0.=Vista 6.1.=7 6.2.=8 6.3=8.1) do (
   if "!Version!" equ "this" (
      set Version=Windows %%a
   ) else if "!ver: %%a=!" neq "%ver%" (
      set Version=this
   )
)

::Identify bit
if exist "%SYSTEMDRIVE%\Program Files (x86)" (
   set Type=64 bit
) else (
   set Type=32 bit
)

::Display result
echo %Version% %Type%
echo/
pause

复制自:http://www.dostips.com/forum/viewtopic.php?f=3&t=4387