public

Force Delete OrgVDC Network from Cloud Director using PowerShell

Cannot delete OrgVDC Network that isn't connected to anything? Let's fix this!

6 months ago

Latest Post Next generation of VMware Home Lab by Alexey Koznov public

When you're trying to delete Organization at the Cloud Director level you need to remove all objects that belong to it (for example, networks). Our engineers were facing issues when all VMs and vApps are removed (the network isn't connected to any object) but they cannot remove OrgVDC network and getting errors like in case of disconnecting OrgVDC network from Edge Gateway - Cannot detach organization VDC Network from Edge ... RPC request timed out and in case of removing this network - Network OrgVDC-Network-01 cannot be deleted, because it is in use by the following vApp Networks: OrgVDC-Network-01.

At the VMware Community forum I've found a good example of using PowerShell for removing these networks using this script (thanks bdmpastx for the awesome script):

# Change Cloud Director server address where problem network and Organization is placed 
$VCDConnection = connect-ciserver -server "iaas.cloud.site"

# After successful login let's get SessionId for next API manupulations
$sessionkey = $VCDConnection.SessionId

# Change the name of Organization where problem network is placed
$org = get-org -name "OrganizationName"

# Change Name of OrgVDC (sometimes one org can contains couple of OrgVDCs) where OrgVDC network is placed 
$vdc = Get-OrgVdc -org $org -name "OrgVDC-01"

# Change OrgVDC Network name to needed name that cannot be deleted at Cloud Director interface
$vdcorgNetwork = Get-OrgVdcNetwork -OrgVdc $vdc -name "OrgVDC-Network-01"

# Let's do some magic with REST API
$sessionkey = $VCDConnection.SessionId
write-host ("`tDeleting OrgVDC Network: "+$vdcorgNetwork.name)

# We need to create XML headers if we want to work with API. 
# We'll add SessionId key for auth, and using objects that we've in variables we'll create URL link 
# for problematic OrgVDC with command to remove it (new option that is fixing problem is ?force=true)

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add('x-vcloud-authorization', $sessionkey)
$Headers.Add('accept','application/*+xml;version=31.0')
$Headers.Add('Content-Type','application/xml')
$vdcnetURL = $vdcorgNetwork.href+'?force=true'

# Let's invoke request to Cloud Director with Delete method , headers and composed body
$wr = (Invoke-WebRequest -uri $vdcnetURL -Method Delete -Headers $Headers).content
[xml]$wrxml = $wr

# Waiting for success of created task for deleting problem OrgVDC network
$wrtaskhref= $wrxml.Task.href
Write-Verbose  "wrtaskhref: $wrtaskhref"
try{
while($ts -ne 'success')
{
[xml]$taskstatus = (Invoke-WebRequest -uri $wrtaskhref -Method get -Headers $headers).content
$ts = $taskstatus.Task.Status
Write-Verbose "ts: $ts"
start-sleep -Seconds 3}
}
catch{}
Write-Verbose -Message 'Pausing for 15 seconds so system can stabilize'
start-sleep -seconds 15

After running the script network should be deleted from Cloud Director and you can proceed further with removing Organization.

Alexey Koznov

Published 6 months ago

Comments?

Leave us your opinion.