Skip to content

Instantly share code, notes, and snippets.

@kevinblumenfeld
Created January 22, 2021 20:25
Show Gist options
  • Save kevinblumenfeld/0faca8cddce2e60abf9e1e867e8a22fc to your computer and use it in GitHub Desktop.
Save kevinblumenfeld/0faca8cddce2e60abf9e1e867e8a22fc to your computer and use it in GitHub Desktop.
function Connect-OktaSecure {
param (
[Parameter(Mandatory)]
[String] $Tenant,
[Parameter()]
[switch] $DeleteCreds,
[Parameter()]
[switch] $Preview
)
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
if (-not (Get-Module -ListAvailable Okta.Core.Automation)) {
Install-Module Okta.Core.Automation -Force -SkipPublisherCheck -Scope CurrentUser
}
$host.ui.RawUI.WindowTitle = "OKTA Tenant: $($Tenant.ToUpper())"
$RootPath = $env:USERPROFILE + "\ps\"
$KeyPath = $Rootpath + "creds\"
if ($DeleteCreds) {
Remove-Item ($KeyPath + "$($Tenant).OktaXml")
break
}
# Create KeyPath Directory
if (-not (Test-Path $KeyPath)) {
Try {
$null = New-Item -ItemType Directory -Path $KeyPath -ErrorAction STOP
}
Catch {
throw $_.Exception.Message
}
}
if (Test-Path ($KeyPath + "$($Tenant).OktaXml")) {
[System.Management.Automation.PSCredential]$Script:OKTACredential = Import-Clixml ($KeyPath + "$($Tenant).OktaXml")
$url = $OKTACredential.GetNetworkCredential().username
$token = $OKTACredential.GetNetworkCredential().Password
}
else {
[System.Management.Automation.PSCredential]$Script:OKTACredential = Get-Credential -Message "If Okta tenant is contoso.okta.com use CONTOSO as Username and API Token as Password"
$OKTACredential | Export-Clixml ($KeyPath + "$($Tenant).OktaXml")
$url = $OKTACredential.GetNetworkCredential().username
$Script:token = $OKTACredential.GetNetworkCredential().Password
}
if ($Preview) {
$Script:domain = "https://$url.oktapreview.com"
}
else {
$Script:domain = "https://$url.okta.com"
}
Connect-Okta -Token $token -FullDomain $domain
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment