首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何比较PowerShell中的两个查询

如何比较PowerShell中的两个查询
EN

Stack Overflow用户
提问于 2019-08-02 13:04:24
回答 1查看 202关注 0票数 2

你好,我是新来的PowerShell和编码本身。我的任务是创建一个PowerShell脚本,它执行以下操作

  1. 检查IIS是否已安装。如果未安装IIS,则会在服务器上暂停IIS (非常容易)。
  2. 如果安装了IIS,则它将角色服务与所需角色服务的列表相匹配,并安装缺少的角色服务。

到目前为止,我已经编写了以下代码:

代码语言:javascript
复制
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进行比较的代码,它将首先列出缺少的特性,然后在用户提示后安装所需的特性,如果已经安装了所有所需的羽毛,则继续编写代码。

知道我会怎么做吗?

EN

回答 1

Stack Overflow用户

发布于 2019-08-02 14:26:08

要做到这一点,有几种方法。一种方法是使用Compare-Object列出这些差异。

代码语言:javascript
复制
Compare-Object -ReferenceObject $b -DifferenceObject $IISFeatures -IncludeEqual

另一种方法是循环遍历$IISFeatures并查看$b中的值是否在其中。

代码语言:javascript
复制
$featureNameList = $b.Name
foreach ($iisFeature in $IISFeatures) {
    if ($iisFeature -notin $featureNameList){
        Write-Output $iisFeature
    }
}

这将输出$IISFeatures列表中所有不在$b中已安装的特性中的特性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57327081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档