如果某些Windows 10可选功能被禁用,我试图通过powershell进行检查,如果缺少任何组件,则添加所有组件。 $ required_IIS_results foreach循环可能不是最有效的方法。这就是我到目前为止所拥有的:
$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
答案 0 :(得分:1)
避免使用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"}
然后不是尝试启用所有功能,而只启用缺少的功能:
If ($required_IIS_results)
{
foreach ($MissingFeature in $required_IIS_results)
{
Enable-WindowsOptionalFeature -Online -FeatureName $MissingFeature
}
}
答案 1 :(得分:1)
为什么不检查每一个请求。功能并立即安装
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
}
答案 2 :(得分:0)
这是我在工作中寻找缺少的功能的方法。
using (var conn = new NpgsqlConnection(connString))
{
conn.Open();
using (var cmd = new NpgsqlCommand(command, conn))
...