Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 635
  • Last Modified:

VBScript to get OS version and use it in teh script logic

I have found several examples of this on the internet, but I'm not sure what is the best way to accomplish what I need.  I want to be able to determine if the OS of a computer is XP or Windows 7 and then run logic based on the returned OS value.  What is the best value to use and could someone give me the code that retrieves that value from the computer?
0
TPBPIT
Asked:
TPBPIT
  • 2
  • 2
  • 2
1 Solution
 
redbmasterCommented:
The best way I have found to do this was with a bat file. Here is the code I received:

ver | find "2003" > nul
if %ERRORLEVEL% == 0 goto ver_2003

ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp

ver | find "2000" > nul
if %ERRORLEVEL% == 0 goto ver_2000

ver | find "NT" > nul
if %ERRORLEVEL% == 0 goto ver_nt

if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit

systeminfo | find "OS Name" > %TEMP%\osname.txt
FOR /F "usebackq delims=: tokens=2" %%i IN (%TEMP%\osname.txt) DO set vers=%%i

echo %vers% | find "Windows 7" > nul
if %ERRORLEVEL% == 0 goto ver_7

echo %vers% | find "Windows Server 2008" > nul
if %ERRORLEVEL% == 0 goto ver_2008

echo %vers% | find "Windows Vista" > nul
if %ERRORLEVEL% == 0 goto ver_vista

goto warnthenexit

:ver_7
:Run Windows 7 specific commands here.
echo Windows 7
goto exit

:ver_2008
:Run Windows Server 2008 specific commands here.
echo Windows Server 2008
goto exit

:ver_vista
:Run Windows Vista specific commands here.
echo Windows Vista
goto exit

:ver_2003
:Run Windows Server 2003 specific commands here.
echo Windows Server 2003
goto exit

:ver_xp
:Run Windows XP specific commands here.
echo Windows XP
goto exit

:ver_2000
:Run Windows 2000 specific commands here.
echo Windows 2000
goto exit

:ver_nt
:Run Windows NT specific commands here.
echo Windows NT
goto exit

:warnthenexit
echo Machine undetermined.

:exit

Open in new window

0
 
redbmasterCommented:
Seeing as how you only care to differentiate between Win XP and 7 then you dont really need the second half of the script. The reason that is there is to differentiate between Win 7 and Server 08. Here is actually what I am using in my scripts:

ver | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp

ver | find "Vista" > nul
if %ERRORLEVEL% == 0 goto ver_vista

ver | find "7" > nul
if %ERRORLEVEL% == 0 goto ex

Open in new window

it
0
 
TPBPITAuthor Commented:
Yeah, I already have a pretty lengthy vbs script writen for XP and ust need to modify a few sections for changes in the directory structure on WIndows 7.  I need to kept it in VBS.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Bill PrewCommented:
Doing this in VBS, you can do something like this:

For Each objOS in GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem")
   strOsCaption = objOS.Caption
   strOsVersion = objOS.Version
Next
If Instr(strOsCaption, "Vista") > 0 Or Instr(strOsCaption, "Windows 7") > 0 Then
   strWinVer = "Win7"
Else
   strWinVer = "XP"
End If
Wscript.Echo strWinVer

Open in new window

~bp
0
 
TPBPITAuthor Commented:
That's what I needed.  Thanks.
0
 
Bill PrewCommented:
Welcome.

~bp
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now