I have set of scripts for doing scripted installs. You can use the scripts on any server 2008 machine. However, I need to check if .NET 3.5 has been installed (before the scripts run) using a dos batch file. Is that possible?
I know I can check if a file in the C:\WINDOWS\Microsoft.NET\Framework\v3.5
exists, but it would be nice to have something a little more reliable.
I would like to check if it's actually installed, not just if the dir/file exists.
Thanks
This question is related to
.net
windows
batch-file
cmd
REM
Search for the CONFIG file, if this doesn't exit then the user doesn't have the .Net framework 2.0
`
SET FileName=%windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG
IF EXIST %FileName% GOTO INSTALL_DIALER
ECHO.You currently do not have the Microsoft(c) .NET Framework 2.0 installed.
You can write yourself a little console app and use System.Environment.Version to find out the version. Scott Hanselman gives a blog post about it.
Or look in the registry for the installed versions. HKLM\Software\Microsoft\NETFramework Setup\NDP
Since you said you want to know if its actually installed, I think the best way (short of running version specific code), is to check the reassuringly named "Install" registry key. 0x1 means yes:
C:\>reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5"| findstr Install
Install REG_DWORD 0x1
InstallPath REG_SZ c:\WINNT\Microsoft.NET\Framework\v3.5\
This also happens to be the "Microsoft Recommended" official method.
WMI is another possibility, but seems impractical (Slow? Takes 2 min on my C2D, SSD). Maybe it works better on your server:
C:\>wmic product where "Name like 'Microsoft .Net%'" get Name, Version
Name Version
Microsoft .NET Compact Framework 1.0 SP3 Developer 1.0.4292
Microsoft .NET Framework 3.0 Service Pack 2 3.2.30729
Microsoft .NET Framework 3.5 SP1 3.5.30729
Microsoft .NET Compact Framework 2.0 2.0.5238
Microsoft .NET Framework 4 Client Profile 4.0.30319
Microsoft .NET Framework 4 Multi-Targeting Pack 4.0.30319
Microsoft .NET Framework 2.0 Service Pack 2 2.2.30729
Microsoft .NET Framework 1.1 1.1.4322
Microsoft .NET Framework 4 Extended 4.0.30319
C:\>wmic product where "name like 'Microsoft .N%' and version='3.5.30729'" get name
Name
Microsoft .NET Framework 3.5 SP1
Other than these I think the only way to be 100% sure is to actually run a simple console app compiled targeting your framework version. Personally, I consider this unnecessary and trust the registry method just fine.
Finally, you could set up an intranet test site which is reachable from your server and sniffs the User Agent to determine .NET versions. But that's not a batch file solution of course. Also see doc here.
you can have a look to this page for .NET 4 : http://www.itninja.com/question/batch-script-to-check-and-install-dotnet4-0
You can write yourself a little console app and use System.Environment.Version to find out the version. Scott Hanselman gives a blog post about it.
Or look in the registry for the installed versions. HKLM\Software\Microsoft\NETFramework Setup\NDP
Since you said you want to know if its actually installed, I think the best way (short of running version specific code), is to check the reassuringly named "Install" registry key. 0x1 means yes:
C:\>reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5"| findstr Install
Install REG_DWORD 0x1
InstallPath REG_SZ c:\WINNT\Microsoft.NET\Framework\v3.5\
This also happens to be the "Microsoft Recommended" official method.
WMI is another possibility, but seems impractical (Slow? Takes 2 min on my C2D, SSD). Maybe it works better on your server:
C:\>wmic product where "Name like 'Microsoft .Net%'" get Name, Version
Name Version
Microsoft .NET Compact Framework 1.0 SP3 Developer 1.0.4292
Microsoft .NET Framework 3.0 Service Pack 2 3.2.30729
Microsoft .NET Framework 3.5 SP1 3.5.30729
Microsoft .NET Compact Framework 2.0 2.0.5238
Microsoft .NET Framework 4 Client Profile 4.0.30319
Microsoft .NET Framework 4 Multi-Targeting Pack 4.0.30319
Microsoft .NET Framework 2.0 Service Pack 2 2.2.30729
Microsoft .NET Framework 1.1 1.1.4322
Microsoft .NET Framework 4 Extended 4.0.30319
C:\>wmic product where "name like 'Microsoft .N%' and version='3.5.30729'" get name
Name
Microsoft .NET Framework 3.5 SP1
Other than these I think the only way to be 100% sure is to actually run a simple console app compiled targeting your framework version. Personally, I consider this unnecessary and trust the registry method just fine.
Finally, you could set up an intranet test site which is reachable from your server and sniffs the User Agent to determine .NET versions. But that's not a batch file solution of course. Also see doc here.
This is working for me:
@echo off
SETLOCAL ENABLEEXTENSIONS
echo Verify .Net Framework Version
for /f "delims=" %%I in ('dir /B /A:D %windir%\Microsoft.NET\Framework') do (
for /f "usebackq tokens=1,3 delims= " %%A in (`reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\%%I" 2^>nul ^| findstr Install`) do (
if %%A==Install (
if %%B==0x1 (
echo %%I
)
)
)
)
echo Do you see version v4.5.2 or greater in the list?
pause
ENDLOCAL
The 2^>nul
redirects errors to vapor.
REM
Search for the CONFIG file, if this doesn't exit then the user doesn't have the .Net framework 2.0
`
SET FileName=%windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG
IF EXIST %FileName% GOTO INSTALL_DIALER
ECHO.You currently do not have the Microsoft(c) .NET Framework 2.0 installed.
you can have a look to this page for .NET 4 : http://www.itninja.com/question/batch-script-to-check-and-install-dotnet4-0
You mean a DOS command such as below will do the job displaying installed .NET frameworks:
wmic /namespace:\\root\cimv2 path win32_product where "name like '%%.NET%%'" get version
The following may then be displayed:
Version
4.0.30319
WMIC is quite useful once you master using it, much easier than coding WMI in scripts depending on what you want to achieve.
This is working for me:
@echo off
SETLOCAL ENABLEEXTENSIONS
echo Verify .Net Framework Version
for /f "delims=" %%I in ('dir /B /A:D %windir%\Microsoft.NET\Framework') do (
for /f "usebackq tokens=1,3 delims= " %%A in (`reg query "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\%%I" 2^>nul ^| findstr Install`) do (
if %%A==Install (
if %%B==0x1 (
echo %%I
)
)
)
)
echo Do you see version v4.5.2 or greater in the list?
pause
ENDLOCAL
The 2^>nul
redirects errors to vapor.
If you're going to run a little console app, you may as well install clrver.exe
from the .NET SDK. I don't think you can get cleaner than that. This isn't my answer (but I happen to agree), I found it here.
you can check installed c# compilers and the printed version of the .net:
@echo off
for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*csc.exe") do (
set "l="
for /f "skip=1 tokens=2 delims=k" %%$ in ('"%%# #"') do (
if not defined l (
echo Installed: %%$
set l=%%$
)
)
)
echo latest installed .NET %l%
the csc.exe
does not have a -version
switch but it prints the .net version in its logo. You can also try with msbuild.exe but .net framework 1.* does not have msbuild.
You can write yourself a little console app and use System.Environment.Version to find out the version. Scott Hanselman gives a blog post about it.
Or look in the registry for the installed versions. HKLM\Software\Microsoft\NETFramework Setup\NDP
You mean a DOS command such as below will do the job displaying installed .NET frameworks:
wmic /namespace:\\root\cimv2 path win32_product where "name like '%%.NET%%'" get version
The following may then be displayed:
Version
4.0.30319
WMIC is quite useful once you master using it, much easier than coding WMI in scripts depending on what you want to achieve.
you can check installed c# compilers and the printed version of the .net:
@echo off
for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*csc.exe") do (
set "l="
for /f "skip=1 tokens=2 delims=k" %%$ in ('"%%# #"') do (
if not defined l (
echo Installed: %%$
set l=%%$
)
)
)
echo latest installed .NET %l%
the csc.exe
does not have a -version
switch but it prints the .net version in its logo. You can also try with msbuild.exe but .net framework 1.* does not have msbuild.
If you're going to run a little console app, you may as well install clrver.exe
from the .NET SDK. I don't think you can get cleaner than that. This isn't my answer (but I happen to agree), I found it here.
Source: Stackoverflow.com