VMware Cloud Community
pbarbuto
Contributor
Contributor

PowerCLI scripts using REST returning errors

I have a script for editing VMs in vCloud director. All it has to do is set RAM, CPUs, extend the harddisk, and enable hotadd for RAM and CPU. It works sometimes, and sometimes the errors it returns are different. So I'm banging my head up against the wall trying to troubleshoot this thing.

Sometimes the error is: Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (415) Unsupported Media Type."

And sometimes the error is: Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (400) Bad Request."

The error always seems to be with the GetResponse in the PUT function. Can anyone help with this?

Here's the script in full:

#Add snapins

Add-PSSnapin VMware.VimAutomation.Core

Add-PSSnapin -Name "VMware.VimAutomation.Cloud"

$user = "username"

$pass = "password"

$sec = ConvertTo-SecureString $pass -AsPlainText -Force

$cred = New-Object System.Management.Automation.PsCredential($user, $sec)

#Connect to vCloud server

do {

"Connecting ..."

$connection = Connect-CIServer -Server Server -Org "Org" -Credential $cred

$x ++

}until($connection.isconnected -eq "True" -or $x -eq 10)

#Define values

$VMName = "VMName"

$newSize = "DiskSize"

$NumCpu = "CPUs"

$MemoryMB = "RAM"

#Get Object

$VM = Get-CIVM $VMName

#load functions

function Get-REST (){

                #Get the XML data from server

               

                $webclient = New-Object system.net.webclient       

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

                $webclient.Headers.Add("accept","${hardwareContentType};version=5.1")       

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

                New-Variable -Name "hardwareConfXML" -Value $hardwareConfXML1 -Scope Global

               

}

function Put-REST ($hardwareConfXML){

                # 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;version=5.1"       

                $request.Method="PUT"       

                $request.ContentType = $hardwareContentType

                if ($var -eq "MEM"){$postData = $hardwareConfXML.virtualhardwaresection.outerxml}  

                if ($var -eq "HA"){$postData = $hardwareConfXML.vmCapabilities.outerxml} 

                $xmlString = $postData.replace([Environment]::NewLine ,"")      

                [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() 

}

#Create function to edit VM

function Set-CIVM {      

                $vmHardware = $vm.ExtensionData.GetVirtualHardwareSection()

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

                Get-REST     

                               

                if($MemoryMB -ne 0){           

                                $memoryNode = $hardwareConfXML.VirtualHardwareSection.item | where {$_.resourcetype -eq 4}           

                                $memoryNode.VirtualQuantity = "$MemoryMB"           

                                $memoryNode.ElementName = "$MemoryMB MB of memory"      

                }        

                if($NumCpu -ne 0){           

                                $CPUNode = $hardwareConfXML.VirtualHardwareSection.item | where {$_.resourcetype -eq 3}           

                                $CPUNode.VirtualQuantity = "${NumCpu}"           

                                $CPUNode.ElementName = "${NumCpu} virtual CPU(s)"       

                }        

                Put-REST $hardwareConfXML

                Remove-Variable -Name "hardwareConfXML" -Scope Global -Force

                Remove-Variable -Name "vmhardware" -force

                Remove-Variable -Name "hardwareContentType" -force

}

function Set-CIHA {

               $vmHardware =  $vm.ExtensionData.GetVmCapabilities()

               $hardwareContentType = $vmHardware.type

                Get-REST 

               

                $vmHA = $hardwareConfXML.VMCapabilities

                if ($vmHA.MemoryHotAddEnabled -eq "false"){

                    if($vm.PowerState -eq "PoweredOn") {

                    Stop-CIVM -VM $VM -Confirm:$False }

                    $vmHA.MemoryHotAddEnabled = "true"}

                if ($vmHA.CpuHotAddEnabled -eq "false"){

                    if($vm.PowerState -eq "PoweredOn") {

                    Stop-CIVM -VM $VM -Confirm:$False }

                    $vmHA.CpuHotAddEnabled = "true"}

                Put-REST $hardwareConfXML

                Remove-Variable -Name "hardwareConfXML" -Scope Global -Force

                Remove-Variable -Name "vmhardware" -force

                Remove-Variable -Name "hardwareContentType" -force

}

#Enable Hotadd

$var = "HA"

Set-CIHA

#update RAM and CPU

$var = "MEM"

Set-CIVM

#extend c: drive

    $civm = Get-CIVM -Name $VMName

  $allhdd = $civm.ExtensionData.getvirtualhardwaresection()

  ($allhdd.Item | where { $_.Description -like “Hard Disk”}).HostResource[0].AnyAttr[0]."#text" = $newSize

  Start-Sleep -Seconds 5

  $allhdd.updateserverdata()

  Start-Sleep -Seconds 5

#PowerOn Machine

if($vm.PowerState -eq "PoweredOn") {

    Start-CIVM -VM $VM -Confirm:$False }

Disconnect-CIServer -Force -Confirm:$false

Reply
0 Kudos
2 Replies
pbarbuto
Contributor
Contributor

Update: I've found that if i remove the Set-CIHA function the script runs successfully. So I can GET and PUT when editing RAM, CPUs, and disksize. Theres just something wrong with setting HotAdd. Its not going to be in the PUT function as that same function works with Set-CIVM, I guess its in the body of the Set-CIHA:

function Set-CIHA {

               $vmHardware =  $vm.ExtensionData.GetVmCapabilities()

               $hardwareContentType = $vmHardware.type

                Get-REST

              

                $vmHA = $hardwareConfXML.VMCapabilities

                if ($vmHA.MemoryHotAddEnabled -eq "false"){

                    if($vm.PowerState -eq "PoweredOn") {

                    Stop-CIVM -VM $VM -Confirm:$False }

                    $vmHA.MemoryHotAddEnabled = "true"}

                if ($vmHA.CpuHotAddEnabled -eq "false"){

                    if($vm.PowerState -eq "PoweredOn") {

                    Stop-CIVM -VM $VM -Confirm:$False }

                    $vmHA.CpuHotAddEnabled = "true"}

                Put-REST $hardwareConfXML

                Remove-Variable -Name "hardwareConfXML" -Scope Global -Force

                Remove-Variable -Name "vmhardware" -force

                Remove-Variable -Name "hardwareContentType" -force

}

Reply
0 Kudos
pbarbuto
Contributor
Contributor

I was finally able to get this to work by just creating two separate scripts: one for editing cpus, ram, and disk size and another for setting the HotAdd function. I'm not sure why the single script that combines both wasn't working nor do I care at this point. I just have the Set-CIHA script run first and than run the edit settings script.

Reply
0 Kudos