Last active
June 22, 2019 21:19
-
-
Save davidruhmann/f4c3004155ac3aee6df1 to your computer and use it in GitHub Desktop.
VMWare vCloud vApp Lease Extension Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;@echo off | |
;echo.>"%Temp%\vcloud.ps1" | |
;findstr -rbv ; %0 >> "%Temp%\vcloud.ps1" | |
;powershell -executionpolicy unrestricted -command "%Temp%\vcloud.ps1" | |
;exit /b | |
$api = "https://vcloud.example.com/api"; | |
# requires powershell version 4+ | |
# environment variables needed | |
# VCLOUD_AUTH = base64 encoding of user@org:password | |
# VCLOUD_USER = <username> | |
$code = 0; | |
$url = "$api/sessions"; | |
$headers = @{"Authorization"="Basic $env:VCLOUD_AUTH"; "Accept"="application/*+xml;version=5.5"}; | |
$response = Invoke-WebRequest -Method Post -Uri $url -Headers $headers | |
if ($response.StatusCode-eq 200) | |
{ | |
Write-Host "[INFO] Authenication Successful"; | |
$url = "$api/vApps/query?filter=ownerName==$env:VCLOUD_USER"; | |
$headers = @{ | |
"Accept"="application/*+xml;version=5.5"; | |
"x-vcloud-authorization"=$response.Headers["x-vcloud-authorization"] | |
}; | |
$result = Invoke-WebRequest -Method Get -Uri $url -Headers $headers | |
if ($result.StatusCode -eq 200) | |
{ | |
$vapps = ([xml]$result.Content).GetElementsByTagName("VAppRecord"); | |
Write-Host "[INFO] " $vapps.count " vApps Found"; | |
foreach ($vapp in $vapps) | |
{ | |
$body = @' | |
<?xml version="1.0" encoding="UTF-8"?> | |
<vcloud:LeaseSettingsSection | |
xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" | |
xmlns:vcloud="http://www.vmware.com/vcloud/v1.5" | |
href=" | |
'@ + $vapp.href + @' | |
/leaseSettingsSection/" | |
ovf:required="false" | |
type="application/vnd.vmware.vcloud.leaseSettingsSection+xml"> | |
<ovf:Info>Lease settings section</ovf:Info> | |
<vcloud:Link | |
href=" | |
'@ + $vapp.href + @' | |
/leaseSettingsSection/" | |
rel="edit" | |
type="application/vnd.vmware.vcloud.leaseSettingsSection+xml"/> | |
<vcloud:DeploymentLeaseInSeconds>1209600</vcloud:DeploymentLeaseInSeconds> | |
<vcloud:StorageLeaseInSeconds>1209600</vcloud:StorageLeaseInSeconds> | |
</vcloud:LeaseSettingsSection> | |
'@; | |
#Write-Host $body | |
$url = $vapp.href + "/leaseSettingsSection/"; | |
$headers = @{ | |
"Accept"="application/*+xml;version=5.5"; | |
"x-vcloud-authorization"=$response.Headers["x-vcloud-authorization"]; | |
"Content-Type"="application/vnd.vmware.vcloud.leaseSettingsSection+xml; charset=ISO-8859-1" | |
}; | |
try | |
{ | |
$result = Invoke-WebRequest -Method Put -Uri $url -Headers $headers -Body $body | |
if ($result.StatusCode -eq 202) | |
{ | |
Write-Host "[INFO] Lease Extension Successful for" $vapp.name; | |
} | |
else | |
{ | |
Write-Host "[ERROR] Lease Extension Failed for" $vapp.name; | |
$code = 1; | |
} | |
} | |
catch | |
{ | |
Write-Host "[ERROR] Lease Extension Failed for" $vapp.name; | |
$code = 2; | |
} | |
} | |
Write-Host "[INFO] vApp Update Complete"; | |
} | |
else | |
{ | |
Write-Host "[ERROR] vApp Query Failed"; | |
$code = 3; | |
} | |
$url = "$api/vAppTemplates/query?filter=ownerName==$env:VCLOUD_USER"; | |
#$url = "$api/query?type=vAppTemplate&format=records&filter=ownerName==$env:VCLOUD_USER"; | |
$headers = @{ | |
"Accept"="application/*+xml;version=5.5"; | |
"x-vcloud-authorization"=$response.Headers["x-vcloud-authorization"] | |
}; | |
$result = Invoke-WebRequest -Method Get -Uri $url -Headers $headers | |
if ($result.StatusCode -eq 200) | |
{ | |
$templates = ([xml]$result.Content).GetElementsByTagName("VAppTemplateRecord"); | |
Write-Host "[INFO] " $templates.count " vApp Templates Found"; | |
foreach ($template in $templates) | |
{ | |
$body = @' | |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<ns6:LeaseSettingsSection | |
xmlns="http://www.vmware.com/vcloud/versions" xmlns:ns2="http://schemas.dmtf.org/ovf/envelope/1" xmlns:ns3="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:ns4="http://schemas.dmtf.org/wbem/wscim/1/common" xmlns:ns5="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:ns6="http://www.vmware.com/vcloud/v1.5" xmlns:ns7="http://www.vmware.com/schema/ovf" xmlns:ns8="http://schemas.dmtf.org/ovf/environment/1" xmlns:ns9="http://www.vmware.com/vcloud/extension/v1.5" | |
type="application/vnd.vmware.vcloud.leaseSettingsSection+xml" | |
href=" | |
'@ + $template.href + @' | |
/leaseSettingsSection/" ns2:required="false"> | |
<ns2:Info/> | |
<ns6:Link | |
rel="edit" | |
type="application/vnd.vmware.vcloud.leaseSettingsSection+xml" | |
href=" | |
'@ + $template.href + @' | |
/leaseSettingsSection/"/> | |
<ns6:StorageLeaseInSeconds>2592000</ns6:StorageLeaseInSeconds> | |
</ns6:LeaseSettingsSection> | |
'@; | |
#Write-Host $body | |
$url = $template.href + "/leaseSettingsSection/"; | |
$headers = @{ | |
"Accept"="application/*+xml;version=5.5"; | |
"x-vcloud-authorization"=$response.Headers["x-vcloud-authorization"]; | |
"Content-Type"="application/vnd.vmware.vcloud.leaseSettingsSection+xml; charset=ISO-8859-1" | |
}; | |
try | |
{ | |
$result = Invoke-WebRequest -Method Put -Uri $url -Headers $headers -Body $body | |
if ($result.StatusCode -eq 202) | |
{ | |
Write-Host "[INFO] Lease Extension Successful for" $template.name; | |
} | |
else | |
{ | |
Write-Host "[ERROR] Lease Extension Failed for" $template.name; | |
$code = 4; | |
} | |
} | |
catch | |
{ | |
Write-Host "[ERROR] Lease Extension Failed for" $template.name; | |
$code = 5; | |
} | |
} | |
Write-Host "[INFO] vApp Template Update Complete"; | |
} | |
else | |
{ | |
Write-Host "[ERROR] vApp Template Query Failed"; | |
$code = 6; | |
} | |
} | |
else | |
{ | |
Write-Host "[ERROR] Authentication Failed"; | |
} | |
exit($code); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;@echo off | |
;echo.>"%Temp%\vapp.ps1" | |
;findstr -rbv ; %0 >> "%Temp%\vapp.ps1" | |
;powershell -executionpolicy unrestricted -command "%Temp%\vapp.ps1" | |
;exit /b | |
$vapps = @{ | |
"Example"="fb71ad14-e19f-4358-a633-95666b16d69e"; | |
"Another"="5bd5567f-4858-42ba-8757-1ec92815a2e0" | |
}; | |
$warn = $false; | |
$url = "https://vcloud.example.com/api/sessions"; | |
# user@organization:password in base64 | |
$headers = @{"Authorization"="Basic $env:BASE64_AUTH"; "Accept"="application/*+xml;version=5.5"}; | |
$response = Invoke-WebRequest -Method Post -Uri $url -Headers $headers | |
if ($response.StatusCode-eq 200) | |
{ | |
Write-Host "[INFO] Authenication Successful"; | |
foreach ($vapp in $vapps.Keys) | |
{ | |
Write-Host "$vapp ..."; | |
$uid = $vapps[$vapp]; | |
$body = @' | |
<?xml version="1.0" encoding="UTF-8"?> | |
<vcloud:LeaseSettingsSection | |
xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" | |
xmlns:vcloud="http://www.vmware.com/vcloud/v1.5" | |
href="https://vcloud.example.com/api/vApp/vapp- | |
'@ + $uid + @' | |
/leaseSettingsSection/" | |
ovf:required="false" | |
type="application/vnd.vmware.vcloud.leaseSettingsSection+xml"> | |
<ovf:Info>Lease settings section</ovf:Info> | |
<vcloud:Link | |
href="https://vcloud.example.com/api/vApp/vapp- | |
'@ + $uid + @' | |
/leaseSettingsSection/" | |
rel="edit" | |
type="application/vnd.vmware.vcloud.leaseSettingsSection+xml"/> | |
<vcloud:DeploymentLeaseInSeconds>1209600</vcloud:DeploymentLeaseInSeconds> | |
<vcloud:StorageLeaseInSeconds>1209600</vcloud:StorageLeaseInSeconds> | |
</vcloud:LeaseSettingsSection> | |
'@; | |
#Write-Host $body | |
$url = "https://vcloud.example.com/api/vApp/vapp-" + $uid + "/leaseSettingsSection/"; | |
$headers = @{ | |
"Accept"="application/*+xml;version=5.5"; | |
"x-vcloud-authorization"=$response.Headers["x-vcloud-authorization"] | |
"Content-Type"="application/vnd.vmware.vcloud.leaseSettingsSection+xml; charset=ISO-8859-1" | |
}; | |
$result = Invoke-WebRequest -Method Put -Uri $url -Headers $headers -Body $body | |
if ($result.StatusCode -eq 202) | |
{ | |
Write-Host "[INFO] Lease Extension Successful" | |
} | |
else | |
{ | |
Write-Host "[ERROR] Lease Extension Failed" | |
$warn = $true; | |
} | |
} | |
} | |
else | |
{ | |
Write-Host "[ERROR] Authentication Failed"; | |
} | |
exit($warn); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment