Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).

Automating Hyper-V VM deployment & configuration through PowerShell

2 min read

I am not Microsoft guy but recently I have been asked by my colleague to assist him in automating Hyper-V VM provisioning process. There are various resources available on the internet and I was able to bring those scripts together under one hood and was able to automate HyperV role enablement, VM creation process, choose hardware configuration, automate Windows 2k12 installation with static IP, setup AD domain.. all through PowerShell.

Here are the scripts:


Script-1 : vmcreation.ps1


#This script will automatically create 2 Hyper-V Virtual machines.
#Ensure that you have PS version 3.0 installed and Win2k12+ running as a pre-requisite.
#Please change the settings according to your need.

# Variables
$VM1 = “WinVM1”                                        # Name of VM1
$VM2 = “WinVM2”                                        # Name of VM2
$VM1RAM = 6144MB                        # RAM assigned to VM1
$VM2RAM = 6144MB                        # RAM assigned to VM2
$VM1VHD = 80GB                                # Size of Hard-Drive for VM1
$VM2VHD = 40GB                                # Size of Hard-Drive for VM2
$VMLOC = “C:\vhd”                            # Location of the VM and VHDX files
$NetworkSwitch1 = “VirtualSwitch1”
$NEtworkSwitch2 = “Corp”                    # Name of the Corporate Network Switch
$WIN2k12ISO = “C:\ISO\KC7THA00_98B7E2C4_12R2_MUI_STD.iso”               # Windows Server 2012 R2 ISO

# Create VM Folder and Network Switch
MD $VMLOC -ErrorAction SilentlyContinue
$TestSwitch = Get-VMSwitch -Name $NetworkSwitch1 -ErrorAction SilentlyContinue; if ($TestSwitch.Count -EQ 0){New-VMSwitch -Name $NetworkSwitch1 -SwitchType Private}

# Create Virtual Machines
New-VM -Name $VM1 -Path $VMLOC  -NewVHDPath $VMLOC\$VM1.vhdx -MemoryStartupBytes $VM1RAM -NewVHDSizeBytes $VM1VHD -SwitchName $NetworkSwitch1 -Generation 2
New-VM -Name $VM2 -Path $VMLOC  -NewVHDPath $VMLOC\$VM2.vhdx -MemoryStartupBytes $VM2RAM -NewVHDSizeBytes $VM2VHD -SwitchName $NetworkSwitch1 -Generation 2

# Configure Virtual Machines

#Adding DVD Drive to mount ISO

Add-VMDvdDrive -VMName $VM1
Add-VMDvdDrive -VMName $VM2

#Changing Boot Order to FirstBootDevice as DVD

$dvd = Get-VMDvdDrive -VMName $VM1
Set-VMFirmware -VMName $VM1 -FirstBootDevice $dvd
$dvd1 = Get-VMDvdDrive -VMName $VM2
Set-VMFirmware -VMName $VM2 -FirstBootDevice $dvd1

Set-VMDvdDrive -VMName $VM1 -Path $WIN2k12ISO
Set-VMDvdDrive -VMName $VM2 -Path $WIN2k12ISO

#Adding a corporate virtual switch
Add-VMNetworkAdpater -VMName $VM1 -SwitchName $VirtualSwitch2
Add-VMNetworkAdapter -VMName $VM2 -SwitchName $VirtualSwitch2

Start-VM $VM2
Start-VM $VM1


Script2 : Setup_IP_Ethernet.ps1


#A Powershell script to set Static IP for 1st Ethernet port

#Import NetAdapter Module

Import-Module NetAdapter

#Retrieve the network adapter that you want to configure

$netadapter = Get-NetAdapter -Name Ethernet

#Take input from user for network configurations

