Powershell/Private/Test-JumpCloudUsername.ps1
function Test-JumpCloudUsername { [CmdletBinding()] [OutputType([System.Boolean])] [OutputType([System.Object[]])] param ( [Parameter()] [System.String] $JumpCloudApiKey, [Parameter()] [System.String] $JumpCloudOrgID, [Parameter()] [System.String] $Username, [Parameter()] [System.Boolean] $prompt = $false ) Begin { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $Headers = @{ 'Accept' = 'application/json'; 'Content-Type' = 'application/json'; 'x-api-key' = $JumpCloudApiKey; 'x-org-id' = $JumpCloudOrgID; } $Form = @{ "filter" = @{ 'and' = @( @{'username' = @{'$regex' = "(?i)(`^$($Username)`$)" } } ) } "fields" = "username , systemUsername" } $Body = $Form | ConvertTo-Json -Depth 4 } Process { Try { # Write-ToLog "Searching JC for: $Username" $Response = Invoke-WebRequest -Method 'Post' -Uri "https://console.jumpcloud.com/api/search/systemusers" -Headers $Headers -Body $Body -UseBasicParsing $Results = $Response.Content | ConvertFrom-Json $StatusCode = $Response.StatusCode } catch { $StatusCode = $_.Exception.Response.StatusCode.value__ Write-ToLog -Message "Status Code $($StatusCode)" } } End { # Search User should return 200 success If ($StatusCode -ne 200) { Write-ToLog -Message "JumpCloud username could not be found" Return $false, $null, $null } If ($Results.totalCount -eq 1 -and $($Results.results[0].username) -eq $Username) { # write-host $Results.results[0]._id Write-ToLog -Message "Identified JumpCloud User`nUsername: $($Results.results[0].username)`nID: $($Results.results[0]._id)" if ($Results.results[0].SystemUsername) { Write-ToLog -Message "JumpCloud User have a Local Account User set: $($Results.results[0].SystemUsername)" return $true, $Results.results[0]._id, $Results.results[0].SystemUsername } else { return $true, $Results.results[0]._id, $null } } else { if ($prompt) { $message += "$Username is not a valid JumpCloud User`nPlease enter a valid JumpCloud Username" $wshell = New-Object -ComObject Wscript.Shell $var = $wshell.Popup("$message", 0, "ADMU Status", 0x0 + 0x40) } Return $false, $null, $null } } } |