Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Photo

Check for OS Version in a script

- - - - -

  • Please log in to reply
17 replies to this topic

#1
midiboy

midiboy

    Senior Member

  • Member
  • PipPipPipPip
  • 586 posts
Hi !

Is it possible to check for the installed Windows version in a small cmd script ? I would like to check if the OS is either Windows Server 2003, Windows XP, Windows 2000 or Windows Vista.

However the variable "OS" is returning Windows_NT in all 3 cases. Any ideas ?

Thanks for your help !
Alex


How to remove advertisement from MSFN

#2
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
Try this VBS script
Save As OsName.vbs

strComputer = "." 
 Set Wmi = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
 Set colItems = Wmi.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48) 
   For Each objItem in colItems 
	msgbox "Os Name: " & objItem.Caption & " - " & objItem.Version,4128,"Os Name"
   Next


Edited by gunsmokingman, 13 October 2007 - 10:59 AM.



GunSmokingMan



#3
midiboy

midiboy

    Senior Member

  • Member
  • PipPipPipPip
  • 586 posts
Hi gunsmokingman !

You are great (as usual!) :hello:

Thanks !

Just two more questions ...

1. since I am not good in vbs ... how can I add the OS Name into a variable from inside the vbs and use that variable in a cmd ? (I would start the vbs from inside that cmd)

2. Would it be possible to just strip the output to XP, Vista, 2003 or 2000 for the 4 major OSes and not have all the subversions (like Home, Standard, build numbers etc.) in the name ? Thats because I would simply like to do different stuff for Vista than for XP/2000/2003 so I just need to know if the OS is Vista or XP or 2000/2003.

Thanks for your help !
Alex

#4
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,530 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

Will this do what you want?
 
@echo off&setlocal
for /f "delims=" %%? in ('net config work^|findstr/b Soft') do call :OSis %%?
echo/%%WinOS%% is %WinOS%
endlocal&goto :eof
:OSis
echo/%*|find "2000">nul 2>&1&&(set "WinOS=2000"&goto :eof)
echo/%*|find "2002">nul 2>&1&&(set "WinOS=XP"&goto :eof)
echo/%*|find "2003">nul 2>&1&&(set "WinOS=2003"&goto :eof)
echo/%*|find "Vista">nul 2>&1&&(set "WinOS=Vista"&goto :eof)


#5
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
Here is the VBS script inside of the cmd it passes the OS Name back to the
original cmd.
Save as OsName.cmd

@Echo Off
CLS
Color F3
Mode 55,5
Title Time
MkDir %SystemDrive%\OsTemp
Set VBS=%SystemDrive%\OsTemp\OsName.vbs
Set Cmd=%SystemDrive%\OsTemp\Name.cmd
> %VBS% Echo Dim TS 
>> %VBS% Echo strComputer = "." 
>> %VBS% Echo Set Wmi = GetObject("winmgmts:\\" ^& strComputer ^& "\root\CIMV2") 
>> %VBS% Echo Set colItems = Wmi.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48) 
>> %VBS% Echo Dim Name
>> %VBS% Echo For Each objItem in colItems :Name=objItem.Caption :Next
>> %VBS% Echo  If InStr(Name,"Vista") Then Name = "Vista"
>> %VBS% Echo  If InStr(Name,"2003") Then Name="Server 2003"
>> %VBS% Echo  If InStr(Name,"XP") Then Name="XP"
>> %VBS% Echo  If InStr(Name,"2000") Then Name="Windows 2000"
>> %VBS% Echo Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
>> %VBS% Echo Set Ts = Fso.CreateTextFile("%Cmd%")
>> %VBS% Echo Ts.writeline "Set Name=" ^& Name  '/-^> Varible For Name
>> %VBS% Echo Ts.Close
%VBS%
call %Cmd%
Del %VBS%
Del %Cmd%
RmDir %SystemDrive%\OsTemp
Echo.
Echo  -^> %Name%
Echo.
pause


Edited by gunsmokingman, 14 October 2007 - 01:42 PM.



