Private/Authentication/Set-JCAuthentication.ps1
# Connect-JCOnline: $Auth = Set-JCAuthentication -JumpCloudApiKey:($JumpCloudApiKey) # Set-JCOrganization: $Auth = Set-JCAuthentication -JumpCloudApiKey:($JumpCloudApiKey) # If (-not [System.String]::IsNullOrEmpty($Auth)) # { # # Do stuff # } # Else # { # Write-Error ('Unable to set module authentication.') # } Function Set-JCAuthentication { Param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName, ParameterSetName = 'Api')]$JumpCloudApiKey, [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName, ParameterSetName = 'Org')]$JumpCloudOrgId ) DynamicParam { $Param_JumpCloudApiKey = @{ 'Name' = 'JumpCloudApiKey'; 'Type' = [System.String]; 'Position' = 0; 'ValueFromPipelineByPropertyName' = $true; 'ValidateNotNullOrEmpty' = $true; 'ValidateLength' = (40, 40); 'ParameterSets' = ('force', 'Interactive'); 'HelpMessage' = "Please enter your JumpCloud API key. This can be found in the JumpCloud admin console within 'API Settings' accessible from the drop down icon next to the admin email address in the top right corner of the JumpCloud admin console."; } $Param_JumpCloudOrgId = @{ 'Name' = 'JumpCloudOrgId'; 'Type' = [System.String]; 'Position' = 1; 'ValueFromPipelineByPropertyName' = $true; 'ValidateNotNullOrEmpty' = $true; 'ParameterSets' = ('force', 'Interactive'); 'HelpMessage' = 'Organization Id can be found in the Settings page within the admin console. Only needed for multi tenant admins.'; } # If the $env:JCApiKey is not set then make the JumpCloudApiKey mandatory else set the default value to be the env variable If ([System.String]::IsNullOrEmpty($env:JCApiKey)) { $Param_JumpCloudApiKey.Add('Mandatory', $true); } Else { $Param_JumpCloudApiKey.Add('Default', $env:JCApiKey); } # If the $env:JCOrgId is set then set the default value to be the env variable If (-not [System.String]::IsNullOrEmpty($env:JCOrgId)) { $Param_JumpCloudOrgId.Add('Default', $env:JCOrgId); } If ((Get-PSCallStack).Command -like '*MarkdownHelp') { $JCEnvironment = 'local' } If ($JCEnvironment -eq "local") { $Param_ip = @{ 'Name' = 'ip'; 'Type' = [System.String]; 'ValueFromPipelineByPropertyName' = $true; 'HelpMessage' = 'Enter an IP address'; } } # Build output # Build parameter array $RuntimeParameterDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary $ParamVarPrefix = 'Param_' Get-Variable -Scope:('Local') | Where-Object {$_.Name -like '*' + $ParamVarPrefix + '*'} | ForEach-Object { # Add RuntimeDictionary to each parameter $_.Value.Add('RuntimeParameterDictionary', $RuntimeParameterDictionary) # Creating each parameter $VarName = $_.Name $VarValue = $_.Value Try { New-DynamicParameter @VarValue | Out-Null } Catch { Write-Error -Message:('Unable to create dynamic parameter:"' + $VarName.Replace($ParamVarPrefix, '') + '"; Error:' + $Error) } } Return $RuntimeParameterDictionary } Begin { # Debug message for parameter call Invoke-Command -ScriptBlock:($ScriptBlock_DefaultDebugMessageBegin) -ArgumentList:($MyInvocation, $PsBoundParameters, $PSCmdlet) -NoNewScope } Process { Try { If ($PSCmdlet.ParameterSetName -eq 'Api') { # If "$JumpCloudApiKey" is populated or if "$env:JCApiKey" is not set If (-not ([System.String]::IsNullOrEmpty($JumpCloudApiKey))) { # Set $env:JCApiKey $env:JCApiKey = $JumpCloudApiKey $global:JCAPIKEY = $env:JCApiKey } If ([System.String]::IsNullOrEmpty($JumpCloudOrgId) -and [System.String]::IsNullOrEmpty($env:JCOrgId)) { Write-Host ('Connect-JCOnline; $JumpCloudOrgId IS NOT populated: "' + $JumpCloudOrgId + '"; AND $env:JCOrgId IS NOT populated: "' + $env:JCOrgId + '";') -ForegroundColor Black -BackgroundColor DarkBlue Set-JCOrganization -JumpCloudApiKey:($env:JCApiKey) } ElseIf (-not [System.String]::IsNullOrEmpty($JumpCloudOrgId) -and [System.String]::IsNullOrEmpty($env:JCOrgId)) { Write-Host ('Connect-JCOnline; ($JumpCloudOrgId IS populated: "' + $JumpCloudOrgId + '"; AND $env:JCOrgId IS NOT populated: "' + $env:JCOrgId + '";)') -ForegroundColor Black -BackgroundColor Cyan Set-JCOrganization -JumpCloudApiKey:($env:JCApiKey) -JumpCloudOrgId:($JumpCloudOrgId) } ElseIf ([System.String]::IsNullOrEmpty($JumpCloudOrgId) -and -not [System.String]::IsNullOrEmpty($env:JCOrgId)) { Write-Host ('Connect-JCOnline; ($JumpCloudOrgId IS NOT populated: "' + $JumpCloudOrgId + '"; AND $env:JCOrgId IS populated: "' + $env:JCOrgId + '";)') -ForegroundColor Black -BackgroundColor Gray Set-JCOrganization -JumpCloudApiKey:($env:JCApiKey) -JumpCloudOrgId:($env:JCOrgId) } ElseIf (-not [System.String]::IsNullOrEmpty($JumpCloudOrgId) -and -not [System.String]::IsNullOrEmpty($env:JCOrgId) -and $JumpCloudOrgId -ne $env:JCOrgId) { Write-Host ('Connect-JCOnline; $JumpCloudOrgId IS populated: "' + $JumpCloudOrgId + '"; AND $env:JCOrgId IS populated: "' + $env:JCOrgId + '"; AND $JumpCloudOrgId -ne $env:JCOrgId;)') -ForegroundColor Black -BackgroundColor Blue Set-JCOrganization -JumpCloudApiKey:($env:JCApiKey) -JumpCloudOrgId:($JumpCloudOrgId) } Else { Write-Debug ('The $JumpCloudOrgId supplied matches existing $env:JCOrgId.') $Org = Set-JCOrganization -JumpCloudApiKey:($env:JCApiKey) -JumpCloudOrgId:($env:JCOrgId) Return $Org } } ElseIf ($PSCmdlet.ParameterSetName -eq 'Org') { If ([System.String]::IsNullOrEmpty($JumpCloudApiKey) -and [System.String]::IsNullOrEmpty($env:JCApiKey)) { Write-Host ('Set-JCOrganization; $JumpCloudApiKey IS NOT populated: "' + $JumpCloudApiKey + '"; AND $env:JCApiKey IS NOT populated: "' + $env:JCApiKey + '";') -ForegroundColor Black -BackgroundColor DarkBlue Connect-JCOnline } ElseIf ((-not [System.String]::IsNullOrEmpty($JumpCloudApiKey) -and [System.String]::IsNullOrEmpty($env:JCApiKey))) { Write-Host ('Set-JCOrganization; $JumpCloudApiKey IS populated: "' + $JumpCloudApiKey + '"; AND $env:JCApiKey IS NOT populated: "' + $env:JCApiKey + '";') -ForegroundColor Black -BackgroundColor Cyan Connect-JCOnline -JumpCloudApiKey:($JumpCloudApiKey) } ElseIf ([System.String]::IsNullOrEmpty($JumpCloudApiKey) -and -not [System.String]::IsNullOrEmpty($env:JCApiKey)) { Write-Host ('Set-JCOrganization; $JumpCloudApiKey IS NOT populated: "' + $JumpCloudApiKey + '"; AND $env:JCApiKey IS populated: "' + $env:JCApiKey + '";') -ForegroundColor Black -BackgroundColor Gray Connect-JCOnline -JumpCloudApiKey:($env:JCApiKey) } ElseIf ((-not [System.String]::IsNullOrEmpty($JumpCloudApiKey) -and -not [System.String]::IsNullOrEmpty($env:JCApiKey)) -and $JumpCloudApiKey -ne $env:JCApiKey) { Write-Host ('Set-JCOrganization; $JumpCloudApiKey IS populated: "' + $JumpCloudApiKey + '"; AND $env:JCApiKey IS populated: "' + $env:JCApiKey + '"; AND $JumpCloudApiKey -ne $env:JCApiKey;') -ForegroundColor Black -BackgroundColor Blue Connect-JCOnline -JumpCloudApiKey:($JumpCloudApiKey) } Else { $Org = Set-JCOrganization -JumpCloudApiKey:($env:JCApiKey) -JumpCloudOrgId:($env:JCOrgId) $env:JCOrgId = $Org.Id $global:JCOrgId = $env:JCOrgId $env:JCOrgName = $Org.Name Return $Org } } Else { Write-Error ('Unknown ParameterSetName.') } } Catch { Write-Error ($_) } } End { } } |