0

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?

Filburt
  • 17,626
  • 12
  • 64
  • 115
Bee
  • 3
  • 1
  • Windows 7 has been totally out of support since [January, 2020](https://support.microsoft.com/en-us/windows/windows-7-support-ended-on-january-14-2020-b75d4580-2cc7-895a-2c9c-1466d9a53962), and will not receive security patches anymore. Consider updating to supported version instead. What's more, the question is off-topic as it's about how to manage OS features and better on-topic at SuperUser. – vonPryz Sep 15 '21 at 09:09
  • While I agree OP should use a supported OS, I disagree that this isn't on topic for [so]. From a software engineering standpoint, an API OP wants to use is unavailable in an older runtime and they want to understand how to solve their issue within their own constraints. They may have a better audience at [su], but especially when it comes to shell languages there is some acceptable software-topic overlap when it comes to programmatically accessing OS features, running external commands, and issues with utilities commonly used during automation or within the software development lifecycle. – codewario Sep 16 '21 at 11:41

1 Answers1

0

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.

codewario
  • 19,553
  • 20
  • 90
  • 159
  • thank you! My main puzzle is to check the startup status of windows features. Installation is not difficult, your answer perfectly solved my problem. –  Bee Sep 16 '21 at 04:09