你好,我是新来的PowerShell和编码本身。我的任务是创建一个PowerShell脚本,它执行以下操作
到目前为止,我已经编写了以下代码:
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进行比较的代码,它将首先列出缺少的特性,然后在用户提示后安装所需的特性,如果已经安装了所有所需的羽毛,则继续编写代码。
知道我会怎么做吗?
发布于 2019-08-02 14:26:08
要做到这一点,有几种方法。一种方法是使用Compare-Object列出这些差异。
Compare-Object -ReferenceObject $b -DifferenceObject $IISFeatures -IncludeEqual另一种方法是循环遍历$IISFeatures并查看$b中的值是否在其中。
$featureNameList = $b.Name
foreach ($iisFeature in $IISFeatures) {
if ($iisFeature -notin $featureNameList){
Write-Output $iisFeature
}
}这将输出$IISFeatures列表中所有不在$b中已安装的特性中的特性。
https://stackoverflow.com/questions/57327081
复制相似问题