From 22631f28362865a3777717d4dd13fb52a72b94f1 Mon Sep 17 00:00:00 2001 From: pinguinfuss Date: Thu, 9 Mar 2023 20:16:31 +0100 Subject: [PATCH] More reordering/cleanup --- src/Connection/Connect-To.ps1 | 65 ++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index a15ab77..95cb8c5 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -264,38 +264,30 @@ function Connect-To { } } 'NetAppFAS' { + # Construct the splatting for Connect-NcController + $params = @{ + Name = $RemoteHost + Credential = $creds + HTTPS = $true + } + + # Check if -PassThru was passed. If so, a) do not set the default variable and b) return the + # session object to the caller. + if ($PSBoundParameters.ContainsKey('PassThru')) { + $params.Add('Transient', $true) + } + try { - $null = Connect-NcController -Name $RemoteHost -Credential $creds -ErrorAction Stop -HTTPS + $handle = Connect-NcController @params } catch { # Write a error message to the log. Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) } - } - 'VMware' { - try { - Connect-VIServer -Server $RemoteHost -Credential $creds -ErrorAction Stop | Out-Null - } - catch { - # Write a error message to the log. - Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) - } - } - 'VMwareCisServer' { - try { - if ($PassThru.IsPresent) { - Connect-CisServer -Server $RemoteHost -Credential $creds - } - else { - $null = Connect-CisServer -Server $RemoteHost -Credential $creds - } - } - - catch { - # Write a error message to the log. - Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) + if ($PSBoundParameters.ContainsKey('PassThru')) { + return $handle } } 'SCP' { @@ -324,6 +316,31 @@ function Connect-To { Write-Error -Message $m } } + 'VMware' { + try { + Connect-VIServer -Server $RemoteHost -Credential $creds -ErrorAction Stop | Out-Null + } + + catch { + # Write a error message to the log. + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) + } + } + 'VMwareCisServer' { + try { + if ($PassThru.IsPresent) { + Connect-CisServer -Server $RemoteHost -Credential $creds + } + else { + $null = Connect-CisServer -Server $RemoteHost -Credential $creds + } + } + + catch { + # Write a error message to the log. + Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type) + } + } default { # Write a error message to the log. Write-Error -Message ('Unable to connect to {0} using Type {1}.' -f $RemoteHost, $Type)