-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect-vmc-nsx-vcenter.ps1
59 lines (45 loc) · 1.97 KB
/
connect-vmc-nsx-vcenter.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
<#
Script name: connect-vmc-nsx-vcenter.ps1
Last update: 27 Sep 2021
Author: Eric Gray, @eric_gray
Description: Quickly log in to VMC, NSX-T, and optionally vCenter Server
#>
#Requires -Modules VMware.VimAutomation.Vmc, VMware.VMC.NSXT
param ($SddcName=$false,
$RefreshToken=$false,
[switch]$IncludeVCenterConnect=$false)
$defaultEnvToken = $env:TOKEN
# if token not passed as a param, see if the default env var exists
if(-not $RefreshToken -and -not $defaultEnvToken) {
Write-Host -ForegroundColor Red "No -RefreshToken param passed and no API token in default env var!"
exit 1
} elseif (-not $RefreshToken) {
$RefreshToken = $defaultEnvToken
}
Write-Host -ForegroundColor Green "Connecting to VMC..."
Connect-Vmc -RefreshToken $refreshToken
if (-not $global:DefaultVmcServers ) {
Write-Host -ForegroundColor Red "Not connected to VMware Cloud - check for a valid refresh token"
exit 1
}
$orgName = Get-VmcOrganization
if(!$SddcName) {
$allSddcs = Get-VmcSddc
if($allSddcs.Count -eq 1) { # if org has more than one SDDC, need to specify
Write-Host -ForegroundColor Blue "Found one SDDC: $allSddcs"
$SddcName = $allSddcs.Name
} else {
Write-Host -ForegroundColor Blue "Found SDDCs: $allSddcs"
Write-Host -ForegroundColor Red "Could not determine default SDDC, please specifiy with parameter -SddcName."
exit 1
}
}
Write-Host -ForegroundColor Green "Connecting to NSX-T proxy..."
Connect-NSXTProxy -RefreshToken $refreshToken -OrgName $orgName -SDDCName $SddcName
if($IncludeVCenterConnect.isPresent) {
Write-Host -ForegroundColor Green "Connecting to vCenter Server using credentials from SDDC object..."
$sddc = Get-VmcSddc -Name $SddcName
Connect-VIServer -Server $sddc.VCenterHostName -Credential $sddc.VCenterCredentials
} else {
Write-Host -ForegroundColor Blue "Not attempting to connect to vCenter Server. Use -IncludeVCenterConnect switch to connect using stored credentials."
}