Get-GreenRadiusTokenAssignments.ps1
|
function Get-GreenRadiusTokenAssignments { <# .SYNOPSIS Gets token assignments from the GreenRADIUS server. .DESCRIPTION Utilizes the GreenRADIUS API to retrieve information on token assignments, filtered by the specified parameters. .OUTPUTS Information will be processed and passed to Write-Host. This function has no returns. .PARAMETER Username The username(s) of the user(s) to check authentication records against. Can accept one string, or an array of strings. This is in username@domain format. This does NOT necessarily use the UPN of the user, but rather the actual domain name as configured in GreenRADIUS, independent of the individual user. .PARAMETER Token The token ID(s) to filter against. Can accept one string, or an array of strings. .PARAMETER TokenType The token type(s) to filter against. Can accept one string, or an array of strings. Valid values are: "All", "YubiKey", "OATH", "U2F", and "Mobile" .PARAMETER AssignedBefore Timestamp, written in plain English. Entries from after this time will be ignored. The time provided will be interpreted as being Powershell's timezone, not the server's. Format examples are: February 5, 2025, 6:37:04 PM (it doesn't like "5th", "3rd", etc.) Feb 5, 2026, 15:03:46 2026-02-05 16:00 03/04/2026 .PARAMETER AssignedAfter Timestamp, written in plain English. Entries from before this time will be ignored. The time provided will be interpreted as being Powershell's timezone, not the server's. Format examples are: February 5, 2025, 6:37:04 PM (it doesn't like "5th", "3rd", etc.) Feb 5, 2026, 15:03:46 2026-02-05 16:00 03/04/2026 .PARAMETER ExportCSV Filepath to which to save a CSV of the records. .PARAMETER RawRequest Variable name, without the dollar sign, in which to store the raw request which was created and sent to the API. This does not need to be a pre-existing variable. Mostly used for debugging this function. This uses the Global scope. .PARAMETER RawResponse Variable name, without the dollar sign, in which to store the raw response from the API. This does not need to be a pre-existing variable. Mostly used for debugging this function. This uses the Global scope. .NOTES Version 2026.02.11.1451 #> <# LICENSE Copyright (c) 2026 Derek Howard and the City of Eureka, California Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, and/or distribute copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software, and the original author shall be credited as having created the original work. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #> [CmdletBinding()] param ( [string[]]$Username, [string[]]$Token, [ValidateSet("YubiKey", "OATH", "TOTP", "HOTP", "U2F", "Mobile")] [string[]]$TokenType, [string]$AssignedBefore, [string]$AssignedAfter, [string]$ExportCSV, [string]$RawRequest, [string]$RawResponse ) if (-not $global:GreenRadiusApiSession) { Write-Error "You forgot to run Connect-GreenRadiusApi!" return $false; } $Body = @{} if ($Username) { # "[array]" ensures even a single string is wrapped in an array $Body["username"] = @([array]$Username) } if ($Token) { $Body["token_id"] = @([array]$Token) } if ($TokenType) { $tokenTypeMap = [ordered]@{ 'YubiKey' = 'yubikey' 'OATH' = 'oath-totp','oath-hotp' 'TOTP' = 'oath-totp' 'HOTP' = 'oath-hotp' 'U2F' = 'u2f' 'Mobile' = 'm2f' } $tokenTypes = foreach ($key in $tokenTypeMap.Keys) { if ($TokenType -contains $key) { $tokenTypeMap[$key] } } $Body["token_type"] = @([array]$tokenTypes) } if ($AssignedBefore) { $Body["assigned_before"] = ([DateTimeOffset](Get-Date $AssignedBefore)).ToUnixTimeSeconds() } if ($AssignedAfter) { $Body["assigned_after"] = ([DateTimeOffset](Get-Date $AssignedAfter)).ToUnixTimeSeconds() } $Params = @{ Uri = "https://$($global:GreenRadiusApiSession.HostName)/gras-api/v2/mgmt/tokenassignment" Method = "Get" Credential = $global:GreenRadiusApiSession.Credential Body = ($Body | ConvertTo-Json -Depth 5) ContentType = "application/json" } if ($RawRequest) { Set-Variable -Name $RawRequest -Value $Body -Scope Global -Force } $apiResult = Invoke-RestMethod @Params if ($apiResult.GetType().Name -eq "String") { #This only seems necessary for the /tokenassignment api (used in Get-GreenRadiusTokenAssignments) #and even then only when no search parameters are specified, but here we go anyway $apiResult = $apiResult | ConvertFrom-Json -AsHashTable } if ($RawResponse) { Set-Variable -Name $RawResponse -Value $apiResult -Scope Global -Force } if ($apiResult.records_with_mappings.count -gt 0) { $processedRecords = [System.Collections.Generic.List[PSObject]]::new() if ($apiResult.records_with_mappings.records.Keys.Count) { $recordNames = $apiResult.records_with_mappings.records.Keys } elseif ($apiResult.records_with_mappings.records.psobject.Properties.Name) { $recordNames = $apiResult.records_with_mappings.records.psobject.Properties.Name } foreach ($recordName in $recordNames) { if ($apiResult.records_with_mappings.records.$recordName.user_mappings.Keys) { $mappingNames = $apiResult.records_with_mappings.records.$recordName.user_mappings.Keys } elseif ($apiResult.records_with_mappings.records.$recordName.user_mappings.psobject.Properties.Name) { $mappingNames = $apiResult.records_with_mappings.records.$recordName.user_mappings.psobject.Properties.Name } foreach ($mappingName in $mappingNames) { $mapping = $apiResult.records_with_mappings.records.$recordName.user_mappings.$mappingName $processedRecord = [PSCustomObject]@{ 'Username' = $mapping.user ? $mapping.user : $recordName 'Token ID' = $mapping.token_id ? $mapping.token_id : $recordName 'Token Type' = $mapping.token_type ? $mapping.token_type : $apiResult.records_with_mappings.records.$recordName.token_type 'User State' = $mapping.state_in_directory_server 'Assignment Status' = $mapping.status 'Assigned At' = [DateTimeOffset]::FromUnixTimeSeconds($mapping.assigned_on).LocalDateTime } $processedRecords.Add($processedRecord) } } if ($ExportCSV) { $processedRecords | Export-Csv -Path $ExportCSV -NoTypeInformation } else { $processedRecords | Format-Table -Property * } } else { Write-Host "No results found. Note that the API only returns information on tokens/users which are mapped to at least one token/user." } } |