admin管理员组

文章数量:1400815

When I run this powershell script, the script keeps freezing. It either intermittently hangs on checkMissingFeatures(); this function calls Get-WindowsOptionalFeature

Does anybody know how to fix this issue?

$featuresList = @("IIS-WebServerRole", "IIS-WebServer", "IIS-CommonHttpFeatures", "IIS-ApplicationDevelopment", "IIS-NetFxExtensibility45", "IIS-Security", "IIS-RequestFiltering", "IIS-DefaultDocument", "IIS-ASPNET45", "IIS-ISAPIExtensions", "IIS-ISAPIFilter", "WCF-Services45", "WCF-HTTP-Activation45", "NetFx4Extended-ASPNET45", "NetFx4-AdvSrvs", "NetFx4Extended-ASPNET45", "WCF-HTTP-Activation", "WCF-NonHTTP-Activation", "NetFx3", "WAS-NetFxEnvironment", "IIS-WebServerManagementTools", "IIS-ManagementScriptingTools", "IIS-IIS6ManagementCompatibility", "IIS-ManagementConsole", "IIS-ManagementService", "IIS-WMICompatibility", "IIS-LegacyScripts", "IIS-WebSockets", "IIS-ApplicationInit", "IIS-ASPNET", "IIS-ASP", "IIS-CGI", "IIS-ServerSideIncludes", "IIS-HttpRedirect", "IIS-WebDAV", "IIS-HttpLogging", "IIS-LoggingLibraries", "IIS-RequestMonitor", "IIS-HttpTracing", "IIS-CustomLogging", "IIS-ODBCLogging", "IIS-HealthAndDiagnostics", "IIS-Performance", "IIS-HttpCompressionDynamic", "IIS-HttpCompressionStatic", "IIS-URLAuthorization", "IIS-IPSecurity", "IIS-BasicAuthentication", "IIS-CertProvider", "IIS-WIndowsAuthentication", "IIS-DigestAuthentication", "IIS-ClientCertificateMappingAuthentication", "IIS-IISCertificateMappingAuthentication", "IIS-HostableWebCore", "IIS-NetFxExtensibility")

# true means enabled
function isWinFeatEnabled($feature) {

    $str = Get-WindowsOptionalFeature -Online | Where {$_.FeatureName -eq $feature} | Select State 

    if ($str -match "Disabled") {
        return $false
    }

    return $true
}

# true means is missing...
function checkMissingFeatures($featuresList) {
   
   foreach ($feature in $featuresList) {
       
       $boolResult = isWinFeatEnabled($feature)
               
       if ($boolResult -eq $false) {
           return $true
       } else {
       }
   }

   return $false
}

function processWinFeats($featuresList, $isMissing) {
    
    if ($isMissing -eq $true) {
    
        $cmd1 = "\Windows\System32\cmd.exe "
        $cmd2 = "/C Start /WAIT DISM /LogPath:log.etw /online /NoRestart /Enable-Feature "
                
        foreach ($feature in $featuresList) {            
            $cmd2 += "/FeatureName:" + $feature + " "
        }

        $cmd2 += "& exit 99 \b"

        Write-Host "Installing Windows Features..."

        Start-Process $cmd1 $cmd2
    }    
}

Write-Host "Checking for Windows Features..."

$isMissing = checkMissingFeatures($featuresList)

Write-Host "isMissing: " $isMissing

processWinFeats $featuresList $isMissing

Is there a reason why my script is freezing?

When I run this powershell script, the script keeps freezing. It either intermittently hangs on checkMissingFeatures(); this function calls Get-WindowsOptionalFeature

Does anybody know how to fix this issue?

