Reputation:
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
Upvotes: 66
Views: 172017
Reputation: 16281
I some cases, you may not need to know for its own sake, but for the sake of a dependent application. If that’s the case, the application itself might be able to tell you.
I needed an up-to-date version of .NET
in order to install an up-to-date version of SSMS. If .NET
wasn’t insyalled, or it was too old, the SSMS installer would quit with an error. You can use the error code to take remedial action.
The classic method is something like this:
ssmsInstaller.exe
if errorlevel 1 goto installDotNet
echo SSMS Installed etc
exit
:installDotNet
installDotNet.exe
echo time to reboot
I’ve abbreviated the names, of course, because that’s not the important point. What is important is that ssmsInstaller
should complete with no error (errorlevel 0
). If it fails, the error will be non-zero.
Of course, there may be other reasons for failure, but if you’re sure that the .NET
is the problem, this may do the job.
An alternative flow of logic is something like this:
ssmsInstaller.exe
if %errorlevel% ne 0 (
installDotNet.exe
echo time to reboot
)
echo SSMS Installed etc
A more compact version is:
ssmsInstaller.exe || installDotNet.exe && echo time to reboot
The syntax a || b
means only run b
if a
was unsuccessful. The syntax b && c
means only run c
if b
was successful.
Upvotes: 0
Reputation: 57252
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.
Upvotes: 0
Reputation: 686
you can have a look to this page for .NET 4 : http://www.itninja.com/question/batch-script-to-check-and-install-dotnet4-0
Upvotes: -2
Reputation: 31
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.
Upvotes: 0
Reputation: 46
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.
Upvotes: 0
Reputation: 2638
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.
Upvotes: 76
Reputation: 201
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.
Upvotes: 18
Reputation:
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.
Upvotes: 4
Reputation: 12206
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
Upvotes: 5
Reputation: 351456
Unfortunately the best way would be to check for that directory. I am not sure what you mean but "actually installed" as .NET 3.5 uses the same CLR as .NET 3.0 and .NET 2.0 so all new functionality is wrapped up in new assemblies that live in that directory. Basically, if the directory is there then 3.5 is installed.
Only thing I would add is to find the dir this way for maximum flexibility:
%windir%\Microsoft.NET\Framework\v3.5
Upvotes: 15