Public/Remove-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 |
<#
#> function Remove-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()] [PSCredential] $Credential ) $params = @{ GatewayEndpoint = $GatewayEndpoint } if ($Credential) { $params.Add('Credential',$Credential) } Write-Verbose -Message 'Retrieving existig connections in WAC ...' $existingConections = [PSCustomObject[]](Get-WacConnection @params) $desiredConnection = $existingConections.Where({($_.Type -eq $ConnectionType) -and ($_.Name -eq $ConnectionName)}) if (-not $desiredConnection) { throw "$ConnectionName of type $ConnectionType does not exist in WAC" } if ($desiredConnection.IsSharedConnection) { Write-Verbose -Message "$ConnectionName is a shared connection" $apiEndpoint = "/api/connections/${connectionType}!${connectionName}!global" } else { $apiEndpoint = "/api/connections/${connectionType}!${connectionName}" } $params.Add('APIEndpoint', $apiEndpoint) $params.Add('Method', 'Delete') Write-Verbose -Message 'Generating request parameters ...' $requestParameters = Get-RequestParameter @params Write-Verbose -Message 'Invoking remove WAC connection api ...' $response = Invoke-WebRequest @requestParameters -ErrorAction SilentlyContinue if (-not ($response.StatusCode -eq 204)) { throw "Error removing $ConnectionName of type $ConnectionType" } } |