A few posts earlier you could read the script article that makes it easy to create all the required infrastructure elements in version 7.x. Now I decided to create a similar script to version 8 based on vSphere infrastructure. However, in the future I plan to modify the script as well as add other elements.
At the very bottom you can find the current version of the script to download as well as the document that is used to create the infrastructure.
first things first
At the beginning I collect all required data about the token and install plugins and create ignore for self service certificate
Install-Module ImportExcel -Scope CurrentUser add-type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12' [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy #> #Master Variable (for provide by user) $vraServer = 'vra8api.vworld.domain.local' $VraAdmin = 'administrator@vsphere.local' $CloudUser = 'master' $DefaultPassword = '' $excelFile = 'C:\vra8.xlsx' #Variables # Token $url = “https://$($vraServer)/csp/gateway/am/api/login?access_token” $properties = @{‘username’ = $CloudUser; ‘password’ = $DefaultPassword} $bodyObject = New-Object –TypeName PSObject –Property $properties $body = $bodyObject | ConvertTo-Json $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”} #Refresh Token $request = Invoke-WebRequest $url -Method POST -Headers $headers -Body $body $content = $request.content | convertFrom-json $refreshToken = $content.refresh_token $refreshProperties = @{"refreshToken" = $refreshToken} $bodyObject = New-Object –TypeName PSObject –Property $refreshProperties $body = $bodyObject | ConvertTo-Json $accessUrl = “https://$($vraServer)/iaas/api/login” #Access Token $request = Invoke-WebRequest $accessUrl -Method POST -Headers $headers -Body $body $content = $request.content | convertFrom-json $accessToken = $content.tokenType +" "+ $content.token #API version $iaasUrl = "https://$($vraServer)/iaas/api/about" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $iaasUrl -Method GET -Headers $headers $content = $request.content | convertFrom-json $api_version = $content.latestApiVersion
The script I created creates Cloud Account, then Cloud Zone and Project, assigns Cloud Zone to the project. Creates flavor, imports all templates from the attached datacenter as image mapping. It creates network profiles as a clean solution without connected networks as well as creates network profiles based on networks that are in a specific region. And Creates Storage Profile
Each element of the script is a separate function, some elements are repeated many times in the script but it is done specifically to be able to easily extract the data of interest to us
So we have functions
1. Create Cloud Account
function Create_Cloud_Account() { $counter = 0 $excel = Import-Excel 'c:\vra8.xlsx' -WorksheetName 'Cloud_Account_vSphere' foreach($j in $excel) { if ($j.cloudAccount -ne $null) { $properties = @{"cloudAccountType" = $($j.cloudAccount);‘hostName’ = $($j.hostname); ‘password’ = $($j.password); 'username' = $($j.username); 'acceptSelfSignedCertificate' = $true} $bodyObject = New-Object –TypeName PSObject –Property $properties $body = $bodyObject | ConvertTo-Json $accountUrl = "https://$($vraServer)/iaas/api/cloud-accounts-vsphere/region-enumeration?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $accountUrl -Method POST -Headers $headers -Body $body $region = $request.Content | ConvertFrom-Json $region = $region.externalRegionIds Write-Host $region $accountUrl = "https://$($vraServer)/iaas/api/cloud-accounts-vsphere?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $properties = @{"regionIds" = $region;"cloudAccountType" = $($j.cloudAccount);‘hostName’ = $($j.hostname); ‘password’ = $($j.password); 'username' = $($j.username); 'name' = 'vCenter'+$counter; 'acceptSelfSignedCertificate' = $true} $bodyObject = New-Object –TypeName PSObject –Property $properties $body = $bodyObject | ConvertTo-Json $request = Invoke-WebRequest $accountUrl -Method POST -Headers $headers -Body $body $counter += 1 } } Write-Host $counter }
2. List Cloud Account
function List_Cloud_Account() { $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $accountUrl = "https://$($vraServer)/iaas/api/cloud-accounts?apiVersion=$($api_version)" $request = Invoke-WebRequest $accountUrl -Method GET -Headers $headers $content = $request.Content | ConvertFrom-Json $data = $content.content foreach($i in $data) { Write-Host "=============================" Write-Host "Cloud Account Type: $($i.cloudAccountType)" Write-Host "Cloud Acount Name: $($i.name)" Write-Host "Cloud Account Region Name: $($i.enabledRegionIds)" Write-Host "Cloud Account URL: $($i.cloudAccountProperties.hostName)" Write-Host "=============================" } }
3. Create Cloud Zone
function Create_Cloud_Zone() { $excel = Import-Excel 'c:\vra8.xlsx' -WorksheetName 'Create_Cloud_Zone' $regionsUrl = "https://$($vraServer)/iaas/api/regions?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $regionsUrl -Method GET -Headers $headers $contentr = $request.content | convertFrom-json foreach($x in $contentr.content) { #Write-Host $x.externalRegionId #Write-Host $x.n $vCenterId = $x.id } foreach($j in $excel) { if ("vCenter" -in $j.regionId) { $j.regionId = $vCenterId } $prop = $j | ConvertTo-Json $zoneUrl = "https://$($vraServer)/iaas/api/zones?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $zoneUrl -Method POST -Headers $headers -Body $prop } }
4. List Cloud Zone
function List_Cloud_Zone() { $zoneUrl = "https://$($vraServer)/iaas/api/zones?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $zoneUrl -Method GET -Headers $headers $content = $request.content | convertFrom-json foreach ($x in $content.content) { Write-Host "=============================" Write-Host "Cloud Zone Name: $($x.name)" Write-Host "Cloud Zone Id: $($x.id)" Write-Host "Cloud Zone Description: $($x.description)" Write-Host "=============================" } }
5. Create Projects
function Create_Projects() { $excel = Import-Excel 'c:\vra8.xlsx' -WorksheetName 'Projects' foreach($j in $excel) { Write-Host $j if($j.administrators -and $j.members -ne $null) { $zm = [pscustomobject]@{ name = $j.name description = $j.description administrators = @([pscustomobject]@{email = $j.administrators}) members = @([pscustomobject]@{email = $j.members}) } $zm = $zm|ConvertTo-Json $projectUrl = "https://$($vraServer)/iaas/api/projects?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $projectUrl -Method POST -Headers $headers -Body $zm } else { Write-Host "Your Excel File has null field in Administrator or Member column" } } }
6. List Projects
function List_Projects() { $projectUrl = "https://$($vraServer)/iaas/api/projects?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $projectUrl -Method GET -Headers $headers $content = $request.Content | ConvertFrom-Json foreach ($x in $content.content) { $AdminJson = $x.administrators|ConvertTo-Json foreach ($i in $AdminJson) { $AdminList = $i|ConvertFrom-Json } Write-Host "=============================" Write-Host "Project Name: $($x.name)" Write-Host "Project Administrator: $($AdminList.email)" Write-Host "Project Description: $($x.description)" Write-Host "Project Id: $($x.id)" Write-Host "=============================" } }
7. Assign Cloud Zone to Projects
function Zone_to_Project() { $excel = Import-Excel 'c:\vra8.xlsx' -WorksheetName 'Zone_Project' foreach($j in $excel) { $Zone_Name = $j.Zone_Name $Project_Name = $j.Project_Name $projectUrl = "https://$($vraServer)/iaas/api/projects?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $Prequest = Invoke-WebRequest $projectUrl -Method GET -Headers $headers $Pcontent = $Prequest.Content | ConvertFrom-Json foreach ($x in $Pcontent.content) { if($Project_Name -eq $x.name) { $Project_ID = $x.id } } $zoneUrl = "https://$($vraServer)/iaas/api/zones?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $Zrequest = Invoke-WebRequest $zoneUrl -Method GET -Headers $headers $Zcontent = $Zrequest.content | convertFrom-json foreach ($x in $Zcontent.content) { if($Zone_Name -eq $x.name) { $Zone_ID = $x.id } } if ($Zone_ID -ne $null) { $projectzoneUrl = "https://$($vraServer)/iaas/api/projects/$($Project_ID)?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $projectzoneUrl -Method GET -Headers $headers $ZProject = $request.Content | ConvertFrom-Json $zones = $Zproject.zones $zm = [pscustomobject]@{ zoneAssignmentConfigurations = @([pscustomobject]@{zoneId = $Zone_ID;priority = 0; maxNumberInstances = 0}) } $zm.zoneAssignmentConfigurations += $zones $zmJson = $zm | ConvertTo-Json $PZrequest = Invoke-WebRequest $projectzoneUrl -Method PATCH -Headers $headers -Body $zmJson -ContentType 'application/json' } } }
8. Create Flavor
function Flavor() { $excel = Import-Excel 'c:\vra8.xlsx' -WorksheetName 'Flavor' foreach($j in $excel) { $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $accountUrl = "https://$($vraServer)/iaas/api/cloud-accounts?apiVersion=$($api_version)" $request = Invoke-WebRequest $accountUrl -Method GET -Headers $headers $content = $request.Content | ConvertFrom-Json $data = $content.content foreach($i in $data) { if($i.name -in $j.Cloud_Account) { $CAid = $i.id } } $regionsUrl = "https://$($vraServer)/iaas/api/regions?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $regionsUrl -Method GET -Headers $headers $contentr = $request.content | convertFrom-json foreach($x in $contentr.content) { $Region = "*$($j.Region)*" if ($x.externalRegionId -like $Region ) { $Rid = $x.id } } $zm = [pscustomobject]@{ name = "vcenter-flavor-profile" flavorMapping = [pscustomobject]@{$j.name = [pscustomobject]@{cpuCount = $j.cpu;memoryInMB =$j.memory} } regionId = $Rid } $zm = $zm|ConvertTo-Json $flavorUrl = "https://$($vraServer)/iaas/api/flavor-profiles?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $flavorUrl -Method POST -Headers $headers -Body $zm $contentr = $request.content | convertFrom-json } }
9. Create Image Mapping
function Image() { $excel = Import-Excel 'c:\vra8.xlsx' -WorksheetName 'Image' foreach($j in $excel) { $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $accountUrl = "https://$($vraServer)/iaas/api/cloud-accounts?apiVersion=$($api_version)" $request = Invoke-WebRequest $accountUrl -Method GET -Headers $headers $content = $request.Content | ConvertFrom-Json $data = $content.content foreach($i in $data) { if($i.name -in $j.Cloud_Account) { $CAid = $i.id } } $regionsUrl = "https://$($vraServer)/iaas/api/regions?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $regionsUrl -Method GET -Headers $headers $contentr = $request.content | convertFrom-json foreach($x in $contentr.content) { $Region = "*$($j.Region)*" if ($x.externalRegionId -like $Region ) { $Rid = $x.id $Rname = $x.externalRegionId } } $imageUrl = "https://$($vraServer)/iaas/api/fabric-images?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $imageUrl -Method GET -Headers $headers $contentr = $request.content | convertFrom-json $list = $contentr.content Write-Host "Image List on Region: $($Rname)" foreach ($b in $list) { Write-Host "=============================" Write-Host "Image OS Family: $($b.osFamily)" Write-Host "Template Description: $($b.description)" Write-Host "Template Name: $($b.name)" Write-Host "=============================" } Write-Host "Do you want add all Images to Region" $input = Read-Host "Please answear y/n" switch($input) { 'y' { Write-Host "OK" foreach ($c in $list) { $imageUrl = "https://$($vraServer)/iaas/api/image-profiles?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $body = [pscustomobject]@{ name = $c.name description = $c.description imageMapping = [pscustomobject]@{$c.name = [pscustomobject]@{id = $c.id} } regionId = $Rid } $body = $body|ConvertTo-Json $request = Invoke-WebRequest $imageUrl -Method POST -Headers $headers -Body $body } } 'n' { Write-Host "Bye" } } } }
10. Create Network Profile
function Network_Profile() { $excel = Import-Excel 'c:\vra8.xlsx' -WorksheetName 'Image' foreach($j in $excel) { $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $accountUrl = "https://$($vraServer)/iaas/api/cloud-accounts?apiVersion=$($api_version)" $request = Invoke-WebRequest $accountUrl -Method GET -Headers $headers $content = $request.Content | ConvertFrom-Json $data = $content.content foreach($i in $data) { if($i.name -in $j.Cloud_Account) { $CAid = $i.id } } $regionsUrl = "https://$($vraServer)/iaas/api/regions?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $regionsUrl -Method GET -Headers $headers $contentr = $request.content | convertFrom-json foreach($x in $contentr.content) { $Region = "*$($j.Region)*" if ($x.externalRegionId -like $Region ) { $Rid = $x.id $Rname = $x.externalRegionId } } $networkUrl = "https://$($vraServer)/iaas/api/fabric-networks?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $networkUrl -Method GET -Headers $headers $contentr = $request.content | convertFrom-json $list = $contentr.content Write-Host "Network List on Region: $($Rname)" foreach ($b in $list) { Write-Host "=============================" Write-Host "Network Name: $($b.name)" Write-Host "Network ID: $($b.id)" Write-Host "=============================" } Write-Host "Do you want add all Network to Region" $input = Read-Host "Please answear y/n" switch($input) { 'y' { Write-Host "OK" foreach ($c in $list) { $networkUrl = "https://$($vraServer)/iaas/api/network-profiles?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $body = [pscustomobject]@{ name = $c.name fabricNetworkIds = @([pscustomobject]$c.id) regionId = $Rid } $body = $body|ConvertTo-Json $request = Invoke-WebRequest $networkUrl -Method POST -Headers $headers -Body $body } } 'n' { Write-Host "Do you want Create other Network Profiles from excel" $input = Read-Host "Please answear y/n" switch($input) { 'y' { $excel = Import-Excel 'c:\vra8.xlsx' -WorksheetName 'Network' foreach($e in $excel) { $networkUrl = "https://$($vraServer)/iaas/api/network-profiles?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $body = [pscustomobject]@{ name = $e.Name #fabricNetworkIds = @([pscustomobject]$c.id) regionId = $Rid } $body = $body|ConvertTo-Json $request = Invoke-WebRequest $networkUrl -Method POST -Headers $headers -Body $body } } 'n' { Write-Host "Bye" } } } } } }
11. Create Storage Profile
function Storage_Profile() { $excel = Import-Excel 'c:\vra8.xlsx' -WorksheetName 'Storage' foreach($j in $excel) { $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $accountUrl = "https://$($vraServer)/iaas/api/cloud-accounts?apiVersion=$($api_version)" $request = Invoke-WebRequest $accountUrl -Method GET -Headers $headers $content = $request.Content | ConvertFrom-Json $data = $content.content foreach($i in $data) { if($i.name -in $j.Cloud_Account) { $CAid = $i.id } } $regionsUrl = "https://$($vraServer)/iaas/api/regions?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $regionsUrl -Method GET -Headers $headers $contentr = $request.content | convertFrom-json foreach($x in $contentr.content) { $Region = "*$($j.Region)*" if ($x.externalRegionId -like $Region ) { $Rid = $x.id $Rname = $x.externalRegionId } } $StorageUrl = "https://$($vraServer)/iaas/api/fabric-vsphere-storage-policies?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $StorageUrl -Method GET -Headers $headers $contentr = $request.content | convertFrom-json $list = $contentr.content $counter = 0 foreach ($n in $list) { if($counter -eq 0){ if($j.Policy_Name -eq "Datastore default") { Write-Host "pierwszy IF" $DatastoreUrl = "https://$($vraServer)/iaas/api/fabric-vsphere-datastores?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $DatastoreUrl -Method GET -Headers $headers $contentra = $request.content | convertFrom-json $lista = $contentra.content foreach ($m in $lista) { if($j.Datastore_Name -eq $m.name) { $DatastoreId = $m.id $body1 = [pscustomobject]@{ name = $j.Storage_Policy_Name diskProperties = [pscustomobject]@{provisioningType = $j.Disk_Properties} diskTargetProperties = [pscustomobject]@{ datastoreId = $DatastoreId} regionId = $Rid } $body1 = $body1|ConvertTo-Json $StorageProfileUrl = "https://$($vraServer)/iaas/api/storage-profiles?apiVersion=$($api_version)" $request = Invoke-WebRequest $StorageProfileUrl -Method POST -Headers $headers -Body $body1 } } $counter +=1 } elseif($j.Policy_Name -eq $n.name) { Write-Host "drugi IF" $PolicyId = $n.id $DatastoreUrl = "https://$($vraServer)/iaas/api/fabric-vsphere-datastores?apiVersion=$($api_version)" $headers = @{“Content-Type” = “application/json”; “Accept” = “application/json”; "Authorization" = "$accessToken"} $request = Invoke-WebRequest $DatastoreUrl -Method GET -Headers $headers $contentr = $request.content | convertFrom-json $listc = $contentr.content foreach ($b in $listc) { if($j.Datastore_Name -eq $b.name) { $DatastoreId = $b.id $body2 = [pscustomobject]@{ name = $j.Storage_Policy_Name diskProperties = [pscustomobject]@{storagePolicyId = $PolicyId;provisioningType = $j.Disk_Properties} diskTargetProperties = [pscustomobject]@{ datastoreId = $DatastoreId} regionId = $Rid } $body2 = $body2|ConvertTo-Json $StorageProfileUrl = "https://$($vraServer)/iaas/api/storage-profiles?apiVersion=$($api_version)" $request = Invoke-WebRequest $StorageProfileUrl -Method POST -Headers $headers -Body $body2 } } $counter +=1 }} } } }
function Show-Menu { param ( [string]$Title = 'My Menu' ) cls Write-Host "================ $Title ================" Write-Host "01: Press '1' for Create Cloud Account" Write-Host "02: Press '2' for List Cloud Account." Write-Host "03: Press '3' for Create Cloud Zone." Write-Host "04: Press '4' for List Cloud Zone." Write-Host "05: Press '5' for Create Project." Write-Host "06: Press '6' for List Project." Write-Host "07: Press '7' for Assign Cloud Zone to Project." Write-Host "08: Press '8' for Assign Flavor to Region" Write-Host "09: Press '9' for Assign Image from Region" Write-Host "10: Press '10' for Create Network Profile" Write-Host "11: Press '11' for Create Storage Profile" Write-Host "Q: Press 'Q' to quit." } do { Show-Menu $input = Read-Host "Please make a selection" switch ($input) { '1' { cls Create_Cloud_Account } '2' { cls List_Cloud_Account } '3' { cls Create_Cloud_Zone } '4' { cls List_Cloud_Zone } '5' { cls Create_Projects } '6' { cls List_Projects } '7' { cls Zone_to_Project } '8' { cls Flavor } '9' { cls Image } '10' { cls Network_Profile } '11' { cls Storage_Profile } 'q' { return } } pause } until ($input -eq 'q')
DOWNLOAD
You can download the entire script and Excel used for creation below