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

{

 [cmdletbinding()]

 param

 (

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

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

 )

Process

 {

 # 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 system.net.webclient

 $webclient.Headers.Add(“x-vcloud-authorization”,$vmHardware.Client.SessionKey)

 $webclient.Headers.Add(“accept”,$hardwareContentType)

 [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.Headers.Add(“x-vCloud-authorization”,$vmHardware.Client.SessionKey)

 $request.Accept=”application/vnd.vmware.vcloud.task+xml”

 $request.Method=”PUT”

 $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)

 $requestStream.Close()

 $response = $request.GetResponse()

 $responseStream = $response.getResponseStream()

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

 $result = $streamReader.ReadtoEnd()

 $streamReader.close()

 $response.close()

 }

}

Home/

Share on
Facebook
Twitter
LinkedIn
Pinterest
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.

Hacking the vCloud Director browser support warning

I came upon this article where some guy was explaining how to Hacking the vCloud Director to work on other browsers like chrome. So when he ran this: Invoke-RestMethod “https://vcloud.example.com/cloud/org/jake”

Windows Update Cleanup Plugin

Here is some good news for windows 7 SP1 users and maybe for VDI admins running windows 7 desktops in their VDI environments. Windows update cleanup plugin allows to uninstall

A Look at vCenter 5.5 SSO RC Installation

vCenter single sign on (SSO)  vsphere 5.5  release has several improvements, like the following: Completely re-written from the ground Multi-master architecture Native replication mechanism SSO now has site awareness (think

VMware releases vSphere powerCLI 5.5 R2

There was some new release of vSphere PowerCLI 5.5(Build 1649237) wich was shared by Alan Renouf on facebook . The new R2 has the following features per the release notes:

Veeam Launches Backup & Replication v7

When it comes to data protection, data replication and data recovery, these are very challenging tasks. Consolidation through virtualization has forced customers to retool automated protection and recovery methodologies in

Dedicated SERVER Special
50% OFF for Life!

🔥 DELL Bare-metal Dedicated Server in New York

🔹  Intel Gold 80 Cores
🔹  23TB SSD Flash +
🔹1TB NVMe (W RAID)
🔹 1TB DDR4 RAM 
🔹 10Gbps Redundant Uplinks 
🔹 10 IPv4 + IPv6 Addresses Included 

💲 Just $699/month – 50% OFF FOR LIFE! (Reg $1399/month)
🚀 Zero Setup Costs | Flexible OS: Ubuntu, Proxmox, ESXi, Windows, etc.

✅ Reserve Risk-Free! 30-Day Money-Back Guarantee
💡 100% Uptime Assurance – Enterprise-Grade Hardware 

🖥 Ideal for Virtualization, Data-Intensive Apps, & High Traffic Sites

Use coupon code at checkout for the discount to apply FBHEO3023EJFB
⚡ Limited Availability – Don’t Miss Out!

Get 90% Discount

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

Get 0-90% On All
247Rack Services

247Rack

The Sale Is Until The End Of March