GunSmokingMan



#6
midiboy

midiboy

    Senior Member

  • Member
  • PipPipPipPip
  • 586 posts
You are good and so very helpful !
Thanks a lot, gunsmokingman !

PS: The first cmd has a little error:

endlocal&goto :eof should be
endlocal&goto :OSis

Thanks so much !
Bye,
Alex

Edited by Yzöwl, 14 October 2007 - 03:18 AM.
blasphemous remark removed!


#7
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,530 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

PS: The first cmd has a little error:

endlocal&goto :eof should be
endlocal&goto :OSis

There's only one person who's made a little error there, and it's certainly not me!

If you can guarantee that the command interpreter, (command.com / cmd.exe), is the original one for the said OS then use ther built in ver command.
@echo offver | find "95">nulif not errorlevel 1 (	set WinOS=95	goto OSis)ver | find "98">nulif not errorlevel 1 (	set WinOS=98	goto OSis)ver | find "Millennium">nulif not errorlevel 1 (	set WinOS=ME	goto OSis)ver | find "NT">nulif not errorlevel 1 (	set WinOS=NT	goto OSis)ver | find "2000">nulif not errorlevel 1 (	set WinOS=2000	goto OSis)ver | find "XP">nulif not errorlevel 1 (	set WinOS=XP	goto OSis)ver | find "2003">nulif not errorlevel 1 (	set WinOS=2003	goto OSis)ver | find "Vista">nul	if not errorlevel 1 (	set WinOS=Vista):OSisecho.Your OS is %WinOS%


#8
midiboy

midiboy

    Senior Member

  • Member
  • PipPipPipPip
  • 586 posts
HI !

There's only one person who's made a little error there, and it's certainly not me!


I did not mean any disrespect ! :unsure:

The script just did not work here, after I changed

endlocal&goto :eof

to

endlocal&goto :OSis

it did.

But anyway, you gave me lots of options to solve the problem so Thanks again ! :hello:

#9
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,530 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

I didn't see it a disrespectful...just incorrect!

The last line of the batch file is
endlocal&goto :eof
The piece of code within/below the OSis label has already been run from the call to it earlier within the 'for in do' command.

#10
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,073 posts
  • OS:none specified
  • Country: Country Flag

The piece of code within/below the OSis label has already been run from the call to it earlier within the 'for in do' command.


Maybe the problem relies in a nationalized version of the OS midiboy is running.


Your small batch does not work on my system (italian) because of the "findstr/b Soft", changing it to "findstr/i Soft" works, as instead of "Software version" the output of the command contains "Versione del software", and thus the CALL to :OSis is never made.

jaclaz

#11
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,530 posts
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

Thanks for the information, jaclaz.