$ipaddress = Read-Host “Enter the IP for AD Server[e.g.192.168.211.x”
#example $ipaaddress = “192.168.211.20”
$ipprefix = “24”
$ipgw = Read-Host “Enter the Gateway[e.g. 192.168.211.1]”
#ipgw = “192.168.211.1”
$ipdns = Read-Host “Enter the DNS IP[e.g. 192.168.211.20]”
#ipdns = “192.168.211.20”

echo $ipaddress
echo $ipgw
echo $ipdns

#Disable DHCP server
$netadapter | Set-NetIPInterface -DHCP Disabled

#$ipif = (Get-NetAdapter).ifIndex

$netadapter | New-NetIPAddress -AddressFamily IPv4  -IPAddress $ipaddress -PrefixLength $ipprefix -DefaultGateway $ipgw

#Setting up DNS server

Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses $ipdns

#rename the computer
$newname = “delloem”
Rename-Computer -NewName $newname -force
#install features
$featureLogPath = “c:\log\featurelog.txt”
New-Item $featureLogPath -ItemType file -Force
$addsTools = “RSAT-AD-Tools”
Add-WindowsFeature $addsTools
Get-WindowsFeature | Where installed >>$featureLogPath
#restart the computer
Restart-Computer


Script3: Setup_IP_Ethernet2.ps1


#A Powershell script to set Static IP for Ethernet1

#Import NetAdapter Module

Import-Module NetAdapter

#Retrieve the network adapter that you want to configure

$netadapter = Get-NetAdapter -Name Ethernet1

#Take input from user for network configurations

$ipaddress = Read-Host “Enter the IP for AD Server[e.g.10.94.214.x”
#example $ipaaddress = “192.168.211.20”
$ipprefix = “24”
$ipgw = Read-Host “Enter the Gateway[e.g. 10.94.214.1]”
#ipgw = “192.168.211.1”
$ipdns = Read-Host “Enter the DNS IP[e.g. 163.244.180.39]”
#ipdns = “192.168.211.20”

echo $ipaddress
echo $ipgw
echo $ipdns

#Disable DHCP server
$netadapter | Set-NetIPInterface -DHCP Disabled

#$ipif = (Get-NetAdapter).ifIndex

$netadapter | New-NetIPAddress -AddressFamily IPv4  -IPAddress $ipaddress -PrefixLength $ipprefix -DefaultGateway $ipgw

#Setting up DNS server

Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses $ipdns

#rename the computer
$newname = “delloem”
Rename-Computer -NewName $newname -force
#install features
$featureLogPath = “c:\log\featurelog.txt”
New-Item $featureLogPath -ItemType file -Force
$addsTools = “RSAT-AD-Tools”
Add-WindowsFeature $addsTools
Get-WindowsFeature | Where installed >>$featureLogPath
#restart the computer
Restart-Computer


Script4: AD_Domain_setup.ps1


## This script installs Active Directory Domain Services.
#As an example, corp.dell.com is being chosen as a domain which can be changed anytime in the script.
Install-WindowsFeature ‘AD-Domain-Services’ -IncludeAllSubFeature -IncludeManagementTools

# Create New Forest, add Domain Controller
$domainname = ‘corp.dell.com’
$netbiosName = ‘DAADEnviron’
Import-Module ADDSDeployment

$ad_params = @{
CreateDnsDelegation=$false;
DatabasePath=’C:NTDS’;
DomainMode=’Win2012′;
DomainName=$domainname;
DomainNetbiosName=$netbiosName;
ForestMode=’Win2012′;
InstallDns=$true;
NoRebootOnCompletion=$false;
Force=$true;
SafeModeAdministratorPassword=(ConvertTo-SecureString ‘p@$$w0rd12’ -AsPlainText -Force);
}

Install-ADDSForest @ad_params

Script6: Setup_DHCP_DNS.ps1

## Install the bits on the server for DHCP
Install-WindowsFeature ‘DHCP’ -IncludeAllSubFeature -IncludeManagementTools

## Authorize the DHCP server in Active Directory
Add-DhcpServerInDC -DnsName test-dc.test.local -IPAddress 192.168.10.20

## Create an IPv4 DHCP scope
Add-DhcpServerv4Scope -Name ‘Test Scope’ -StartRange ‘192.168.10.100’ -EndRange ‘192.168.10.250’ -SubnetMask ‘255.255.255.0’

## Set the DNS server to use for all clients to use on the DHCP server
Set-DhcpServerv4OptionValue -DnsDomain test.local -DnsServer 192.168.10.20

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server