Ajweh's Blog

A technical perspective, from the heart of Jordan!

Browsing Posts tagged Windows Server 2008 R2

Alright folks, I’ve been goofing around with James O’Neill Powershell Module for Hyper-V and I believe it contains a very powerful set of cmdlets that will definitely save you the hassle of WMI scripting.

I create a lot of Hyper-V  VMs to test various products, some of them live for a couple of weeks, others survive only a few days.

Those VMs are based on Server 2008 R2 base disk, so every time I need a machine I have to create a new Differencing VHD that has the base disk as a parent, then create a new VM and attach the VHD to it. it was okay the first 100 times but then I thought I need an automated way of provisioning my new VMs.

Moreover, If you’re stuck in a large virtualization project, you might also consider accelerating your VM creation process, lucky for you there is SCVMM but it’s not always around.

so, the script I created will ask you for the Virtual Machine’s name, Memory Amount, and Number of CPUs, then automatically will:

  • Create A VM on a predefined path.
  • Create A Differencing VHD from a predefined parent.
  • Attach a predefined network adapter.
  • Attach the created Differencing VHD.
  • Optionally Start then Connect to the VM.

 

First you need to download the Module library from Here, then go to the directory in Program Files (typically C:\Program Files\Modules\Hyper-V” and right click each file then choose “Unblock” so PowerShell stops warning you about each file in the library.

Second, set the execution policy to Unrestricted by the running the command

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

And at Last here’s the script, remember you have to change:

  • $vmParent this is the fill path to your Parent VHD.
  • $vmPath this is your preferred VM Store.
  • $vmSwitch this is your default Virtual Network Adapter that you want to use.

 

#Script Start
Function Instantiate-VM
 {
  #Import the Hyper-V Module
  Import-Module "C:\Program Files\modules\HyperV"            

  #Set annoying error variable to "shut up" mode
  $erroractionpreference = 0            

  #Set variables
  [string]$vmSwitch = "Internal Hyper-V" #Change this to your preferred NIC.
  [string]$parentVHD = "D:\Virtual\Base\Server2008R2.vhd" #Change this to your preferred Parent VHD.
  [string]$vmName = Read-Host "Type the Virtual Machine's name"
  [string]$vmPath = "D:\Virtual\$vmName" #Change this to your preferred VM Store.            

  [int]$vmMemory = Read-Host "How much memory to assign? In MB"
  Trap [Exception]
  {
   Write-Host -BackgroundColor Red -ForegroundColor White "Error, The memory amount should be an integer!"
   break
  } #CloseTrap            

  [int]$vmCPU = Read-Host "How many CPUs to assign? Integer Number"
  Trap [Exception]
  {
   Write-Host -BackgroundColor Red -ForegroundColor White "Error, The memory amount should be an integer!"
   break
  } #CloseTrap            

  #Create Raw Virtual Machine
  New-VM -Name $vmName -Path $vmPath | Out-File -FilePath .\Log.txt -Append -width 50
  Set-VMCPUCount -VM $vmName -CPUCount $vmCPU | Out-File -FilePath .\Log.txt -Append -width 50
  Set-VMMemory -VM $vmName -Memory $vmMemory | Out-File -FilePath .\Log.txt -Append -width 50
  Add-VMNIC -VM $vmName -VirtualSwitch $vmSwitch | Out-File -FilePath .\Log.txt -Append -width 50            

  #Create a Differencing VHD
  New-VHD -VHDPaths $vmPath\$vmName.vhd -ParentVHDPath $parentVHD | Out-File -FilePath .\Log.txt -Append -width 50            

  #Check if the VHD has been created and attach it on success
  $VHDExists = Test-Path $vmPath\$vmName.vhd
  if ($VHDExists -eq $True)
    {
     Add-VMDisk -VM $vmName -ControllerID 0 -LUN 0 -Path $vmPath\$vmName.vhd
      Write-Host -BackgroundColor Green -ForegroundColor Black "Virtual Machine $vmName has been successfully created"             

    } #Close If
  else
    {
      Write-Host -BackgroundColor Red -ForegroundColor White "It seems the disk creation job has not completed, attach it manually once it's done"
    } #Close Else            

  #Start the machine
  $startMachine = Read-Host "Start and Connect to $vmName? Type [Y]ES or [N]O"
  if ($startMachine.toUpper() -eq "Y")
    {
      New-VMConnectSession -VM $vmName
      Start-VM -VM $vmName
    } #Close If            

} #Close Function            

#Call Function
Instantiate-VM            

Now save the script to a ps1 file, i.e. “CreateVM.ps1” and then use a Batch file that has a powershell command that calls the script.

This is what your Batch file should contain:

@echo off
@title Creating a New Virtual Machine
powershell -file %1 CreateVM.ps1
exit

or you know what Download both of them Here, just open up the script file and edit the paths.

Good Night wa rahmato alah wa barakatu.

  • Share/Bookmark

AGPM 4.0 Demo

2 comments

A Quick Demo of the Advanced Group Policy Management (AGPM) 4.0 in which we highlight the main features of AGPM 4.0 and some usage scenarios.

Installation wise, refer to this Demo for the walkthrough

This video is for educational purpose only, always refer to the product’s website for updates!
  • Share/Bookmark
3 visitors online now
0 guests, 3 bots, 0 members
Max visitors today: 11 at 07:34 am UTC
This month: 11 at 07-02-2014 12:10 pm UTC
This year: 54 at 02-02-2014 06:46 pm UTC
All time: 66 at 05-05-2013 10:10 am UTC