Windows – Programmatically check if a Windows Server 2008 Feature is installed, and Install it

installationscriptwindows-server-2008-r2

I'd like to be able to detect in a script whether a Windows Server 2008 installation has specific Windows features enabled, and install them if necessary. I do not simply want to use a "try to install the feature, even if it is already installed" approach, as I need to be able to perform additional steps at the install time.

For example, I know I can install the SNMP Service using the command line:

pkgmgr /iu:SNMP

What I don't know is how to check whether this package has been installed, ideally from VBScript/WMI, but from a command-line tool if necessary.

Ideally I also want to find a solution that does not involve Powershell, as I don't want to have to lower the powershell execution privileges that are set by default in a Windows 2008 install.

Best Answer

It seems to me that you can do this easily with a powershell script (run as administrator).

Here is an example powershell script found on the internet:

#Powershell Script To Install SNMP Services
Import-Module ServerManager
#Check If SNMP Services Are Already Installed
$check = Get-WindowsFeature | Where-Object {$_.Name -eq "SNMP-Services"}
If ($check.Installed -ne "True") {
        #Install/Enable SNMP Services
        Add-WindowsFeature SNMP-Services | Out-Null
}

You can find more information about finding out a feature's name in:
Windows Server 2008 R2: Adding Features via PowerShell

If WMIC is an option, see these articles:
New Server Core Tips
Using the new Windows Server 2008 Core OCList and OCSetup CLI tools to Add & Remove Server Roles

If programming is an option, see this stackoverflow article:
How can I programmatically check if a server feature is installed in Windows Server 2008?