Add vCloud Harddisks with PowerCLI

June 11, 2013

Table of Contents

Add vCloud Harddisks with PowerCLI When it comes to adding new disks to a VM,it is a little tough and you must be careful due to the many implementation of the objects in the SDK, tasks like conversion from XML to a .NET object is very tough, mostly to the with the vCloud API’s intricate nodes, elements, attributes and many more.the solution is to use the 4 HTTP methods used in REST APIs: GET, POST, PUT, DELETE. So the combination of PowerCLI and powershell.NET you create a new CLHarddisk, though it’s made on advanced level you can pipe a vCloud VM to it:

 # Passing VM via pipeline

Get-CIVM “MyVM” | New-CIHarddisk -size 10240

# Or just using a VM name

New-CIHarddisk -vm “MyVM” -size 10240

So the whole process is made with this code though it may create many disks just be keen:

function New-CIHarddisk





 [Parameter (ParameterSetName=”pipeline”,ValueFromPipeline=$true,Mandatory=$true)]$vm,

 [Parameter (Position=0, Mandatory=$true)]$size




 # Get indexes to create unique IDs for our new disk.

 if ($vm -is [System.String])


 $vm = get-civm $vm


 $vmHardware = $vm.ExtensionData.GetVirtualHardwareSection()

 $scsiController = $vmHardware.item | where {$_.resourceType.value -eq “6”}

 $highaddress = ($vmHardware.item | where {$_.resourcetype.value -eq “17”} | Sort-Object addressonParent)[-1].addressonParent.value

 $addressOnParent = [int]$highaddress + 1

 $highInstance = ($vmHardware.item | where {$_.resourcetype.value -eq “17”} | Sort-Object instanceID)[-1].instanceId.value

 $instanceId = [int]$highInstance + 1

 $highElement = ($vmHardware.item | where {$_.resourcetype.value -eq “17”} | Sort-Object elementName)[-1].elementName.value

 $elementName = [int]$highElement.Split()[-1] + 1


# We need to get the raw XML via vCloud API to add the new disk.

 $hardwareContentType = “application/vnd.vmware.vcloud.virtualHardwareSection+xml”

 $webclient = New-Object



 [xml]$hardwareConfXML = $webclient.DownloadString($vmhardware.href)


# Clone the XML node, assign our variables, and insert child into the XML

 $newDisk = ($hardwareConfXML.VirtualHardwareSection.Item | where {$_.resourcetype -eq 17})[-1].clonenode(1)

 $newDisk.AddressOnParent = “$addressOnParent”

 $newDisk.InstanceId = “$instanceId”

 $newDisk.elementName = “Hard Disk $elementName”

 $newdisk.hostresource.capacity = “$size”

 $newDisk.parent = “$($scsiController.instanceID.value)”

 [void]$hardwareConfXML.VirtualHardwareSection.InsertAfter($newDisk,($hardwareConfXML.VirtualHardwareSection.Item | where {$_.resourcetype -eq 17})[-1])


# PUT the data back through vCloud API

 $request = [System.Net.WebRequest]::Create($vmHardware.href);




 $request.ContentType = $hardwareContentType

 $postData = $hardwareConfXML.virtualhardwaresection.outerxml

 $xmlString = $postData.replace(“

“,””) # adding strings to xml puts in silly newlines.

 [byte[]]$xmlEnc = [System.Text.Encoding]::UTF8.GetBytes($xmlString)

 $request.ContentLength = $xmlEnc.length

 [System.IO.Stream]$requestStream = $request.GetRequestStream()

 $requestStream.write($xmlEnc, 0, $xmlEnc.Length)


 $response = $request.GetResponse()

 $responseStream = $response.getResponseStream()

 $streamReader = new-object System.IO.StreamReader($responseStream)

 $result = $streamReader.ReadtoEnd()






Share on
More posts

Dedicated Servers Quick Guide

What is a Dedicated Servers? Why bother using a dedicated server over a VPS or Shared Hosting?A dedicated server is a server 100% dedicated to your website/project or business needs.

Wrong Side Of The IT Ecosystem

I find it ridiculous that people are blaming Apple for job creation in China as opposed to in the US. People are also debating how US might in-source some of these manufacturing

Latency is no longer a concern!!

No matter if your business require low or high bandwidth  network you definitely need a low latency network that will back you up in every angle and every application your

Get 90% Discount

First 3 People gets the Bonus!
Don't Miss Out Our Big Sale

Get 0-90% On All
247Rack Services


The Sale Is Until The End Of March