Home > Mobile >  How to use dism.exe to check whether Windows Features are turned on/off on Windows 7?
How to use dism.exe to check whether Windows Features are turned on/off on Windows 7?

Time:09-17

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?

CodePudding user response:

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-CimInstnace cmdlets were available on 7, so I'll use the Get-WmiObject method:

Function Get-WmiWindowsOptionalFeatures {
  [CmdletBinding()]
  Param(
    [string]$FeatureName,
    [ValidateSet('Enabled', 'Disabled', 'Absent', 'Unknown', '1', '2', '3', '4')]
    [string]$InstallState
  )

  Get-WmiObject Win32_OptionalFeature | Where-Object {
    $feature = $_

    $featureMatch = !$FeatureName -or ( $FeatureName -and $feature.Name -like $FeatureName )
    
    $installStateMatch = switch ( $InstallState ) {

      { $_ -in 'Enabled', '1' } {
        $feature.InstallState -eq 1
        break
      }

      { $_ -in 'Disabled', '2' } {
        $feature.InstallState -eq 2
        break
      }

      { $_ -in 'Absent', '3' } {
        $feature.InstallState -eq 3
        break
      }

      { $_ -in 'Unknown', '4' } {
        $feature.InstallState -eq 4
        break
      }

      default {
        $true
        break
      }
    }

    $featureMatch -and $installStateMatch
    
  } | Select-Object Name, Caption, Description, InstallDate, @{
    Label      = 'InstallState'
    Expression = {

      switch ( $_.InstallState ) {

        1 {
          'Enabled'
          break
        }

        2 {
          'Disabled'
          break
        }

        3 {
          'Absent'
          break
        }

        4 {
          'Unknown'
          break
        }

        default {
          $_.ToString()
          break
        }
      }
    }
  }
}

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:

  • No arguments: returns all features
  • -FeatureName: returns features matching the Name. Supports -like patterns.
  • -InstallState: returns features matching the InstallState. 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):

  1. Enabled
  2. Disabled
  3. Absent
  4. Unknown

Unfortunately, there is no way to use WMI to install the features, so you'll have to install them with dism.exe.

  • Related