【发布时间】:2019-08-02 13:04:24
【问题描述】:
您好,我是 PowerShell 和编码本身的新手。我的任务是创建一个执行以下操作的 PowerShell 脚本
- 检查是否安装了 IIS。如果没有安装 IIS,它会在服务器上停止 IIS(很简单)。
- 如果安装了 IIS,那么它会将角色服务与我们所需的角色服务列表相匹配,并安装缺失的服务。
到目前为止,我已经编写了以下代码:
Set-ExecutionPolicy Bypass -Scope Process
$IISFeatures = "Web-WebServer","Web-Common-Http","Web-Default-Doc","Web-Dir-Browsing","Web-Http-Errors","Web-Static-Content","Web-Http-Redirect","Web-Health","Web-Http-Logging","Web-Custom-Logging","Web-Log-Libraries","Web-ODBC-Logging","Web-Request-Monitor","Web-Http-Tracing","Web-Performance","Web-Stat-Compression","Web-Dyn-Compression","Web-Security","Web-Filtering","Web-Basic-Auth","Web-CertProvider","Web-Client-Auth","Web-Digest-Auth","Web-Cert-Auth","Web-IP-Security","Web-Url-Auth","Web-Windows-Auth","Web-App-Dev","Web-Net-Ext","Web-Net-Ext45","Web-AppInit","Web-Asp-Net","Web-Asp-Net45","Web-CGI","Web-ISAPI-Ext","Web-ISAPI-Filter","Web-Includes","Web-Mgmt-Tools","Web-Mgmt-Console","Web-Scripting-Tools","Web-Mgmt-Service"
$b = Get-WindowsFeature web* | Where-Object {$_.InstallState -eq 'Available'}
function InstallIIS()
{
Install-WindowsFeature -Name $IISFeatures
}
function VerifyAndInstallRoleServices()
{
}
Write-Host "`nWelcome to prerequisite installation PowerShell Script. `n`nWe will now conitnue with the installation of prerequisites`n"
$machinename = hostname
Write-Host "Verifying IIS Role and Role services`n"
if ((Get-WindowsFeature Web-Server).InstallState -eq "Installed") {
Write-Host "IIS is installed on $machinename`n"
}
else {
Write-Host "IIS is not installed on $machinename`n"
$a = Read-Host -Prompt "Press 'Y' if you want this script to install IIS for you"
if ($a -eq 'Y') {Write-Host "IIS is being installed now"}
InstallIIS
}
我想要一个将 $b 与 $IISFeatures 进行比较的代码,并首先列出缺少的功能,然后在用户提示安装所需功能后,如果已安装所有所需功能,则继续使用代码。
知道我会怎么做吗?
【问题讨论】:
标签: powershell iis windows-server-2016