Functions/Public/Connect-vRAServer.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
function Connect-vRAServer { <# .SYNOPSIS Connect to a vRA Server .DESCRIPTION Connect to a vRA Server and generate a connection object with Servername, Token etc .PARAMETER Server vRA Server to connect to .PARAMETER Tenant Tenant to connect to .PARAMETER Username Username to connect with .PARAMETER Password Password to connect with .PARAMETER Credential Credential object to connect with .PARAMETER IgnoreCertRequirements Ignore requirements to use fully signed certificates .INPUTS System.String System.SecureString Management.Automation.PSCredential Switch .OUTPUTS System.Management.Automation.PSObject. .EXAMPLE Connect-vRAServer -Server vraappliance01.domain.local -Tenant Tenant01 -Credential (Get-Credential) .EXAMPLE $SecurePassword = ConvertTo-SecureString “P@ssword” -AsPlainText -Force Connect-vRAServer -Server vraappliance01.domain.local -Tenant Tenant01 -Username TenantAdmin01 -Password $SecurePassword -IgnoreCertRequirements #> [CmdletBinding(DefaultParametersetName="Username")][OutputType('System.Management.Automation.PSObject')] Param ( [parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$Server, [parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] [String]$Tenant = "vsphere.local", [parameter(Mandatory=$true,ParameterSetName="Username")] [ValidateNotNullOrEmpty()] [String]$Username, [parameter(Mandatory=$true,ParameterSetName="Username")] [ValidateNotNullOrEmpty()] [SecureString]$Password, [Parameter(Mandatory=$true,ParameterSetName="Credential")] [ValidateNotNullOrEmpty()] [Management.Automation.PSCredential]$Credential, [parameter(Mandatory=$false)] [Switch]$IgnoreCertRequirements ) # --- Default Signed Certificates to true $SignedCertificates = $true if ($PSBoundParameters.ContainsKey("IgnoreCertRequirements") ){ if ($PSVersionTable.PSEdition -eq "Desktop" -or !$PSVersionTable.PSEdition) { if ( -not ("TrustAllCertsPolicy" -as [type])) { 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; } } "@ } [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy } $SignedCertificates = $false } # --- Convert Secure Credentials to a format for sending in the JSON payload if ($PSBoundParameters.ContainsKey("Credential")){ $Username = $Credential.UserName $JSONPassword = $Credential.GetNetworkCredential().Password } if ($PSBoundParameters.ContainsKey("Password")){ $JSONPassword = (New-Object System.Management.Automation.PSCredential(“username”, $Password)).GetNetworkCredential().Password } try { # --- Create Invoke-RestMethod Parameters $JSON = @" { "username":"$($Username)", "password":"$($JSONPassword)", "tenant":"$($Tenant)" } "@ $Params = @{ Method = "POST" URI = "https://$($Server)/identity/api/tokens" Headers = @{ "Accept"="application/json"; "Content-Type" = "application/json"; } Body = $JSON } if ((!$SignedCertificate) -and ($PSVersionTable.PSEdition -eq "Core")) { $Params.Add("SkipCertificateCheck", $true) } $Response = Invoke-RestMethod @Params # --- Create Output Object $Global:vRAConnection = [PSCustomObject] @{ Server = "https://$($Server)" Token = $Response.id Tenant = $Null Username = $Username APIVersion = $Null SignedCertificates = $SignedCertificates } # --- Update vRAConnection with tenant and api version $Global:vRAConnection.Tenant = (Get-vRATenant -Id $Tenant).id $Global:vRAConnection.APIVersion = (Get-vRAVersion).APIVersion } catch [Exception]{ throw } Write-Output $vRAConnection } |