Reputation: 1
I'm trying to check via powershell if some windows 10 optional features are disabled and add all components if any are missing. The $required_IIS_results foreach loop may not be the most efficient method. Here's what I have so far:
$required_IIS_features = @(
# Windows 10 IIS Features
"IIS-WebServerRole",
"IIS-WebServer",
"IIS-WebServerManagementTools",
"IIS-ManagementConsole",
"IIS-CommonHttpFeatures",
"IIS-CGI",
"IIS-HttpRedirect",
"IIS-IPSecurity"
)
Function Confirm-IISPrerequisites {
#Check to see if IIS components are installed
Write-Host "## Determining if all necessary IIS components have been installed" -ForegroundColor Green
$required_IIS_results = ForEach ($feature in $required_IIS_features) {Get-WindowsOptionalFeature -Online -FeatureName $feature | Where-Object {$_.State -eq "Disabled"}}
If($required_IIS_results){
Write-Host "Installing Required IIS and CGI module" -ForegroundColor Yellow
Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole
Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServer
Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerManagementTools
Enable-WindowsOptionalFeature -Online -FeatureName IIS-ManagementConsole
Enable-WindowsOptionalFeature -Online -FeatureName IIS-CommonHttpFeatures
Enable-WindowsOptionalFeature -Online -FeatureName IIS-CGI
Enable-WindowsOptionalFeature -Online -FeatureName IIS-HttpRedirect
Enable-WindowsOptionalFeature -Online -FeatureName IIS-IPSecurity
} Else {
Write-Host "## All of the Necessary IIS Role Services have been installed" -ForegroundColor Green
}
}
#execute function
Confirm-IISPrerequisites
Upvotes: 0
Views: 2594
Reputation: 1
Here is what I did at work to search for missing features.
$L = (Get-WindowsOptionalFeature -Online | where FeatureName -eq Printing-
Foundation-LPDPrintService)
$M = (Get-WindowsOptionalFeature -Online | where FeatureName -eq Printing-
Foundation-LPRPortMonitor)
if ($L.state -eq "disabled"){enable-windowsoptionalfeature -Online -
FeatureName "Printing-Foundation-LPDPrintService"}
if ($M.state -eq "disabled"){enable-windowsoptionalfeature -Online -
FeatureName "Printing-Foundation-LPRPortMonitor"}
Upvotes: 0
Reputation: 1356
a way to avoid foreach:
$required_IIS_features = @(
# Windows 10 IIS Features
"IIS-WebServerRole",
"IIS-WebServer",
"IIS-WebServerManagementTools",
"IIS-ManagementConsole",
"IIS-CommonHttpFeatures",
"IIS-CGI",
"IIS-HttpRedirect",
"IIS-IPSecurity"
)
$required_IIS_results = Get-WindowsOptionalFeature -FeatureName IIS* -Online | Where-Object {$_.FeatureName -in $required_IIS_features -and $_.state -eq "Disabled"}
and then instead of trying to enable everything, enable only the features that were missing:
If ($required_IIS_results)
{
foreach ($MissingFeature in $required_IIS_results)
{
Enable-WindowsOptionalFeature -Online -FeatureName $MissingFeature
}
}
Upvotes: 2
Reputation:
Why not check every req. feature and install immediately
Function Confirm-IISPrerequisites {
#Check to see if IIS components are installed
Write-Host "## Determining if all necessary IIS components have been installed" -ForegroundColor Green
ForEach ($feature in $required_IIS_features) {
IF ((Get-WindowsOptionalFeature -Online -FeatureName $feature).State -eq "Disabled"){
Write-Host "$($feature) missing - installing" -ForegroundColor Red
Enable-WindowsOptionalFeature -Online -FeatureName $feature
}
}
Write-Host "## All of the Necessary IIS Role Services have been installed" -ForegroundColor Green
}
Upvotes: 1