传递开关(例如-includemangementtools)以将WindowsFeature作为变量安装
我用它作为一种方法,最终能够正确使用Stackoverflow 我正在尝试将以下行作为更大脚本的一部分运行:传递开关(例如-includemangementtools)以将WindowsFeature作为变量安装,windows,powershell,Windows,Powershell,我用它作为一种方法,最终能够正确使用Stackoverflow 我正在尝试将以下行作为更大脚本的一部分运行: Install-WindowsFeature Web-Server -IncludeManagementTools 这显然是独立工作的,但当我尝试将“-includemangementtools”作为变量的一部分传递时就不行了 我正在阅读要作为文本文件的一部分安装的功能列表,其中一些具有选项参数,如-includemangementtools。传递第一部分“Web服务器”可以正常工作,
Install-WindowsFeature Web-Server -IncludeManagementTools
这显然是独立工作的,但当我尝试将“-includemangementtools”作为变量的一部分传递时就不行了
我正在阅读要作为文本文件的一部分安装的功能列表,其中一些具有选项参数,如-includemangementtools。传递第一部分“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
}
这不是一个直接的答案,更像是一个建议,建议您改变设置的结构,使其更明确、更易于维护 在我看来,你会从中受益。它允许您使用哈希表保存命令的参数,这可以使动态参数更容易一些
$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
可以通过任何布尔测试进行设置。运行后
Install-WindowsFeature Web-Server -IncludeManagementTools
运行以下命令
Get-WindowsFeature
它应该显示所有安装的windows功能。“Include Management Tools”开关中包含的任何内容都会被分解,并具有自己的功能名称。因此,任何东西都可以使用“Install WindowsFeature-Name xxx”来安装。我想我会使用这种方法,使用一些逻辑来构造包含所需参数的哈希表。谢谢-我实际上使用了下面的技术来指定子功能,但这对未来来说是一个有用的思考过程。我认为这是一个非常优雅的解决方案,虽然运行Get-*实际上不起作用,但我通过通配符搜索找到了它:PS C:\Users\manniisup>Get WindowsFeature tools Display Name Install State----------------[X]管理工具Web管理工具InstalledIt是Get-WindowsFeature,而不是Get-x,因此我不会因为您误解了我的原始回复而对其进行否决。嗨,Erik,我不是有意否决的,抱歉-对Stack来说是新手,我确实理解了答案(这是一个星星而不是x)!我同意这是一个更优雅的解决方案,但你有没有尝试过,看看它是否与我描述的问题不符?