[PowerShell Utils] Remotely install Hyper-V and Failover Cluster feature on a list of windows 2012 servers
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
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.
[PowerShell Utils] Remotely install Hyper-V and Failover Cluster feature on a list of windows 2012 servers的更多相关文章
- What is a Microsoft Failover Cluster Virtual Adapter
What is a Microsoft Failover Cluster Virtual Adapter anyway? A question often asked is, "What i ...
- Hyper V NAT 网络设置 固定IP / DHCP
Hyper V 默认的Default Switch同时支持了NAT网络以及DHCP,虚拟机能够访问外网. 但使用过程中发现这个IP网段经常变化,而且Hyper V没有提供管理其NAT网络与DHCP的图 ...
- windows server 2008 r2 企业版 hyper v做虚拟化的相关问题处理
windows server 2008 r2 企业版 hyper v做虚拟化的相关问题处理 今天在dell r710 上用windows server 2008 r2企业版hyper v 做虚拟化,添 ...
- 设置Hyper V
1.打开服务器管理器 2.添加角色和功能 3.安装类型 -> 基于角色或基于功能的安装 4.服务器选择 -> 下一步 5.服务器角色 勾选"Hyper V"
- bundle install 出现 'gem install mysql2 -v '0.3.15' succeeds before bunding '
bundle install 出现 'gem install mysql2 -v '0.3.15' succeeds before bunding ' 解决:sudo apt-get install ...
- HYPER -V 独立安装的 2016版本 中文版 下载好慢啊
HYPER -V 独立安装的 2016版本 中文版 下载好慢啊
- Powershell script to install Windows Updates (msu) from folder
######################################################### # # Name: InstallWindowsUpdates.ps1 # Auth ...
- [PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V
Hello everyone, this is the third post of the series. . Background =============== In my solution, ...
- windows rails new demo时候出错Make sure that `gem install mysql2 -v '0.3.15'` succeeds before bundling.
rails new demo --database=mysql最后报错Gem files will remain installed in D:/BillFiles/rails_dev/Ruby193 ...
随机推荐
- BZOJ.5312.冒险(线段树)
题目链接 \(Description\) 维护一个序列,支持区间and/or一个数.区间查询最大值. \(Solution\) 维护区间最大值?好像没什么用,修改的时候和暴力差不多. 我们发现有时候区 ...
- BZOJ.4517.[SDOI2016]排列计数(错位排列 逆元)
题目链接 错位排列\(D_n=(n-1)*(D_{n-1}+D_{n-2})\),表示\(n\)个数都不在其下标位置上的排列数. 那么题目要求的就是\(C_n^m*D_{n-m}\). 阶乘分母部分的 ...
- Google的开源C++单元测试框架Google Test
玩转Google开源C++单元测试框架Google Test系列(gtest)(总) 前段时间学习和了解了下Google的开源C++单元测试框架Google Test,简称gtest,非常的不错. 我 ...
- vue项目开发之v-for列表渲染的坑
不知道大家在用vue开发的过程中有没有遇到过在使用v-for的时候会出现大片的黄色警告,比如下图: 其实这是因为没有写key的原因 :key是为vue的响应式渲染提供方法,在列表中单条数据改变的情况下 ...
- MikroTik RouterOS获取在线终端和在线IP总数并自动对IP做限速(转)
1.将在线IP自动添加到地址列表 2.实时检测在线IP数 :local s 0 :foreach i in=[/ip firewall address-list find list=Online] d ...
- 使用CefSharp在.Net程序中嵌入Chrome浏览器(五)——Javascript交互
要在CEF中和网页的JS进行交互,首先我们要通过设置启用Javascrit集成功能. CefSharpSettings.LegacyJavascriptBindingEnabled = true; 调 ...
- 在centos中安装jenkins master为service
需要sudo或root权限. 翻译自: https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+Red+Hat+dis ...
- 什么是K线?K线的详解!
K线图这种图表源于日本德川幕府时代,被当时日本米市的商人用来记录米市的行情与价格波动,后因其细腻独到的标画方式而被引入到股市及期货市场.目前,这种图表分析法在我国乃至整个东南亚地区均尤为流行.由于绘制 ...
- 程序员必须知道的HTML常用代码有哪些?
HTML即超文本标记语言,是目前应用最为广泛的语言之一,是组成一个网页的主要语言.在现今这个HTML5华丽丽地占领了整个互联网的时候,如果想要通过网页抓住浏览者的眼球光靠因循守旧是不行的,程序猿们需要 ...
- 【Gitlab】从Gitlab拉取项目+往Gitlab发布项目 【GitLab自定义端口】
1>GIt需要提前安装在本地,本机,自己的电脑,开发环境电脑,IDEA所在的电脑 2>代码仓库:gitlab 3>开发工具:IDEA 4>内网搭建gitlab,访问url: h ...