Merge pull request #3 from vmware/master

Update local fork
This commit is contained in:
Kyle Ruddy
2019-09-15 06:30:12 -04:00
committed by GitHub
9 changed files with 1408 additions and 327 deletions

View File

@@ -36,7 +36,12 @@ Description = 'PowerShell Module for Managing Hybrid Cloud Extension (HCX) on VM
PowerShellVersion = '6.0'
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Connect-HcxServer', 'Get-HcxCloudConfig', 'Get-HcxEndpoint', 'New-HcxMigration', 'Get-HcxMigration', 'Connect-HcxVAMI', 'Get-HcxVCConfig', 'Set-HcxLicense', 'Set-HcxVCConfig', 'Get-HcxNSXConfig', 'Set-HcxNSXConfig', 'Get-HcxCity', 'Get-HcxLocation', 'Set-HcxLocation', 'Get-HcxRoleMapping', 'Set-HcxRoleMapping', 'Get-HcxProxy', 'Set-HcxProxy', 'Remove-HcxProxy', 'Get-HcxLicense'
FunctionsToExport = 'Connect-HcxServer', 'Get-HcxCloudConfig', 'Get-HcxEndpoint', 'New-HcxMigration', 'Get-HcxMigration', 'Connect-HcxVAMI',
'Get-HcxVCConfig', 'Set-HcxLicense', 'Set-HcxVCConfig', 'Get-HcxNSXConfig', 'Set-HcxNSXConfig', 'Get-HcxCity', 'Get-HcxLocation', 'Set-HcxLocation',
'Get-HcxRoleMapping', 'Set-HcxRoleMapping', 'Get-HcxProxy', 'Set-HcxProxy', 'Remove-HcxProxy', 'Connect-HcxCloudServer', 'Get-HCXCloudActivationKey',
'Get-HCXCloudSubscription', 'New-HCXCloudActivationKey', 'Get-HCXCloud', 'Set-HCXCloud'
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()

View File

