gpt4 book ai didi

powershell - 如何在PowerShell中比较两个查询

转载 作者:行者123 更新时间:2023-12-02 23:51:51 25 4
gpt4 key购买 nike

您好,我是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进行比较的代码,该代码将首先列出缺少的功能,然后在用户提示安装所需的功能之后,如果所有必需的羽毛都已安装,则继续执行该代码。

    知道我会怎么做吗?

    最佳答案

    有几种方法可以完成此操作。
    一种方法是使用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中的已安装功能中的所有功能。

    关于powershell - 如何在PowerShell中比较两个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57327081/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com