Passing switches ( eg -IncludeManagementTools) to Install-WindowsFeature as variables

Ian

New Member
#1
I'm using this as a way of finally getting around to using Stackoverflow properly!

I'm trying to run the following line as part of a larger script:
Mã:
Install-WindowsFeature Web-Server -IncludeManagementTools
WHich obviously works on it's own, but not when I try and pass "-IncludeManagementTools" as part of a variable.
I'm reading in the list of features to install as part of a text file, and some of these have option parameters like -IncludeManagementTools . Passing the first part "Web-Server" works fine, passing the second part doesn't, unless typed in directly to the console, ie
Mã:
    $var="Web-Server"       
    Install-WindowsFeature $var -IncludeManagementTools
Full current code is below; I've tried splitting on the variable as well as what is below, and using Switch statements rather than if loops, but the optional parameter always gets misinterpreted as the name of the feature to install eg:
Mã:
    $currentline = "Web-Server -IncludeManagementTools"
    Install-WindowsFeature $currentline.Split(" ")[0]
Works, but
Mã:
    $currentline = "Web-Server -IncludeManagementTools"
    Install-WindowsFeature $currentline.Split(" ")[0] $currentline.split(" ")[1]
Fails. ANy help greatly appreciated!
Full code:
Mã:
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
}
 

Admin

Administrator
Thành viên BQT
#2
After running
Mã:
Install-WindowsFeature Web-Server -IncludeManagementTools
Run the following
Mã:
Get-WindowsFeature
It should show all windows features installed. Whatever is included in the 'Include Management Tools' switch is broken down and has it's own Feature Name. So everything can be installed with 'Install-WindowsFeature -Name'
 
OP
OP
I

Ian

New Member
#3
I think this is really elegant solution, although running the Get-* doesn't actually work, but I found it with a wildcard search: PS C:\Users\manniisup> Get-WindowsFeature tools Display Name Name Install State ------------ ---- ------------- [X] Management Tools Web-Mgmt-Tools Installed
 
Top