Use PowerShell to Add VHDs and Configure New Virtual Machines

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create and to add new virtual hard disks while configuring new virtual machines on Hyper-V. Microsoft Scripting Guy, Ed Wilson, is here. Yesterday was a holiday in the U.S. People in Charlotte, North Carolina were outside cooking hot dogs on their barbeque qrills, and on the north side of the city, it was a NASCAR race weekend. Personally, I thought it was a great time to spend at home and play around with Windows PowerShell. The Scripting Wife was also working with Windows PowerShell for her entry for the Scripting Games. So, at least around our household, it was a Windows PowerShell holiday.

Modifying virtual machine storage

One of the things that I like to do is to create a small virtual disk and attach it to a newly created virtual machine on Hyper-V. (Luckily, I can do all this on my laptop running Windows 8). In my previous blog post, Use PowerShell to Build Multiple Virtual Machines, I talked about using New-VM, Set-VMMemory, and Set-VMDvdDrive to perform the initial build of a series of virtual machines. Today I am going to use three more cmdlets: New-VHD, Add-VMHardDiskDrive, and Set-VM. I will create a new VHD for my virtual machine to use for storage, add it to the virtual machine, and then while I am playing around, I am going to change the default snapshot storage location and change the smart paging file location. In the end, I will provide a link to my modified Create-VM script in the Scripting Guys Script Repository.

Create a new VHD

To create a new VHD, I use the New-VHD cmdlet. I need to specify the path to the VHD (including the name for the newly created VHD, in addition to the size of the disk and if I want it to be a dynamic disk. In my command (because I contain it inside a loop that creates the initial virtual machines), I do not have a specific name for the newly created VHD. I will use the name of the virtual machine, and append disk2.vhdx to the end of the file name. The thing that is perhaps a bit cool is the use of the escape ( ` ) character to separate the variable name $n from the rest of the text used to create the file name. Here is the command I use:

New-VHD -Path “F:VM$nVirtual Hard Disks$n`Disk2.vhdx” -Dynamic -SizeBytes 50MB

Add the newly created VHD to the virtual machine

To add the newly created VHD to the previously created virtual machine, I use the Add-VMHardDiskDrive cmdlet. I need to provide it with the name of the virtual machine and the path to the VHD. In reality, it is pretty simple. Here is the command:

ADD-VMHardDiskDrive -VMName $n -Path “F:VM$nVirtual Hard Disks$n`Disk2.vhdx”

Configure snapshot and smart paging locations

To configure the locations for the snap shot storage and for smart paging, I need to use the Set-VM cmdlet. When I do this, I simply provide the locations for the appropriate parameters as shown here:

Set-VM -SnapshotFileLocation “F:VM$nSnapshots” –SmartPagingFilePath “F:VM$npaging”

Finishing up

So that is it for the modifications to my Create-VM.ps1 script. Not bad for 30 lines of code, including blank lines for spacing. The completed script is shown here:

$name = “Client1″,”Server1″,”Server2”

$name |

Foreach {

    new-vm $_ -MemoryStartupBytes 512MB -SwitchName InternalSwitch `

    -BootDevice cd -NewVHDPath “F:VM$_Virtual Hard Disks$_.vhdx” `

    -NewVHDSizeBytes 127GB }

 

Foreach($n in $name)

{

 if($n -match ‘client’)

    {

     Set-VMMemory -VMName $n -DynamicMemoryEnabled $true -MinimumBytes 512MB -MaximumBytes 4096MB -StartupBytes 2048MB -Buffer 20

     Set-VMDvdDrive -VMName $n -Path $((Get-Item F:winblue*client*).fullname)

     New-VHD -Path “F:VM$nVirtual Hard Disks$n`Disk2.vhdx” -Dynamic -SizeBytes 50MB

     ADD-VMHardDiskDrive -VMName $n -Path “F:VM$nVirtual Hard Disks$n`Disk2.vhdx”

     Get-VM -Name $n |

      Set-VM -SnapshotFileLocation “F:VM$nSnapshots” -SmartPagingFilePath “F:VM$npaging”

    }

 if($n -match ‘server’)

  {

   Set-VMMemory -VMName $n -DynamicMemoryEnabled $true -MinimumBytes 512MB -MaximumBytes 2048MB -StartupBytes 1024MB -Buffer 20

   Set-VMDvdDrive -VMName $n -Path $((Get-Item F:winblue*server*).fullname)

   New-VHD -Path “F:VM$nVirtual Hard Disks$n`Disk2.vhdx” -Dynamic -SizeBytes 50MB

   ADD-VMHardDiskDrive -VMName $n -Path “F:VM$nVirtual Hard Disks$n`Disk2.vhdx”

   Get-VM -Name $n |

    Set-VM -SnapshotFileLocation “F:VM$nSnapshots” -SmartPagingFilePath “F:VM$npaging”

  }

 

}

 

Get-VM $name | Start-VM The complete script is uploaded to the Scripting Guys Script Repository: Create and Configure a New Virtual Machine. Join me tomorrow when I will talk about more cool Windows PowerShell stuff. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon