Functions/Connection/New-OIMSession.ps1
|
function New-OIMSession { [CmdletBinding(SupportsShouldProcess)] param( [PSCredential]$Credential, [ValidateSet('DialogUser', 'RoleBasedADSAccount')] [string]$Module = 'RoleBasedADSAccount', [parameter( Mandatory = $true, ValueFromPipeline = $false, ValueFromPipelinebyPropertyName = $true )] [string]$BaseURI, [parameter( Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelinebyPropertyName = $true )] [string]$AppName = '', [Parameter( Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelinebyPropertyName = $false )] [pscredential]$IISCredential, [Parameter( Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelinebyPropertyName = $false )] [switch]$UseDefaultCredentials, [Parameter( Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelinebyPropertyName = $false )] [switch]$SkipVersionCheck, [parameter( Mandatory = $false, ValueFromPipeline = $false, ValueFromPipelinebyPropertyName = $true )] [switch]$SkipCertificateCheck ) BEGIN { #Modern OIM API Server (9.x) is hosted at the base URL directly. Older on-prem #Application Server deployments are hosted under a named application, e.g. -AppName AppServer if ([string]::IsNullOrEmpty($AppName)) { $Uri = $BaseURI.TrimEnd('/') } else { $Uri = "$($BaseURI.TrimEnd('/'))/$AppName" } #Hashtable to hold Logon Request $LogonRequest = @{ } #Define Logon Request Parameters $LogonRequest['Method'] = 'POST' $LogonRequest['SessionVariable'] = 'WebSession' $LogonRequest['ContentType'] = 'application/json' #Only use the caller's Windows identity when explicitly requested. #IISCredential is a separate, explicit credential for the IIS/reverse-proxy layer. if ($UseDefaultCredentials.IsPresent) { $LogonRequest['UseDefaultCredentials'] = $true } $LogonRequest['SkipCertificateCheck'] = $SkipCertificateCheck.IsPresent }#begin # Connecting PROCESS { $authdata = @{AuthString = "Module=$Module" } if ($null -ne $Credential ) { $authdata = @{AuthString = "Module=$Module;User=$($Credential.Username);Password=$($Credential.GetNetworkCredential().password)" } } $authJson = ConvertTo-Json $authdata -Depth 2 $LogonRequest['Uri'] = "$Uri/auth/apphost" $LogonRequest['Body'] = $authJson.ToString() if ($null -ne $IISCredential ) { $LogonRequest['Credential'] = $IISCredential Write-Warning "Using IIS credentials" } if ($PSCmdlet.ShouldProcess($LogonRequest['Uri'], 'Logon')) { try { #Send Logon Request $OIMSession = Invoke-OIMRestMethod @LogonRequest } catch { #Throw all errors not related to ITATS542I throw $PSItem } finally { #If Logon Result If ($OIMSession) { #BaseURI set in Module Scope Set-Variable -Name BaseURI -Value $Uri -Scope Script Set-Variable -Name WebSession -Value $WebSession -Scope Script Set-Variable -Name IISCredential -Value $IISCredential -Scope Script #Initial Value for Version variable [System.Version]$Version = '0.0' if ( -not ($SkipVersionCheck)) { Try { #Get CyberArk ExternalVersion number. [System.Version]$Version = Get-OIMObject DialogDatabase -ErrorAction Stop | Select-Object -ExpandProperty EditionVersion } Catch { [System.Version]$Version = '0.0' } } #Version information available in module scope. Set-Variable -Name ExternalVersion -Value $Version -Scope Script } } } }#process END { }#end } |