[PowerShell Utils] Remotely install Hyper-V and Failover Cluster feature on a list of windows 2012 servers

时间:2021-06-23 15:32:48

Hello everyone, this is the second post of the series. .

 

Background

===============

In my environment, I have 15 Windows hosts. I need to configure them from the OS installation to configure fail over cluster and then create and run VMs on them. Without scripting, manually complete related tasks would be very bored and easy to make mistakes. I started using PowerShell and it really helped ease the pain.

Now, let's take a look at the second script that you will love to use.

 

What can this one do

===============

I have 15 hosts recently joined domain, and I want all of them to join a fail over cluster. The first thing I am facing is to install hyper-v and fail over cluster feature on all these 15 hosts.

Installing Hyper-V feature requires to restart the host.

This script will help you install these two features on all the hosts remotely, avoiding constantly check if the server is back online. Everything in one click!

 

Logic Intro

===============

First, the script will turn off the windows firewall on target host.

Second, you will see whether hyper-v feature is already installed. If did, continue. If not, hyper-v feature will be installed.

Hyper-V feature installation requires server restart. The script will restart the host and keep trying to contact the target server until it gets back online.

Third, check if fail over cluster feature needs to be installed.

Finally, list these two features status on this server.

All above steps will be repeated for another server.

 

Script is here

===============

PowerShell

#

#Functions defined here

#

function autoinstall-hypervfailoverfeatures ($ComputerName)

{

    #Turnoff windows firewall, or Test-Connection will fail

    Get-NetFirewallProfile -CimSession $ComputerName | Set-NetFirewallProfile -Enabled False

    Get-WindowsFeature -Name Failover-Clustering -ComputerName $ComputerName

    Get-WindowsFeature -Name hyper-v -ComputerName $ComputerName

    #Verify features and install them

    $hypervFeature = Get-WindowsFeature -Name Hyper-V -ComputerName $ComputerName

    if($hypervFeature.Installed)

    {

        Write-Host "Hyper-V installed already on $ComputerName."

    }

    else

    {

        Write-Host "Installing Hyper-V feature on $ComputerName."

        Install-WindowsFeature –Name Hyper-V -ComputerName $ComputerName -IncludeManagementTools -Restart

    }

    #Wait for restart command to take effect, or fail over cluster feature will start installing, failing the whole thing.

    Start-Sleep -s 10

    $count = 1

    While (!(Test-Connection -Cn $ComputerName -BufferSize 16 -Count 1 -Quiet))

    {

        $count++

        $totalseconds = $count * 5

        Write-Host "$ComputerName did not response to ping command."

        Write-Host "Waited $totalseconds seconds. Wait for another 5 seconds, will try again."

        Start-Sleep -s 5

    }

    Write-host "Server is now alive. Proceed......"

    $failoverclusterFeature = Get-WindowsFeature -Name Failover-Clustering -ComputerName $ComputerName

    if($failoverclusterFeature.Installed)

    {

        Write-Host "Failover cluster installed already on $ComputerName."

    }

    else

    {

        Write-Host "Installing Failover cluster feature on $ComputerName"

        Install-WindowsFeature –Name Failover-Clustering -ComputerName $ComputerName -IncludeManagementTools -Restart

    }

    Write-Host "Features status shown below:"

    Get-WindowsFeature -Name Failover-Clustering -ComputerName $ComputerName

    Get-WindowsFeature -Name hyper-v -ComputerName $ComputerName

}

function AutoInstall-HypverVFailoverFeatures-Exec

{

     param ([string[]]$computernames)

     foreach  ($computer in $computernames)

     {

        autoinstall-hypervfailoverfeatures($computer)

     }

}

#

#List all the server you want to install Hyper-V and Failover cluster

#

AutoInstall-HypverVFailoverFeatures-Exec host9,host10



 

 

At last

===============

Wensheng and I worked together on this script.

Thank you, Wensheng.

 

24th Nov Update

===============

Wensheng wrote a shorter version, here it is.

Note this is a workflow of PowerShell, and it is parallel, meaning all the hosts operates at the same time! And he used Restart-Computer cmdlet as an alternative to keep pinging target computer. A lot better.

Usage:

[PowerShell Utils] Remotely install Hyper-V and Failover Cluster feature on a list of windows 2012 servers

PowerShell

 workflow AutoInstall-HypverVFailoverFeatures-Exec

{

    param ([string[]]$computernames)

    foreach ($computer in $computernames)

    {   

        #Turnoff windows firewall

        "Turning off windows firewall on $computer."

        Get-NetFirewallProfile -CimSession $computer | Set-NetFirewallProfile -Enabled False

        #show cluster and hyper-v feature status

        "Showing features status on $computer."

        Get-WindowsFeature -Name Failover-Clustering,hyper-v -ComputerName $computer

    }

    foreach -parallel ($computer in $computernames)

    {

        #Verify features and install them

        $hypervFeature = Get-WindowsFeature -Name Hyper-V -ComputerName $computer

        if($hypervFeature.Installed)

        {

            "Hyper-V installed already on $computer."

        }

        else

        {

            "Installing Hyper-V feature on $computer."

            Install-WindowsFeature –Name Hyper-V -ComputerName $computer -IncludeManagementTools

            Restart-Computer -Wait -PSComputerName $computer -Force

        }

        $failoverclusterFeature = Get-WindowsFeature -Name Failover-Clustering -ComputerName $computer

        if($failoverclusterFeature.Installed)

        {

            "Failover cluster installed already on $computer."

        }

        else

        {

            "Installing Failover cluster feature on $computer"

            Install-WindowsFeature –Name Failover-Clustering -ComputerName $computer -IncludeManagementTools

        }

    }

    foreach  ($computer in $computernames)

    {

        "$computer Current features status shown below:"

        Get-WindowsFeature -Name Failover-Clustering,hyper-v -ComputerName $computer

    }

}
 

Happy Powershelling, everybody.