I use Get-WindowsOptionalFeature to check whether the Windows feature is turned on/off successfully, but Windows 7 does not support this instruction.
$IIS_InstallPro = "IIS-WebServerRole","IIS-WebServer","IIS-CommonHttpFeatures","IIS-HttpErrors"
#$features = Get-WindowsOptionalFeature -Online -FeatureName IIS*
$features = Dism /Online /Get-Features
function CheckIIS{
foreach($feature in $features)
{
if($feature.State -eq "Disabled" -and $IIS_InstallPro -match $feature.FeatureNmae)
{
return $False
}
}
return $true
}
CheckIIS
Should I use dism.exe to check and return the result? Wanted to know if it's good practice to do that and what would be the best way to do that?
No, there is not a way to make those cmdlets work on Windows 7.
While you really shouldn't be using Windows 7 anymore, you should still be able to get this information from WMI. I don't recall if the
Get-CimInstnacecmdlets were available on 7, so I'll use theGet-WmiObjectmethod:This will give you back a nice operable object with the important fields which can be evaluated and operated upon. The class you have to inspect is
Win32_OptionalFeatures.To use the function:
-FeatureName: returns features matching theName. Supports-likepatterns.-InstallState: returns features matching theInstallState. Takes convenient strings or the numbered value as mapped below.To understand the install state, here are the possible values for each (they are stored as a
uint32):Unfortunately, there is no way to use WMI to install the features, so you'll have to install them with
dism.exe.