#*========================================================================
#* Script Function: Cancellation of the customer org/vdc.
# Author: Chris Danielewski
#*========================================================================
param(
[string]$vCDserver,
[string]$org
)
$vcd = $vCDserver+".test.lab.com"
$path = "Z:\LogDir"
If (Test-Path $path)
{
write-host -foregroundcolor Green "Log directory found: $path"
write-host ""
}
Else
{
write-host -foregroundcolor Red "Unable to access $path , Please double check if the share is mapped"
exit
}
$logdir = $path+$org+".txt"
if (Test-Path $logdir)
{
write-host "Log file exist $logdir"
}
Else
{
write-host "Log file set to $logdir"
New-Item $logdir -type file
}
#Get password function
<confidential>
#*===========================================================================
#* Script Function: Gets Auth String
#* Thanks to Timothy Test for sharing those get/post functions!
#*===========================================================================
Function GetConinfo ($BaseURL,$password)
{
$url=$baseURL+"/sessions"
Write-Host "Trying to connect to " $url -ForegroundColor Green
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$webRequest = [System.Net.WebRequest]::Create($url)
$webRequest.ServicePoint.Expect100Continue = $false
$webRequest.PreAuthenticate = $true
$webRequest.Method = "Post"
$webRequest.Accept = "application/*+xml;version=5.6";
$webRequest.Credentials = New-Object System.Net.NetworkCredential("admin@system",[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)))
$resp = $webRequest.GetRequestStream();
$rs = $webRequest.GetResponse()
[System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs.GetResponseStream();
[string]$results = $sr.ReadToEnd();
$rs.Close();
return New-Object PSObject -Property @{Auth=$rs ; HTTP=[xml]$results}
}
#*===========================================================================
#* Script Function: Execute GET
#*===========================================================================
Function Getvcd($URL,$auth)
{
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$webRequest = [System.Net.WebRequest]::Create($url)
$webRequest.ContentType = "text/html"
$webRequest.ServicePoint.Expect100Continue = $false
$webRequest.Method = "Get"
$webRequest.Headers.Add("x-vcloud-authorization",$auth);
$webRequest.Accept = "application/*+xml;version=5.1";
[System.Net.WebResponse]$resp = $webRequest.GetResponse();
$rs = $resp.GetResponseStream();
[System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs;
[string]$results = $sr.ReadToEnd();
$rs.Close();
return [xml]$results;
}
function vcdRestP($url,$auth,$method,$contentType,$body)
{
$postContent = [byte[]][char[]][string]$body
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$webRequest = [System.Net.WebRequest]::Create($url)
$webRequest.ContentType = $contentType
$webRequest.ServicePoint.Expect100Continue = $false
$webRequest.Method = $method
$webRequest.Headers.Add("x-vcloud-authorization",$auth);
$webRequest.Accept = "application/*+xml;version=5.6";
$resp = $webRequest.GetRequestStream();
$resp.Write($postContent, 0, $postContent.length)
$rs = $webRequest.GetResponse();
[System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs.GetResponseStream();
[string]$results = $sr.ReadToEnd();
$rs.Close();
return $results;
}
# Prep
#set password
$password = <confidential> -AsPlainText -force
#set base URL to vCD api
$BaseURL="https://"+$vCDname+"/api"
#Get connection and authentication
$Coninfo=GetConinfo $BaseURL $password
$auth=$Coninfo.Auth.Headers["x-vcloud-authorization"]
#get org list URL
$OrgListURL = ($Coninfo.HTTP.Session.Link | where {$_.type -eq "application/vnd.vmware.vcloud.orglist+xml"}).href
#Use GET to retrieve org list
$OrgInfo= getvcd $OrgListURL $auth
#set Target Org URL
$TargetOrg = ($OrgInfo.OrgList.org | where {$_.name -eq $org}).href
#Use GET to retrieve Target Org XML
$TargetOrgXML = getvcd $TargetOrg $auth
#Set OrgID and cut the prefix to obtain just the ID
$orgID = $TargetOrgXML.Org.Id
$orgID = $orgID.replace("urn:vcloud:org:","")
#Post variables , no body and content type needed for those requests.
$D_method="POST"
$D_body=""
$D_contentType=""
#PowerOff vAPPs
#set Target Org VDC URLs
$TargetOrgVDC = ($TargetOrgXML.Org.Link | where {$_.type -eq "application/vnd.vmware.vcloud.vdc+xml"}).href
#Loop through each VDC to obtain VDC XML object and get all vAPPs.
ForEach ($vdc in $TargetOrgVDC)
{
$TargetOrgVDCXML = getvcd $vdc $auth
$vApps = $TargetOrgVDCXML.vdc.ResourceEntities.ResourceEntity.href
#Loop through all vAPPs, remove prefix to obtain vApp ID and get each vApp status and name
ForEach ($vApp in $vApps)
{
$Pattern = "https.*vApp/"
$New = ""
$vApp = [regex]::replace($vApp, $pattern, "")
$vAppURL = ($BaseURL+"/vApp/"+$vApp)
$vAppXML = getvcd $vAppURL $auth
$vAppstate = $vAppXML.vapp.status
$vAppname = $vAppXML.vapp.name
# shutdown vApps if powered on
if ($vAppstate -ne 8)
{
write-host -foregroundcolor Blue "Issued PowerOff to $vAppname"
$vAppPowerOffURL = ($BaseURL+"/vApp/"+$vApp+"/power/action/shutdown")
$date = get-date
#Save the output to log file
"vAPP name:"+$vAppname+" URL: "+$BaseURL+" ORG: "+$orgID+" DATE: "+$date | Out-File $logdir -Append
$poweroffPOST = vcdRestP $vAppPowerOffURL $auth $D_method
Start-Sleep 10
}
}
}
#Disable VDCs
$TargetOrgVDC = ($TargetOrgXML.Org.Link | where {$_.type -eq "application/vnd.vmware.vcloud.vdc+xml"}).href
#Loop through each vcd, remove prefix and post disable action
ForEach ($vdc in $TargetOrgVDC)
{
$Pattern = "https.*c/"
$New = ""
$vdc = [regex]::replace($vdc, $pattern, "")
$vdctoDisable = ($BaseURL+"/admin/vdc/"+$vdc+"/action/disable")
Start-Sleep 10
write-host -foregroundcolor Blue "Disabling VDC"
$disablevdc = vcdRestP $vdctoDisable $auth $D_method
}
#Disable Org
Start-Sleep -s 10
write-host -foregroundcolor Blue "Disabling the Organization"
$orgURLtoDisable = ($BaseURL+"/admin/org/"+$orgID+"/action/disable")
$disable = vcdRestP $orgURLtoDisable $auth $D_method