I am writing a program that needs .NET to run. When I run it on Windows 2008 Server R2 it fails upon .NET 3.5 installation. This is because in this version of Windows you can only install .NET 3.5 via Server Manager (or Role Manager).
I was wondering if there is a way to do it programmatically?
I searched the web and found that maybe I could use PowerShell or WMI. I ho开发者_开发问答ped that someone here could verify that that's the way to go, and if not, point me in the right direction.
UPDATE:
From further investigation I found that using WMIs Win32_ServerFeature_ID class I can enumerate the existing features. But I cannot find any explanation as to how to add a new feature.
Help very much needed.
Thanks.
This can be done by invoking the dism
command programmatically:
dism /Online /Enable-Feature:NetFx3
I would be interested in hearing any solutions that don't require a shell-out though.
The Win32_ServerFeature
wmi class does not expose any method to add or remove a Windows server feature, only is intended for list the features installed. and as far I know there is not a WMI class to do this task. the option which I can recomend you is use these PowerShell Cmdlets
- Add-WindowsFeature
- Get-WindowsFeature
- Remove-WindowsFeature
I know this reply is in reference to Windows 7, but for anyone looking, there is an extension that works for Windows 7. PowerShell module to Add/Remove Windows 7 features
We use PowerShell to automate server setup, you'll find it will do what you like very nicely. For the feature you're looking at the script would look something like this:
Import-Module ServerManager
$netFx = Get-WindowsFeature -Name AS-NET-Framework
if ($netFx -eq $null)
{
Add-WindowsFeature AS-NET-Framework
}
This script will be runnable via your application's installation process... probably. I've run PowerShell from Windows Installer custom actions and that's (in my experience) the toughest case.
精彩评论