【问题标题】:How to compare two queries in PowerShell如何在 PowerShell 中比较两个查询
【发布时间】:2019-08-02 13:04:24
【问题描述】:

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

  1. 检查是否安装了 IIS。如果没有安装 IIS,它会在服务器上停止 IIS(很简单)。
  2. 如果安装了 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


    【解决方案1】:

    有几种方法可以做到这一点。 一种是使用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 中找到的已安装功能中的所有功能。

    【讨论】:

      猜你喜欢
      • 2011-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      相关资源
      最近更新 更多