Thursday, March 30, 2017

WFA Command to install or uninstall a windows feature on a remote computer

In order to double check an iSCSI environment, one part was to make sure the Multipath-IO feature was installed.  With this command, you can install or uninstall a windows feature on a remote computer.



Download dar file




The code

param(

    [Parameter(Mandatory=$true,HelpMessage="Computername")]
    [string]$ComputerName,

    [Parameter(Mandatory=$true,HelpMessage="Credential name to make a remote connection")]
    [string]$CredentialName,

    [Parameter(Mandatory=$true,HelpMessage="Feature name")]
    [string]$FeatureName,

    [Parameter(Mandatory=$true,HelpMessage="Action")]
    [ValidateSet("install","uninstall")]
    [string]$Action,

    [Parameter(Mandatory=$false,HelpMessage="Include Management tools ?")]
    [bool]$IncludeManagementTools=$false,

    [Parameter(Mandatory=$false,HelpMessage="Include All Subfeatures ?")]
    [bool]$IncludeAllSubFeatures=$false

)


function installWindowsFeature($name,$includeMgmt,$includeSub){
    $f = getWindowsFeature -name $name
    if($f){
        if(-not $f.Installed){
            Get-WFALogger -Info -Message "Feature is not installed, installing now..."
            invoke-Command -session $session -script {param($name,$includeMgmt,$includeSub) Install-WindowsFeature -Name $name -IncludeAllSubFeature:$includeSub -IncludeManagementTools:$includeMgmt -Confirm:$false} -ArgumentList $name,$includeMgmt,$includeSub
            $f = getWindowsFeature -name $name
            if($f.Installed){
                Get-WFALogger -Info -Message "installed succesfully"
            }else{
                Throw "Failed to install"
            }
        }else{
            Get-WFALogger -Warn -Message "already installed"
        }
    }else{
        throw "No such feature exists [$name]"
    }

}
function uninstallWindowsFeature($name,$includeMgmt){
    $f = getWindowsFeature -name $name
    if($f){
        if($f.Installed){
            Get-WFALogger -Info -Message "Feature is installed, uninstalling now..."
            invoke-Command -session $session -script {param($name,$includeMgmt) Uninstall-WindowsFeature -Name $name -IncludeManagementTools:$includeMgmt -Confirm:$false} -ArgumentList $name,$includeMgmt
            $f = getWindowsFeature -name $name
            if(-not $f.Installed){
                Get-WFALogger -Info -Message "uninstalled succesfully"
            }else{
                Throw "Failed to uninstall"
            }
        }else{
            Get-WFALogger -Warn -Message "Was not installed"
        }
    }else{
        throw "No such feature exists [$name]"
    }

}
function getWindowsFeature($name){
    return Invoke-Command -Session $session -ScriptBlock {param($name) Get-WindowsFeature -Name $name} -ArgumentList $name
}

$ErrorActionPreference = "stop"

try{

    # get credentials
    $mycreds = Get-WfaCredentials $CredentialName
    
    # create remote ps session
    $session = New-PSSession -ComputerName $ComputerName -Credential $mycreds

    #install
    if($Action -eq "install"){
        installWindowsFeature -name $FeatureName -includeMgmt:$IncludeManagementTools -includeSub:$IncludeAllSubFeatures
    }else{
        uninstallWindowsFeature -name $FeatureName -includeMgmt:$IncludeManagementTools
    }

}catch{
    throw $_.Exception
}finally{
    try{
        $out = Disconnect-PSSession $session -EA SilentlyContinue
        $out = Remove-PSSession $session -EA SilentlyContinue
    }catch{
    }
}

No comments :

Post a Comment