@@ -1357,4 +1357,346 @@ Function Remove-HcxProxy {
Write-Warning "No proxy settings were configured"
}
}
}
Function Connect-HcxCloudServer {
<#
.NOTES
===========================================================================
Created by: William Lam
Date: 06/19/2019
Organization: VMware
Blog: http://www.virtuallyghetto.com
Twitter: @lamw
===========================================================================
.SYNOPSIS
Connect to the HCX Cloud Service
.DESCRIPTION
This cmdlet connects to the HCX Cloud Service
.EXAMPLE
Connect-HcxCloudServer -RefreshToken
#>
Param (
[Parameter(Mandatory=$true)][String]$RefreshToken,
[Switch]$Troubleshoot
)
$results = Invoke-WebRequest -Uri "https://console.cloud.vmware.com/csp/gateway/am/api/auth/api-tokens/authorize?refresh_token=$RefreshToken" -Method POST -ContentType "application/json" -UseBasicParsing -Headers @{"csp-auth-token"="$RefreshToken"}
if($results.StatusCode -ne 200) {
Write-Host -ForegroundColor Red "Failed to retrieve Access Token, please ensure your VMC Refresh Token is valid and try again"
break
}
$accessToken = ($results | ConvertFrom-Json).access_token
$payload = @{
token = $accessToken;
}
$body = $payload | ConvertTo-Json
$hcxCloudLoginUrl = "https://connect.hcx.vmware.com/provider/csp/api/sessions"
if($PSVersionTable.PSEdition -eq "Core") {
$results = Invoke-WebRequest -Uri $hcxCloudLoginUrl -Body $body -Method POST -UseBasicParsing -ContentType "application/json" -SkipCertificateCheck
} else {
$results = Invoke-WebRequest -Uri $hcxCloudLoginUrl -Body $body -Method POST -UseBasicParsing -ContentType "application/json"
}
if($results.StatusCode -eq 200) {
$hcxAuthToken = $results.Headers.'x-hm-authorization'
$headers = @{
"x-hm-authorization"="$hcxAuthToken"
"Content-Type"="application/json"
"Accept"="application/json"
}
$global:hcxCloudConnection = new-object PSObject -Property @{
'Server' = "https://connect.hcx.vmware.com/provider/csp/consumer/api";
'headers' = $headers
}
$global:hcxCloudConnection
} else {
Write-Error "Failed to connect to HCX Cloud Service, please verify your CSP Refresh Token is valid"
}
}
Function Get-HCXCloudActivationKey {
<#
.NOTES
===========================================================================
Created by: William Lam
Date: 06/19/2019
Organization: VMware
Blog: http://www.virtuallyghetto.com
Twitter: @lamw
===========================================================================
.SYNOPSIS
Returns the activation keys from HCX Cloud
.DESCRIPTION
This cmdlet returns the activation keys from HCX Cloud
.EXAMPLE
Get-HCXCloudActivationKeys
.EXAMPLE
Get-HCXCloudActivationKeys -Type [AVAILABLE|CONSUMED|DEACTIVATED|DELETED]
#>
Param (
[Parameter(Mandatory=$false)][ValidateSet("AVAILABLE","CONSUMED","DEACTIVATED","DELETED")][String]$Type,
[Switch]$Troubleshoot
)
If (-Not $global:hcxCloudConnection) { Write-error "HCX Auth Token not found, please run Connect-HcxVAMI " } Else {
$method = "GET"
$hcxLicenseUrl = $global:hcxCloudConnection.Server + "/activationKeys"
if($Troubleshoot) {
Write-Host -ForegroundColor cyan "`n[DEBUG] - $METHOD`n$hcxLicenseUrl`n"
}
if($PSVersionTable.PSEdition -eq "Core") {
$results = Invoke-WebRequest -Uri $hcxLicenseUrl -Method $method -Headers $global:hcxCloudConnection.headers -UseBasicParsing -SkipCertificateCheck
} else {
$results = Invoke-WebRequest -Uri $hcxLicenseUrl -Method $method -Headers $global:hcxCloudConnection.headers -UseBasicParsing
}
if($Type) {
($results.content | ConvertFrom-Json).result.activationKeys | where { $_.status -eq $Type}
} else {
($results.content | ConvertFrom-Json).result.activationKeys
}
}
}
Function Get-HCXCloudSubscription {
<#
.NOTES
===========================================================================
Created by: William Lam
Date: 06/19/2019
Organization: VMware
Blog: http://www.virtuallyghetto.com
Twitter: @lamw
===========================================================================
.SYNOPSIS
Returns the subscription information for HCX CLoud Service
.DESCRIPTION
This cmdlet returns the subscription information for HCX Cloud Service
.EXAMPLE
Get-HCXCloudSubscription
#>
Param (
[Switch]$Troubleshoot
)
If (-Not $global:hcxCloudConnection) { Write-error "HCX Auth Token not found, please run Connect-HcxVAMI " } Else {
$method = "GET"
$hcxSubscriptionUrl = $global:hcxCloudConnection.Server + "/subscriptions"
if($Troubleshoot) {
Write-Host -ForegroundColor cyan "`n[DEBUG] - $METHOD`n$hcxSubscriptionUrl`n"
}
if($PSVersionTable.PSEdition -eq "Core") {
$results = Invoke-WebRequest -Uri $hcxSubscriptionUrl -Method $method -Headers $global:hcxCloudConnection.headers -UseBasicParsing -SkipCertificateCheck
} else {
$results = Invoke-WebRequest -Uri $hcxSubscriptionUrl -Method $method -Headers $global:hcxCloudConnection.headers -UseBasicParsing
}
($results.content | ConvertFrom-Json).subscriptions | select @{Name = "SID"; Expression = {$_.sid}},@{Name = "STATUS"; Expression = {$_.status}},@{Name = 'OfferName'; Expression = {$_.subscriptionComponents.offerName}}
}
}
Function New-HCXCloudActivationKey {
<#
.NOTES
===========================================================================
Created by: William Lam
Date: 06/19/2019
Organization: VMware
Blog: http://www.virtuallyghetto.com
Twitter: @lamw
===========================================================================
.SYNOPSIS
Requests new HCX Activation License Key
.DESCRIPTION
This cmdlet requests new HCX Activation License Key
.EXAMPLE
Get-HCXCloudActivationKey -SID <SID> -SystemType [HCX-CLOUD|HCX-ENTERPRISE]
#>
Param (
[Parameter(Mandatory=$true)][String]$SID,
[Parameter(Mandatory=$true)][ValidateSet("HCX-CLOUD","HCX-ENTERPRISE")][String]$SystemType,
[Switch]$Troubleshoot
)
If (-Not $global:hcxCloudConnection) { Write-error "HCX Auth Token not found, please run Connect-HcxVAMI " } Else {
$method = "POST"
$hcxLicenseUrl = $global:hcxCloudConnection.Server + "/activationKeys"
$payload = @{
numberOfKeys = "1";
sid = $SID;
systemType = ($SystemType).toLower();
}
$body = $payload | ConvertTo-Json
if($Troubleshoot) {
Write-Host -ForegroundColor cyan "`n[DEBUG] - $METHOD`n$hcxSubscriptionUrl`n"
Write-Host -ForegroundColor cyan "[DEBUG]`n$body`n"
}
try {
if($PSVersionTable.PSEdition -eq "Core") {
$requests = Invoke-WebRequest -Uri $hcxLicenseUrl -Method $method -Body $body -Headers $global:hcxCloudConnection.headers -UseBasicParsing -SkipCertificateCheck
} else {
$requests = Invoke-WebRequest -Uri $hcxLicenseUrl -Method $method -Body $body -Headers $global:hcxCloudConnection.headers -UseBasicParsing
}
} catch {
if($_.Exception.Response.StatusCode -eq "Unauthorized") {
Write-Host -ForegroundColor Red "`nThe HCX Cloud session is no longer valid, please re-run the Connect-HCXCloudServer cmdlet to retrieve a new token`n"
break
} else {
Write-Error "Error in requesting new HCX license key"
Write-Error "`n($_.Exception.Message)`n"
break
}
}
if($requests.StatusCode -eq 200) {
Write-Host "Successfully requestd new $SystemType License Key"
($requests.content | ConvertFrom-Json).activationKeys
}
}
}
Function Get-HCXCloud {
<#
.NOTES
===========================================================================
Created by: William Lam
Date: 06/19/2019
Organization: VMware
Blog: http://www.virtuallyghetto.com
Twitter: @lamw
===========================================================================
.SYNOPSIS
Returns HCX deployment information for all SDDCs
.DESCRIPTION
This cmdlet returns HCX deployment information for all SDDCs
.EXAMPLE
Get-HCXCloud
#>
Param (
[Switch]$Troubleshoot
)
If (-Not $global:hcxCloudConnection) { Write-error "HCX Auth Token not found, please run Connect-HcxVAMI " } Else {
$method = "GET"
$hcxCloudSDDCUrl = $global:hcxCloudConnection.Server + "/sddcs"
if($Troubleshoot) {
Write-Host -ForegroundColor cyan "`n[DEBUG] - $METHOD`n$hcxSubscriptionUrl`n"
}
if($PSVersionTable.PSEdition -eq "Core") {
$results = Invoke-WebRequest -Uri $hcxCloudSDDCUrl -Method $method -Headers $global:hcxCloudConnection.headers -UseBasicParsing -SkipCertificateCheck
} else {
$results = Invoke-WebRequest -Uri $hcxCloudSDDCUrl -Method $method -Headers $global:hcxCloudConnection.headers -UseBasicParsing
}
($results.content | ConvertFrom-Json).sddcs | Sort-Object -Property Name | select @{Name = "SDDCName"; Expression = {$_.name}}, @{Name = "SDDCID"; Expression = {$_.id}}, @{Name = "HCXStatus"; Expression = {$_.activationStatus}}, @{Name = "Region"; Expression = {$_.region}}
}
}
Function Set-HCXCloud {
<#
.NOTES
===========================================================================
Created by: William Lam
Date: 06/19/2019
Organization: VMware
Blog: http://www.virtuallyghetto.com
Twitter: @lamw
===========================================================================
.SYNOPSIS
Activate or Deactivate HCX for given VMC SDDC
.DESCRIPTION
This cmdlet activates or deactivates HCX for given VMC SDDC
.EXAMPLE
Set-HCXCloud -Activate -SDDCID $SDDCID
.EXAMPLE
Set-HCXCloud -Deactivate -SDDCID $SDDCID
#>
Param (
[Parameter(Mandatory=$true)][String]$SDDCID,
[Switch]$Activate,
[Switch]$Deactivate,
[Switch]$Troubleshoot
)
If (-Not $global:hcxCloudConnection) { Write-error "HCX Auth Token not found, please run Connect-HcxVAMI " } Else {
$method = "POST"
if($Activate) {
$HcxSid = (Get-HCXCloudSubscription | where {$_.STATUS -eq "ACTIVE"}).SID
# Check to see if there is an available HCX-Cloud Key
$HcxKey = ((Get-HCXCloudActivationKey -Type AVAILABLE | where {$_.systemType -eq 'hcx-cloud'}) | select -First 1).activationKey
if($HcxKey -eq $null) {
$HcxKey = New-HCXCloudActivationKey -SID $HcxSid -SystemType HCX-CLOUD
}
if($HCXKey -eq $null -or $HcxSid -eq $null) {
Write-Error "Failed to retrieve HCX Subscription ID or request HCX Cloud License Key"
break
}
$hcxSDDCUrl = $global:hcxCloudConnection.Server + "/sddcs/$($SDDCID)?action=activate"
$payload = @{
activationKey = $HcxKey;
}
} else {
$payload = ""
$hcxSDDCUrl = $global:hcxCloudConnection.Server + "/sddcs/$($SDDCID)?action=deactivate"
}
$body = $payload | ConvertTo-Json
if($Troubleshoot) {
Write-Host -ForegroundColor cyan "`n[DEBUG] - $METHOD`n$hcxSDDCUrl`n"
Write-Host -ForegroundColor cyan "[DEBUG]`n$body`n"
}
try {
if($PSVersionTable.PSEdition -eq "Core") {
$requests = Invoke-WebRequest -Uri $hcxSDDCUrl -Method $method -Body $body -Headers $global:hcxCloudConnection.headers -UseBasicParsing -SkipCertificateCheck
} else {
$requests = Invoke-WebRequest -Uri $hcxSDDCUrl -Method $method -Body $body -Headers $global:hcxCloudConnection.headers -UseBasicParsing
}
} catch {
if($_.Exception.Response.StatusCode -eq "Unauthorized") {
Write-Host -ForegroundColor Red "`nThe HCX Cloud session is no longer valid, please re-run the Connect-HCXCloudServer cmdlet to retrieve a new token`n"
break
} else {
Write-Error "Error in attempting to activate or deactivate HCX"
Write-Error "`n($_.Exception.Message)`n"
break
}
}
if($requests.StatusCode -eq 200) {
if($Activate) {
Write-Host "Activating HCX for SDDC: $SDDCID, starting deployment. You can monitor the status using the HCX Cloud Console"
} else {
Write-Host "Deactivating HCX for SDDC: $SDDCID, starting un-deploymentt. You can monitor the status using the HCX Cloud Console"
}
($requests.content | ConvertFrom-Json)
}
}
}