$featuresList = @("IIS-WebServerRole", "IIS-WebServer", "IIS-CommonHttpFeatures", "IIS-ApplicationDevelopment", "IIS-NetFxExtensibility45", "IIS-Security", "IIS-RequestFiltering", "IIS-DefaultDocument", "IIS-ASPNET45", "IIS-ISAPIExtensions", "IIS-ISAPIFilter", "WCF-Services45", "WCF-HTTP-Activation45", "NetFx4Extended-ASPNET45", "NetFx4-AdvSrvs", "NetFx4Extended-ASPNET45", "WCF-HTTP-Activation", "WCF-NonHTTP-Activation", "NetFx3", "WAS-NetFxEnvironment", "IIS-WebServerManagementTools", "IIS-ManagementScriptingTools", "IIS-IIS6ManagementCompatibility", "IIS-ManagementConsole", "IIS-ManagementService", "IIS-WMICompatibility", "IIS-LegacyScripts", "IIS-WebSockets", "IIS-ApplicationInit", "IIS-ASPNET", "IIS-ASP", "IIS-CGI", "IIS-ServerSideIncludes", "IIS-HttpRedirect", "IIS-WebDAV", "IIS-HttpLogging", "IIS-LoggingLibraries", "IIS-RequestMonitor", "IIS-HttpTracing", "IIS-CustomLogging", "IIS-ODBCLogging", "IIS-HealthAndDiagnostics", "IIS-Performance", "IIS-HttpCompressionDynamic", "IIS-HttpCompressionStatic", "IIS-URLAuthorization", "IIS-IPSecurity", "IIS-BasicAuthentication", "IIS-CertProvider", "IIS-WIndowsAuthentication", "IIS-DigestAuthentication", "IIS-ClientCertificateMappingAuthentication", "IIS-IISCertificateMappingAuthentication", "IIS-HostableWebCore", "IIS-NetFxExtensibility")

# true means enabled
function isWinFeatEnabled($feature) {

    $str = Get-WindowsOptionalFeature -Online | Where {$_.FeatureName -eq $feature} | Select State 

    if ($str -match "Disabled") {
        return $false
    }

    return $true
}

# true means is missing...
function checkMissingFeatures($featuresList) {
   
   foreach ($feature in $featuresList) {
       
       $boolResult = isWinFeatEnabled($feature)
               
       if ($boolResult -eq $false) {
           return $true
       } else {
       }
   }

   return $false
}

function processWinFeats($featuresList, $isMissing) {
    
    if ($isMissing -eq $true) {
    
        $cmd1 = "\Windows\System32\cmd.exe "
        $cmd2 = "/C Start /WAIT DISM /LogPath:log.etw /online /NoRestart /Enable-Feature "
                
        foreach ($feature in $featuresList) {            
            $cmd2 += "/FeatureName:" + $feature + " "
        }

        $cmd2 += "& exit 99 \b"

        Write-Host "Installing Windows Features..."

        Start-Process $cmd1 $cmd2
    }    
}

Write-Host "Checking for Windows Features..."

$isMissing = checkMissingFeatures($featuresList)

Write-Host "isMissing: " $isMissing

processWinFeats $featuresList $isMissing

Is there a reason why my script is freezing?

Share Improve this question edited Mar 25 at 1:34 Lex Li 63.4k11 gold badges124 silver badges161 bronze badges asked Mar 25 at 0:46 Erik343Erik343 3335 silver badges15 bronze badges 13
  • For such questions, you will have to describe clearly the underlying environments, such as Windows editions and versions when you observe an issue. – Lex Li Commented Mar 25 at 1:11
  • Ok, I’m running Windows 11, I will check the other information next morning when I get into work. – Erik343 Commented Mar 25 at 1:28
  • Overall, it is a script in bad shape. You can do a single Get-WindowsOptionalFeature -Online and then figure out the states of multiple features, but in this script it wastes all the time running that slow command over and over again. – Lex Li Commented Mar 25 at 1:40
  • Does Get-WindowsOptionalFeature require a stable Internet connection because we have a corporate firewall? – Erik343 Commented Mar 25 at 1:53
  • 1 Is it actually "hanging" or just running slowly? Add a load of console logging with timestamps - e.g. write-host "$([datetime]::Now) doing something..." and see if it shows the output. If it looks like it's hanging, add some more logging around the bit of code that appears to have frozen, and gradually narrow it down to where the bottleneck is. – mclayton Commented Mar 25 at 9:23
 |  Show 8 more comments

1 Answer 1

Reset to default 0

I found that if you disable quick edit, the powershell script works!

This is my .bat file

reg add HKCU\Console /v QuickEdit /t REG_DWORD /d 0 /f
Start "My Installer" powershell.exe -NoProfile -executionpolicy unrestricted .\INSTALL.ps1

I just edited the registry to disable quick edit:

HKEY_CURRENT_USER\Console\QuickEdit

A 1 value is enabled, and a 0 is disabled.

本文标签: windowsPowerShell script freezingStack Overflow