I'm creating a script for installing the service pack for the corresponding windows version.
So I want the script to determine the Windows version I'm on and the service pack installed and then execute a command to launch the service pack installer
I know I can do wmic os get Caption
to get the window version and wmic os get ServicePackMajorVersion
to get the service pack.
And I can do start /wait %~dp0\servicepack.exe
to launch the service pack, but I don't know how to put these commands together so if I'm on Windows 7 with no service pack it will install the Windows 7 service pack, but if I'm on Windows server it will install the Windows server service pack. Any thoughts?
So I did some research and came up with this:
wmic os get Caption
if /i {Caption}=={Microsoft Windows Server 2008 *} goto :WinS
if /i {Caption}=={Microsoft Windows Vista *} goto :WinV
if /i {Caption}=={Microsoft Windows 7 *} goto :Win7
if /i {Caption}=={Microsoft Windows 8 *} goto :Win8
if /i {Caption}=={Microsoft Windows 8.1 *} goto :Win8.1
echo Windows Version not Suported!
pause
exit
:WinS
wmic os get ServicePackMajorVersion
if /i {ServicePackMajorVersion}=={0} start /wait %~dp0\servicepackSV1.exe
if /i {ServicePackMajorVersion}=={1} start /wait %~dp0\servicepackSV2.exe
if /i {ServicePackMajorVersion}=={2} goto :GoodtoGo
echo Service Pack Installed!!
goto :
:WinV
wmic os get ServicePackMajorVersion
if /i {ServicePackMajorVersion}=={0} start /wait %~dp0\servicepackSV1.exe
if /i {ServicePackMajorVersion}=={1} start /wait %~dp0\servicepackSV2.exe
if /i {ServicePackMajorVersion}=={2} goto :GoodtoGo
echo Service Pack Installed!!
goto :
:Win7
wmic os get ServicePackMajorVersion
if /i {ServicePackMajorVersion}=={0} start /wait %~dp0\servicepack7.exe
if /i {ServicePackMajorVersion}=={1} goto :GoodtoGo
echo Service Pack Installed!!
goto :
:Win8
echo No service pack needed!
goto
:Win8.1
wmic qfe get hotfixid | find "KB2919355"
if %errorlevel%==0 goto :GoodtoGo
start /wait %~dp0\win8.exe
goto :
:GoodtoGo
echo The Latest ServicePack is allready installed! you'r GoodtoGo!
goto
I Have not tested this yet, but will it work?
Best Answer
I don't know how to put these commands together
You can indeed use
wmic
to retrieve these values. However, in order to use them later in your batch file (for example in anif
expression) you need to store the values in an environment variable.Here is an example of how to do this:
Now we have the OS stored in
%_os
and the Service Pack Major Version stored in%_sp%
.Notes:
We can't use regular expressions in a string comparison, they are not supported.
The
for
command retrieves only the 3rd part (token) of the OS. This will work for the desktop versions (if you want to distinguish Server 2008 from other versions you will need to find another solution)._os
will be set to one of the following valuesServer
,Vista
,7
,8
, or8.1
, so a regular expression is no longer needed.Next we need to change the
if
statements so they use the stored variables and correctly perform the string comparisons.becomes:
etc.
Putting it all together, use the following batch file:
Notes:
goto
statements were fixed as well.What about 32 and 64 bit. How can I determine what system I'm?
Use the following batch file (GetBits.cmd):
Example output:
Further Reading