View File

@@ -12,7 +12,7 @@
# RootModule = ''
# Version number of this module.
ModuleVersion = '1.1'
ModuleVersion = '1.3'
# ID used to uniquely identify this module
GUID = '6d3f7fb5-4e52-43d8-91e1-f65f72532a1d'

File diff suppressed because it is too large Load Diff

View File

@@ -37,12 +37,13 @@ PowerShellVersion = '6.0'
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Connect-NSXTProxy', 'Get-NSXTSegment', 'New-NSXTSegment', 'Remove-NSXTSegment', `
'Get-NSXTGroup', 'New-NSXTGroup', 'Remove-NSXTGroup', 'Get-NSXTService', 'New-NSXTService', `
'Get-NSXTGroup', 'New-NSXTGroup', 'Remove-NSXTGroup', 'Get-NSXTServiceDefinition', 'New-NSXTServiceDefinition', `
'Get-NSXTFirewall', 'New-NSXTFirewall', 'Remove-NSXTFirewall', 'Get-NSXTDistFirewallSection', `
'Get-NSXTDistFirewall', 'New-NSXTDistFirewall', 'Remove-NSXTDistFirewall', 'Get-NSXTRouteTable', `
'Get-NSXTOverviewInfo', 'Get-NSXTInfraScope', 'Get-NSXTInfraGroup', 'New-NSXTRouteBasedVPN', `
'Get-NSXTRouteBasedVPN', 'Remove-NSXTRouteBasedVPN', 'Remove-NSXTService', 'New-NSXTDistFirewallSection', 'Get-NSXTDistFirewallSection', `
'New-NSXTPolicyBasedVPN', 'Get-NSXTPolicyBasedVPN', 'Remove-NSXTPolicyBasedVPN', 'Get-NSXTDNS', 'Set-NSXTDNS'
'Get-NSXTRouteBasedVPN', 'Remove-NSXTRouteBasedVPN', 'Remove-NSXTServiceDefinition', 'New-NSXTDistFirewallSection', 'Get-NSXTDistFirewallSection', `
'New-NSXTPolicyBasedVPN', 'Get-NSXTPolicyBasedVPN', 'Remove-NSXTPolicyBasedVPN', 'Get-NSXTDNS', 'Set-NSXTDNS', 'Get-NSXTPublicIP', 'New-NSXTPublicIP', `
'Get-NSXTNatRule', 'New-NSXTNatRule', 'Remove-NSXTNatRule'
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()

