将开关(例如-IncludeManagementTools)作为变量传递给Install-WindowsFeature

时间:2018-09-07 16:01:09

标签: windows powershell

我正在使用它作为最终正确使用Stackoverflow的一种方式!

我正在尝试在较大的脚本中运行以下行:

Install-WindowsFeature Web-Server -IncludeManagementTools

这显然可以单独使用,但是当我尝试将“ -IncludeManagementTools”作为变量的一部分传递时,则不行。

我正在阅读要作为文本文件一部分安装的功能列表,其中一些具有-IncludeManagementTools之类的选项参数。传递第一部分“ Web服务器”工作正常,传递第二部分不起作用,除非直接在控制台中输入

    $var="Web-Server"        
    Install-WindowsFeature $var -IncludeManagementTools

完整的当前代码如下;我尝试对变量以及下面的内容进行拆分,并使用Switch语句而不是if循环,但是可选参数始终被误解为要安装的功能的名称,例如:

    $currentline = "Web-Server -IncludeManagementTools"
    Install-WindowsFeature $currentline.Split(" ")[0]

有效,但是

    $currentline = "Web-Server -IncludeManagementTools"
    Install-WindowsFeature $currentline.Split(" ")[0] $currentline.split(" ")[1]

失败。非常感谢您的帮助!

完整代码:

Param(
    [Parameter(Mandatory=$true,Position=1)]
    [string]$ConfigFilePath
)

$featurelist = Import-Csv $ConfigFilePath

# Install requested features
$featurelist | % {
    # The install commands have some optional switches which could be in the input file
    # to avoid an error reading in the Install-* commands, split on " " and feed into the command as seperate strings
    If ( $_.feature -contains " " ) {
        If ( $_.feature -eq "-IncludeManagementTools") {Install-WindowsFeature -Name "($_.feature.Split(" ")[0]) -IncludeManagementTools"}
        Else {}
        If ( $_.feature -eq "-IncludeAllSubFeature") { Install-WindowsFeature -Name "($_.feature.Split(" ")[0]) -IncludeAllSubfeature" }
        Else {}
        }
    Else {
        Install-WindowsFeature -Name $_.feature
    }
}

$installed = @()

# Check installs work
$featurelist | % {
    # The install commands have some optional switches which could be in the input file
    # to avoid an error reading in the Get-* commands, remove anything that comes after a space (ie effectively removing optional switches)
    $installed += Get-WindowsFeature -Name $_.feature.Split(" ")[0]
    }

$missing = $installed | ? { $_.Installed -eq $false }

If (!($missing)) {
    Write-Host "All requested features installed ok." -ForegroundColor Green -BackgroundColor Black
}
Else {
    Write-Host "Some features requested weren't installed.  They will be outputted below." -ForegroundColor Black -BackgroundColor Red
    $missing
}

2 个答案:

答案 0 :(得分:1)

运行后

Install-WindowsFeature Web-Server -IncludeManagementTools

运行以下

Get-WindowsFeature

它应该显示所有已安装的Windows功能。 “包含管理工具”开关中包含的所有内容均已分解,并具有其自己的功能名称。因此,可以使用“ Install-WindowsFeature -Name xxx”安装所有内容

答案 1 :(得分:0)

这不是直接的答案,更多是关于将设置的结构更改为更明确和更易于维护的建议。

在我看来,您将从使用splatting中受益。它使您可以使用哈希表来保存命令的参数,这可以使动态参数更加容易。

$WindowsFeatureParameters = @{
    Name = 'Web-Server'
    IncludeManagementTools = $true
}
Install-WindowsFeature @WindowsFeatureParameters

我还将更改您的CSV文件,以使每个参数都有不同的列。想象一下:

FeatureName,IncludeManagementTools,IncludeAllSubFeature
Web-Server,Yes,No

现在您可以指定:

$FeatureList = Import-Csv $ConfigFilePath 
foreach ($Feature in $FeatureList) {
    $WindowsFeatureParameters = @{
        Name = 'Web-Server'
        IncludeManagementTools = ($Feature.IncludeManagementTools -eq 'Yes')
        IncludeAllSubFeature= ($Feature.IncludeAllSubFeature -eq 'Yes')
    }
    Install-WindowsFeature @WindowsFeatureParameters
}

$Installed = foreach ($Feature in $FeatureList) {
    Get-WindowsFeature -Name $Feature.FeatureName
}

$Missing = $Installed | Where-Object Installed -eq $false

或者,您可以使用开关来指定变量以控制其值:

# Install with management tools
$ManagementTools = $true
Install-WindowsFeature 'Web-Server' -IncludeManagementTools:$ManagementTools

# Install without management tools
$ManagementTools = $false
Install-WindowsFeature 'Web-Server' -IncludeManagementTools:$ManagementTools

很显然,可以通过任何布尔测试来设置$ManagementTools