Spread the love

 1,070 views

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

Clap


Spread the love
Categories: Docker

Ajeet Raina

My name is Ajeet Singh Raina and I am an author of this blogging site. I am a Docker Captain, ARM Innovator & Docker Bangalore Community Leader. I bagged 2 special awards last year(2019): Firstly, “The Tip of Captain’s Hat Award” at Dockercon 2019, San Francisco, and secondly, “2019 Docker Community Award“. I run Collabnix Community Slack with over 5300+ audience . I have built popular GITHUB repositories like DockerLabs, KubeLabs, Kubetools, RedisPlanet Terraform etc. with the support of Collabnix Community. Currently working as Developer Relations Manager at Redis Labs where I help customers and community members adopt Redis. With over 12,000+ followers over LinkedIn & close to 5100+ twitter followers, I like sharing Docker and Kubernetes related content . You can follow me on Twitter(@ajeetsraina) & GitHub(@ajeetraina)

2 Comments

fodboldtrøjer · 1st April 2017 at 9:37 am

I am not sure where you’re getting your info, but great topic.
I needs to spend some time learning more or understanding more.

Thanks for great information I was looking for this info for
my mission.
fodboldtrøjer

lebron 14 · 2nd May 2017 at 11:30 pm

I would like to voice my gratitude for your kind-heartedness supporting people that really need assistance with in this subject. Your special dedication to getting the solution around has been rather beneficial and has really permitted workers much like me to reach their goals. Your own useful facts indicates this much a person like me and additionally to my office workers. Thanks a ton; from all of us.

Leave a Reply

Your email address will not be published. Required fields are marked *

Join Collabnix SlackIt's 100% FREE!

Get chance to chat with 5500+ DevOps Engineers !