View File

@@ -520,7 +520,7 @@ Function New-NSXTFirewall {
if($serviceName -eq "ANY") {
$services = @("ANY")
} else {
$tmp = (Get-NSXTService -Name "$serviceName").Path
$tmp = (Get-NSXTServiceDefinition -Name "$serviceName").Path
$services+=$tmp
}
}
@@ -854,7 +854,7 @@ Function Remove-NSXTGroup {
}
}
Function Get-NSXTService {
Function Get-NSXTServiceDefinition {
<#
.NOTES
===========================================================================
@@ -870,9 +870,9 @@ Function Get-NSXTService {
.DESCRIPTION
This cmdlet retrieves all NSX-T Services
.EXAMPLE
Get-NSXTService
Get-NSXTServiceDefinition
.EXAMPLE
Get-NSXTService -Name "WINS"
Get-NSXTServiceDefinition -Name "WINS"
#>
param(
[Parameter(Mandatory=$false)][String]$Name,
@@ -933,7 +933,7 @@ Function Get-NSXTService {
}
}
Function Remove-NSXTService {
Function Remove-NSXTServiceDefinition {
<#
.NOTES
===========================================================================
@@ -949,7 +949,7 @@ Function Remove-NSXTService {
.DESCRIPTION
This cmdlet removes an NSX-T Service
.EXAMPLE
Remove-NSXTService -Id VMware-Blast -Troubleshoot
Remove-NSXTServiceDefinition -Id VMware-Blast -Troubleshoot
#>
Param (
[Parameter(Mandatory=$True)]$Id,
@@ -987,7 +987,7 @@ Function Remove-NSXTService {
}
}
Function New-NSXTService {
Function New-NSXTServiceDefinition {
<#
.NOTES
===========================================================================
@@ -1003,7 +1003,7 @@ Function New-NSXTService {
.DESCRIPTION
This cmdlet creates a new NSX-T Service
.EXAMPLE
New-NSXTService -Name "MyHTTP2" -Protocol TCP -DestinationPorts @("8080","8081")
New-NSXTServiceDefinition -Name "MyHTTP2" -Protocol TCP -DestinationPorts @("8080","8081")
#>
Param (
[Parameter(Mandatory=$True)]$Name,
@@ -1160,7 +1160,7 @@ Function Get-NSXTDistFirewallSection {
if($PSVersionTable.PSEdition -eq "Core") {
$requests = Invoke-WebRequest -Uri $distFirewallSectionURL -Method $method -Headers $global:nsxtProxyConnection.headers -SkipCertificateCheck
} else {
$requests = Invoke-WebRequest -Uri $distFirdistFirewallSectionURLwallURL -Method $method -Headers $global:nsxtProxyConnection.headers
$requests = Invoke-WebRequest -Uri $distFirewallSectionURL -Method $method -Headers $global:nsxtProxyConnection.headers
}
} catch {
if($_.Exception.Response.StatusCode -eq "Unauthorized") {
@@ -2571,16 +2571,17 @@ Function Set-NSXTDNS {
$method = "PATCH"
$dnsURL = $global:nsxtProxyConnection.Server + "/policy/api/v1/infra/dns-forwarder-zones/$($GatewayType.toLower())-dns-zone"
if($Troubleshoot) {
Write-Host -ForegroundColor cyan "`n[DEBUG] - $method`n$dnsURL`n"
}
$payload = @{
upstream_servers = @($DNS)
}
$body = $payload | ConvertTo-Json -Depth 5
if($Troubleshoot) {
Write-Host -ForegroundColor cyan "`n[DEBUG] - $method`n$dnsURL`n"
Write-Host -ForegroundColor cyan "[DEBUG]`n$body`n"
}
try {
if($PSVersionTable.PSEdition -eq "Core") {
$requests = Invoke-WebRequest -Uri $dnsURL -Body $body -Method $method -Headers $global:nsxtProxyConnection.headers -SkipCertificateCheck
@@ -2602,4 +2603,289 @@ Function Set-NSXTDNS {
Write-Host "Successfully updated NSX-T DNS for $GatewayType"
}
}
}
Function Get-NSXTPublicIP {
param(
[Parameter(Mandatory=$false)][String]$Name,
[Switch]$Troubleshoot
)
If (-Not $global:nsxtProxyConnection) { Write-error "No NSX-T Proxy Connection found, please use Connect-NSXTProxy" } Else {
$method = "GET"
$publicIPURL = ($global:nsxtProxyConnection.Server).replace("/sks-nsxt-manager","") + "/cloud-service/api/v1/infra/public-ips"
if($Troubleshoot) {
Write-Host -ForegroundColor cyan "`n[DEBUG] - $method`n$publicIPURL`n"
}
try {
if($PSVersionTable.PSEdition -eq "Core") {
$requests = Invoke-WebRequest -Uri $publicIPURL -Method $method -Headers $global:nsxtProxyConnection.headers -SkipCertificateCheck
} else {
$requests = Invoke-WebRequest -Uri $publicIPURL -Method $method -Headers $global:nsxtProxyConnection.headers
}
} catch {
if($_.Exception.Response.StatusCode -eq "Unauthorized") {
Write-Host -ForegroundColor Red "`nThe NSX-T Proxy session is no longer valid, please re-run the Connect-NSXTProxy cmdlet to retrieve a new token`n"
break
} else {
Write-Error "Error in retrieving NSX-T Public IPs"
Write-Error "`n($_.Exception.Message)`n"
break
}
}
if($requests.StatusCode -eq 200) {
$results = ($requests.Content | ConvertFrom-Json).results | select display_name,id,ip
if ($PSBoundParameters.ContainsKey("Name")){
$results | where {$_.display_name -eq $Name}
} else {
$results
}
}
}
}
Function New-NSXTPublicIP {
Param(
[Parameter(Mandatory=$false)][String]$Name,
[Switch]$Troubleshoot
)
If (-Not $global:nsxtProxyConnection) { Write-error "No NSX-T Proxy Connection found, please use Connect-NSXTProxy" } Else {
$method = "PUT"
$publicIPURL = ($global:nsxtProxyConnection.Server).replace("/sks-nsxt-manager","") + "/cloud-service/api/v1/infra/public-ips/$($Name)"
$payload = @{
display_name = "$Name";
}
$body = $payload | ConvertTo-Json
if($Troubleshoot) {
Write-Host -ForegroundColor cyan "`n[DEBUG] - $method`n$publicIPURL`n"
Write-Host -ForegroundColor cyan "[DEBUG]`n$body`n"
}
try {
if($PSVersionTable.PSEdition -eq "Core") {
$requests = Invoke-WebRequest -Uri $publicIPURL -Method $method -Body $body -Headers $global:nsxtProxyConnection.headers -SkipCertificateCheck
} else {
$requests = Invoke-WebRequest -Uri $publicIPURL -Method $method -Body $body -Headers $global:nsxtProxyConnection.headers
}
} catch {
if($_.Exception.Response.StatusCode -eq "Unauthorized") {
Write-Host -ForegroundColor Red "`nThe NSX-T Proxy session is no longer valid, please re-run the Connect-NSXTProxy cmdlet to retrieve a new token`n"
break
} else {
Write-Error "Error in retrieving NSX-T Public IPs"
Write-Error "`n($_.Exception.Message)`n"
break
}
}
if($requests.StatusCode -eq 200) {
Write-Host "Successfully requested new NSX-T Public IP Address"
($requests.Content | ConvertFrom-Json) | select display_name,id,ip
}
}
}
Function Remove-NSXTPublicIP {
Param(
[Parameter(Mandatory=$false)][String]$Name,
[Switch]$Troubleshoot
)
If (-Not $global:nsxtProxyConnection) { Write-error "No NSX-T Proxy Connection found, please use Connect-NSXTProxy" } Else {
$method = "DELETE"
$publicIPURL = ($global:nsxtProxyConnection.Server).replace("/sks-nsxt-manager","") + "/cloud-service/api/v1/infra/public-ips/$($Name)"
if($Troubleshoot) {
Write-Host -ForegroundColor cyan "`n[DEBUG] - $method`n$publicIPURL`n"
}
try {
if($PSVersionTable.PSEdition -eq "Core") {
$requests = Invoke-WebRequest -Uri $publicIPURL -Method $method -Headers $global:nsxtProxyConnection.headers -SkipCertificateCheck
} else {
$requests = Invoke-WebRequest -Uri $publicIPURL -Method $method -Headers $global:nsxtProxyConnection.headers
}
} catch {
if($_.Exception.Response.StatusCode -eq "Unauthorized") {
Write-Host -ForegroundColor Red "`nThe NSX-T Proxy session is no longer valid, please re-run the Connect-NSXTProxy cmdlet to retrieve a new token`n"
break
} else {
Write-Error "Error in deleting NSX-T Public IPs"
Write-Error "`n($_.Exception.Message)`n"
break
}
}
if($requests.StatusCode -eq 200) {
Write-Host "Successfully deleted NSX-T Public IP Address $Name"
}
}
}
Function Get-NSXTNatRule {
param(
[Parameter(Mandatory=$false)][String]$Name,
[Switch]$Troubleshoot
)
If (-Not $global:nsxtProxyConnection) { Write-error "No NSX-T Proxy Connection found, please use Connect-NSXTProxy" } Else {
$method = "GET"
$natURL = $global:nsxtProxyConnection.Server + "/policy/api/v1/infra/tier-1s/cgw/nat/USER/nat-rules"
if($Troubleshoot) {
Write-Host -ForegroundColor cyan "`n[DEBUG] - $method`n$natURL`n"
}
try {
if($PSVersionTable.PSEdition -eq "Core") {
$requests = Invoke-WebRequest -Uri $natURL -Method $method -Headers $global:nsxtProxyConnection.headers -SkipCertificateCheck
} else {
$requests = Invoke-WebRequest -Uri $natURL -Method $method -Headers $global:nsxtProxyConnection.headers
}
} catch {
if($_.Exception.Response.StatusCode -eq "Unauthorized") {
Write-Host -ForegroundColor Red "`nThe NSX-T Proxy session is no longer valid, please re-run the Connect-NSXTProxy cmdlet to retrieve a new token`n"
break
} else {
Write-Error "Error in retrieving NSX-T Public IPs"
Write-Error "`n($_.Exception.Message)`n"
break
}
}
if($requests.StatusCode -eq 200) {
$results = ($requests.Content | ConvertFrom-Json).results | select id,display_name,sequence_number,source_network,translated_network,destination_network,translated_ports,service,scope
if ($PSBoundParameters.ContainsKey("Name")){
$results | where {$_.display_name -eq $Name}
} else {
$results
}
}
}
}
Function New-NSXTNatRule {
Param(
[Parameter(Mandatory=$true)][String]$Name,
[Parameter(Mandatory=$true)][String]$PublicIP,
[Parameter(Mandatory=$true)][String]$InternalIP,
[Parameter(Mandatory=$true)][String]$Service,
[Switch]$Troubleshoot
)
If (-Not $global:nsxtProxyConnection) { Write-error "No NSX-T Proxy Connection found, please use Connect-NSXTProxy" } Else {
$method = "PUT"
$natURL = $global:nsxtProxyConnection.Server + "/policy/api/v1/infra/tier-1s/cgw/nat/USER/nat-rules/$($Name)"
if($service -eq "ANY") {
$payload = @{
display_name = $Name;
action = "REFLEXIVE";
service = "";
translated_network = $PublicIP;
source_network = $InternalIP;
scope = @("/infra/labels/cgw-public");
firewall_match = "MATCH_INTERNAL_ADDRESS";
logging = $false;
enabled = $true;
sequence_number = 0;
}
} else {
$nsxtService = Get-NSXTServiceDefinition -Name $Service
$servicePath = $nsxtService.path
$servicePort = $nsxtService.Destination
$payload = @{
display_name = $Name;
action = "DNAT";
service = $servicePath;
translated_network = $InternalIP;
translated_ports = $servicePort;
destination_network = $PublicIP
scope = @("/infra/labels/cgw-public");
firewall_match = "MATCH_EXTERNAL_ADDRESS";
logging = $false;
enabled = $true;
sequence_number = 0;
}
}
$body = $payload | ConvertTo-Json -Depth 5
if($Troubleshoot) {
Write-Host -ForegroundColor cyan "`n[DEBUG] - $method`n$natURL`n"
Write-Host -ForegroundColor cyan "[DEBUG]`n$body`n"
}
try {
if($PSVersionTable.PSEdition -eq "Core") {
$requests = Invoke-WebRequest -Uri $natURL -Method $method -Body $body -Headers $global:nsxtProxyConnection.headers -SkipCertificateCheck
} else {
$requests = Invoke-WebRequest -Uri $natURL -Method $method -Body $body -Headers $global:nsxtProxyConnection.headers
}
} catch {
if($_.Exception.Response.StatusCode -eq "Unauthorized") {
Write-Host -ForegroundColor Red "`nThe NSX-T Proxy session is no longer valid, please re-run the Connect-NSXTProxy cmdlet to retrieve a new token`n"
break
} else {
Write-Error "Error in creating NSX-T NAT Rule"
Write-Error "`n($_.Exception.Message)`n"
break
}
}
if($requests.StatusCode -eq 200) {
Write-Host "Successfully create new NAT Rule"
($requests.Content | ConvertFrom-Json) | select id,display_name,sequence_number,source_network,translated_network,destination_network,translated_ports,service,scope
}
}
}
Function Remove-NSXTNatRule {
Param(
[Parameter(Mandatory=$false)][String]$Name,
[Switch]$Troubleshoot
)
If (-Not $global:nsxtProxyConnection) { Write-error "No NSX-T Proxy Connection found, please use Connect-NSXTProxy" } Else {
$natRuleId = (Get-NSXTNatRule -Name $Name).id
$method = "DELETE"
$natURL = $global:nsxtProxyConnection.Server + "/policy/api/v1/infra/tier-1s/cgw/nat/USER/nat-rules/$($natRuleId)"
if($Troubleshoot) {
Write-Host -ForegroundColor cyan "`n[DEBUG] - $method`n$natURL`n"
}
try {
if($PSVersionTable.PSEdition -eq "Core") {
$requests = Invoke-WebRequest -Uri $natURL -Method $method -Headers $global:nsxtProxyConnection.headers -SkipCertificateCheck
} else {
$requests = Invoke-WebRequest -Uri $natURL -Method $method -Headers $global:nsxtProxyConnection.headers
}
} catch {
if($_.Exception.Response.StatusCode -eq "Unauthorized") {
Write-Host -ForegroundColor Red "`nThe NSX-T Proxy session is no longer valid, please re-run the Connect-NSXTProxy cmdlet to retrieve a new token`n"
break
} else {
Write-Error "Error in deleting NSX-T NAT Rule"
Write-Error "`n($_.Exception.Message)`n"
break
}
}
if($requests.StatusCode -eq 200) {
Write-Host "Successfully deleted NAT Rule $Name"
}
}
}

View File

@@ -81,13 +81,13 @@ Function Get-VMCOrg {
Return all the info about the orgs you are a part of
#>
Param (
[Parameter(Mandatory=$false)]$Name
[Parameter(Mandatory=$false)]$Name
)
If (-Not $global:DefaultVMCServers) { Write-error "No VMC Connection found, please use Connect-VMC to connect" } Else {
$orgService = Get-VMCService com.vmware.vmc.orgs
if ($PSBoundParameters.ContainsKey("Name")){
$orgs = $orgService.list() | Where {$_.display_name -match $Name}
$orgs = $orgService.list() | Where {$_.display_name -eq $Name}
} Else {
$orgs = $orgService.list()
}
@@ -130,7 +130,7 @@ Function Get-VMCSDDC {
$orgID = $org.ID
$sddcService = Get-VMCService com.vmware.vmc.orgs.sddcs
if ($PSBoundParameters.ContainsKey("Name")){
$sddcService.list($OrgID) | Where {$_.name -match $Name}
$sddcService.list($OrgID) | Where {$_.name -eq $Name}
} Else {
$sddcService.list($OrgID)
}