2

As our office upgrades to Window 7, I have been tasked to update the loginscript to work with Windows 7. The creators of said script are long gone, and I am not a batch file expert.

What I am trying to do is determine the OS. As I do some network administration duties, I need to be able to log on to a server without running the login script whereas I will need to the login script to run if I log into a Windows XP or Windows 7 computer.

I found I couldn't use the VER command as Windows 7 and Windows Server 2008 return the exact same results.

This is what I have:

if exist %loginscriptdir%\sysinfo.txt goto setver
if not exist %loginscriptdir%\sysinfo.txt wmic os get name /value > %loginscriptdir%\sysinfo.txt
type %loginscriptdir%\sysinfo.txt > %loginscriptdir%\sysinfo1.txt

:setver
set WinVer=Unknown
set errorlevel=0

If %WinVer% == "Unknown" (
    findstr /c:"Windows XP Professional" %loginscriptdir%\sysinfo1.txt
    if %errorlevel%==1 set WinVer=XP
) else (
    findstr /c:"Windows 7 Enterprise" %loginscriptdir%\sysinfo1.txt
    if %errorlevel%==1 set WinVer=Win7
)

set result=false
if %WinVer% == "XP" set result=true
if %WinVer% == "Win7" set result=true
if "%result%" == "false" (
       goto skipicon1

Throughout the script, I wrote in breaks to find the values. Example:

REM -----
ECHO "%WinVer%"
ECHO "%result%"
ECHO "%errorlevel%"
ECHO Press any key to continue 4.
pause>null
REM -----

The fourth break comes at the end of the script I pasted above. These are the results:

"Unknown"
"false"
"0"
Press any key to continue 4.
tshepang
  • 12,111
  • 21
  • 91
  • 136
Mary L
  • 21
  • 1

2 Answers2

0

Here you go. This is the best way I've found to get the OS accurately from win2kpro-winserver2k10. It also tells if it's 32/64 bit and what sp is installed but you don't have to. Just check %cap% in this example.

@echo off
setlocal

call :GetOS cap bit sp
echo %cap%%bit% (%sp%)
exit /b


:GetOS caption servicepack 
setlocal
set arc=%PROCESSOR_ARCHITECTURE%
set key="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
for /f "tokens=3*" %%a in (
  'reg query %key%^|findstr /i ProductName') do set cap=%%a %%b
for /f "tokens=3*" %%a in (
  'reg query %key%^|findstr /i CSDVersion') do set sp=%%a %%b
endlocal & set %1=%cap% & set %2=%arc% & set %3=%sp%
exit /b
Matt Williamson
  • 6,947
  • 1
  • 23
  • 36
0

The issue with the code is the expansion of the %errorlevel% value. Since it is contained within a scope of parentheses, the value will not be updated till after the scope ends. Meaning that %errorlevel% will always equal its value when the scope began. To fix this you would have to use delayed expansion. setlocal enabledelayedexpansion and !errorlevel!. Here is a StackOverflow post about delayed expansion: Enable and Disable Delayed Expansion, what does it do?

You may just want to use the version detection method shown at ss64.com http://ss64.com/nt/ver.html

Here is the example from ss64.com but simplified:

@echo off
setlocal
:: Get windows Version
for /f "tokens=4,5,6 delims=[.] " %%A in ('ver') do (
set "Major=%%~A"
set "Minor=%%~B"
set "Build=%%~C"
)

if "%Major%.%Minor%"=="5.1" goto WinXP
if "%Major%.%Minor%"=="6.1" goto Win7

echo Unsupported Version Detected "%Major%.%Minor%"
goto End

:WinXP
echo Windows XP Detected
goto End

:Win7
echo Windows 7 or Server 2008 Detected
goto End

:End
endlocal
exit /b 0
Community
  • 1
  • 1
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
  • The `VER` command is a very bad way to determine version. I found that out the hard way. There are at least 2 overlaps that I've found. Checking the registry is the only fail safe way I've found so far. – Matt Williamson Mar 17 '14 at 17:27
  • @MattWilliamson The `ver` method is NOT a bad way, just different for a different goal. It is a method based upon the release number (for NT now days) that I rather prefer than product name matching. Yes, there are name overlaps, but depending on what you are trying to accomplish the name overlap does not matter. In fact, I would find name matching to be a hindrance in the event I miss a name. [Version Numbers](http://en.wikipedia.org/wiki/Windows_version_numbers#Product_progression) [Version Names](http://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions) – David Ruhmann Mar 17 '14 at 17:41
  • I agree that you need to know the caveats but they specifically said `"I found I couldn't use the VER command as Windows 7 and Windows Server 2008 return the exact same results."` – Matt Williamson Mar 17 '14 at 17:44