Public/Add-WacConnection.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 |
<#
#> function Add-WacConnection { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [String] $GatewayEndpoint, [Parameter(Mandatory = $true)] [String] $ConnectionName, [Parameter(Mandatory = $true)] [ValidateSet('msft.sme.connection-type.server','msft.sme.connection-type.cluster','msft.sme.connection-type.hyper-converged-cluster','msft.sme.connection-type.windows-client')] [String] $ConnectionType, [Parameter()] [String[]] $Tags, [Parameter()] [PSCredential] $Credential, [Parameter()] [Switch] $SharedConnection ) $params = @{ GatewayEndpoint = $GatewayEndpoint } if ($Credential) { $params.Add('Credential',$Credential) } Write-Verbose -Message 'Retrieving existig connections in WAC ...' $existingConections = [PSCustomObject[]](Get-WacConnection @params) if ($existingConections.Where({$_.Type -eq $ConnectionType}).Name -contains $ConnectionName) { throw "$ConnectionName of type $ConnectionType already exists in WAC" } $params.Add('APIEndpoint', '/api/connections') $params.Add('Method','Put') Write-Verbose -Message 'Generating request parameters ...' $requestParameters = Get-RequestParameter @params $connectionObject = @() $connectionObject += @{ name = $ConnectionName type = $ConnectionType id = "${ConnectionType}!${ConnectionName}" tags = $Tags } if ($SharedConnection) { Write-Verbose -Message "Adding $ConnectionName as shared connection" $connectionObject[0].Add('groupId','global') } $requestParameters.Add('Body', '[' + $($connectionObject | ConvertTo-Json) + ']') Write-Verbose -Message 'Invoking add WAC connection api ...' $response = Invoke-WebRequest @requestParameters -ErrorAction SilentlyContinue if ($response.StatusCode -eq 200) { return ($response.Content | ConvertFrom-Json).Changes } else { throw "Error adding $ConnectionName of type $ConnectionType" } } |