From ca508570fba07d03ef9d19a364d9b606ce8ff436 Mon Sep 17 00:00:00 2001 From: Grzegorz Kulikowski Date: Mon, 5 Oct 2020 20:28:40 +0200 Subject: [PATCH] Add support for quick disconnect to Disconnect-SsoAdminServer In case one is connected just to one SSO server the cmdlet does not need to take the -server parameter as it will be discovered from $DefaultSsoAdminServers. In case there are more than 1 SSO servers it will not disconnect any connection, instead it will ask to be more precise using the Server parameter. --- .../VMware.vSphere.SsoAdmin.psm1 | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 b/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 index f2d89f6..9d0898b 100644 --- a/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 +++ b/Modules/VMware.vSphere.SsoAdmin/VMware.vSphere.SsoAdmin.psm1 @@ -147,7 +147,7 @@ function Connect-SsoAdminServer { } function Disconnect-SsoAdminServer { -<# + <# .NOTES =========================================================================== Created on: 9/29/2020 @@ -167,18 +167,45 @@ function Disconnect-SsoAdminServer { Disconnect a SSO Admin connection stored in 'mySsoAdminConnection' varaible #> -[CmdletBinding()] - param( - [Parameter( - Mandatory=$true, - ValueFromPipeline=$true, - ValueFromPipelineByPropertyName=$false, - HelpMessage='SsoAdminServer object')] - [ValidateNotNull()] - [VMware.vSphere.SsoAdminClient.DataTypes.SsoAdminServer] - $Server) + [CmdletBinding()] + param( + [Parameter( + ValueFromPipeline = $true, + ValueFromPipelineByPropertyName = $false, + HelpMessage = 'SsoAdminServer object')] + [ValidateNotNull()] + [VMware.vSphere.SsoAdminClient.DataTypes.SsoAdminServer] + $Server + ) Process { + if (-not $PSBoundParameters['Server']) { + switch (@($global:DefaultSsoAdminServers).count) { + { $_ -eq 1 } { $server = ($global:DefaultSsoAdminServers).ToArray()[0] ; break } + { $_ -gt 1 } { + $PSCmdlet.ThrowTerminatingError( + [System.Management.Automation.ErrorRecord]::new( + ([System.ApplicationException]"Connected to more than 1 SSO server, please specify a SSO server via -Server parameter"), + 'Disconnect-SsoAdminServer.ConnectedToMoreThanOneSSO', + [System.Management.Automation.ErrorCategory]::InvalidOperation, + $global:DefaultSsoAdminServers + ) + ) + break + } + Default { + $PSCmdlet.ThrowTerminatingError( + [System.Management.Automation.ErrorRecord]::new( + ([System.ApplicationException]"Not connected to SSO server."), + 'Disconnect-SsoAdminServer.NotConnectedToSSO', + [System.Management.Automation.ErrorCategory]::ConnectionError, + $global:DefaultSsoAdminServers + ) + ) + } + } + } + if ($global:DefaultSsoAdminServers.Contains($Server)) { $global:DefaultSsoAdminServers.Remove($Server) | Out-Null }