It's sometimes difficult to remember that although the forum is English speaking, our OSes aren't necessarily so.
(especially when the originator appears to use perfect English and doesn't state their origin/language in their profile!)
@echo off&setlocalfor /f "delims=" %%? in ('net config work^|findstr/i "\<soft"') do call :OSis %%?echo/%%WinOS%% is %WinOS%endlocal&goto :eof:OSisecho/%*|find "2000">nul 2>&1&&(set "WinOS=2000"&goto :eof)echo/%*|find "2002">nul 2>&1&&(set "WinOS=XP"&goto :eof)echo/%*|find "2003">nul 2>&1&&(set "WinOS=2003"&goto :eof)echo/%*|find "Vista">nul 2>&1&&(set "WinOS=Vista"&goto :eof)


#12
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,073 posts
  • OS:none specified
  • Country: Country Flag
Just as a confirmation:

findstr/i "\<soft"

works in Italian. :thumbup

jaclaz

#13
midiboy

midiboy

    Senior Member

  • Member
  • PipPipPipPip
  • 586 posts
Hi gunsmokingman !

(especially when the originator appears to use perfect English and doesn't state their origin/language in their profile!)


Thanks for the compliment (spent some time in the USA once :whistle: ) and I have updated the profile !

And sorry for drawing the wrong conclusions!
Have a nice day !

Alex

#14
midiboy

midiboy

    Senior Member

  • Member
  • PipPipPipPip
  • 586 posts
Hi again,

here´s a followup question: :blink:


Is there a way to test if the OS is 32 or 64 bit too ? At least with the net config command it does not seem to be possible ?

Thanks again !
Alex

#15
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • OS:none specified
  • Country: Country Flag
If it was for Vista then it would easy. Windows Server 2003, Windows 2000, Windows NT 4.0, Windows XP, and Windows Me/98/95. This property is not available.
Example for Vista only

strComputer = "." 
Set Wmi = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = Wmi.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48) 
   For Each objItem in colItems 
	msgbox "OS Architecture: " & objItem.OSArchitecture,4128,"OS Architecture"
   Next


There is a possible way of seeing if it running in 32 or 64 using the Processor class.
This displayed that my OS is running the CPU in 32 bit and shows I have a 64 bit cpu.

StrComputer="."
Set Wmi = GetObject("winmgmts:\\" & StrComputer & "\root\CIMV2") 
Set colItems = Wmi.ExecQuery("SELECT * FROM Win32_Processor",,48)
Dim CpuArchitecture 
   For Each objItem in colItems 
   If objItem.Architecture = 0 Then CpuArchitecture = "x86"
   If objItem.Architecture = 1 Then CpuArchitecture = "MIPS"
   If objItem.Architecture = 2 Then CpuArchitecture = "Alpha"
   If objItem.Architecture = 3 Then CpuArchitecture = "PowerPC"
   If objItem.Architecture = 6 Then CpuArchitecture = "Intel Itanium Processor Family (IPF)"
   If objItem.Architecture = 9 Then CpuArchitecture = "x64"
	MsgBox "Address Width   " & vbTab & objItem.AddressWidth & vbCrLf &_
		   "Cpu Architecture" & vbTab & CpuArchitecture,4128,"OS Architecture"
   Next




GunSmokingMan



#16
midiboy

midiboy

    Senior Member

  • Member
  • PipPipPipPip
  • 586 posts
Hi gunsmokingman,

again, thanks a lot ! How come you know all those commands ? Is there a magic book where you look these things up or do you simply know them ? :thumbup

Bye and have a nice day !
Alex

#17
Arie

Arie

    One Man Army

  • Member
  • PipPipPipPipPip
  • 835 posts
It's simply Visual Basic. Learn it ;)
Not trying to pretend the enemy that I am.

#18
midiboy

midiboy

    Senior Member

  • Member
  • PipPipPipPip
  • 586 posts
Hi Yzöwl,

I have been using your code successfully for months now:

for /f "delims=" %%? in ('net config work^|findstr/i "\<soft"') do call :OSis %%?

if %WinOS% == Vista goto :Vista
if %WinOS% == XP goto :XP

:OSis
echo/%*|find "2000">nul 2>&1&&(set "WinOS=XP"&goto :eof)
echo/%*|find "2002">nul 2>&1&&(set "WinOS=XP"&goto :eof)
echo/%*|find "2003">nul 2>&1&&(set "WinOS=XP"&goto :eof)
echo/%*|find "Vista">nul 2>&1&&(set "WinOS=Vista"&goto :eof)
echo/%*|find "2008">nul 2>&1&&(set "WinOS=Vista"&goto :eof)


Thanks again for it ! :-)

However, recently I am having a problem with this code on WinXP SP3 during runonceex. Seemingly out of nowhere the cmd stops right after this command:

for /f "delims=" %%? in ('net config work^|findstr/i "\<soft"') do call :OSis %%?

This does not happen on all the scripts where this code is present, just one. The strange thing is, if I press the ENTER key, it continues to run normally, no error appears, nothing. But it just stops there forever if noone presses a key.

Do you maybe have an explanation for it or a way around it ?

Thanks in advance,
Alex

Edited by midiboy, 27 July 2008 - 09:21 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN