Get-GreenRadiusBlockStatus.ps1
|
function Get-GreenRadiusBlockStatus { <# .SYNOPSIS Checks user block status in GreenRADIUS. .DESCRIPTION Utilizes the GreenRADIUS API to retrieve user block statuses. .OUTPUTS If a single username was specified in the -Username argument, and the -Status parameter was NOT specified, then the function will return $true if the user is not blocked, and $false if they are blocked or don't exist. If multiple users OR any filtering was specified (either through passing !1 usernames through the -Username parameter, or by populating the -Regex argument, or by populating the -Status argument), then nothing will be returned. Instead, the function will print the results of each user. .PARAMETER Username The username(s) of the user(s) which we are checking. This parameter accepts an array of strings. If left blank, then the -Regex parameter will be used instead, if it was used. If -Regex is also not specified, then *all* users will be checked and printed. .PARAMETER Regex Regex pattern string to use to filter usernames. Oddly, GreenRADIUS does not consider anything after the @ symbol to be Regex. Here are some examples of both valid and invalid uses: - Regex "someuser.*" Valid. Matches any user with a username that begins with "someuser" across all domains in GreenRADIUS. - Regex "someuser.*@yourDomain.local" Valid. Matches any user with a username that begins with "someuser" but only within the domain "yourDomain.local" - Regex "someuser.*@yourDomain\.local" Invalid. The @ and everything after it are not considered to be Regex and shouldn't be escaped. - Regex "someuser@.*\.local" Invalid. The @ and everything after it are not considered to be Regex. This parameter cannot be used if -Username is also specified. This function originally passed both sets of information to GreenRADIUS, but the API seems to ignore the Regex if full usernames are also specified. .PARAMETER Status If specified, filters down the results to only users who are blocked or unblocked. Valid values for this parameter are "blocked" or "unblocked". .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(DefaultParameterSetName = 'Default')] param ( [Parameter(ParameterSetName = "Usernames")] [string[]]$Username, [Parameter(ParameterSetName = "Regex")] [string]$Regex, [ValidateSet("Blocked","Unblocked")] [string]$Status, [string]$RawRequest, [string]$RawResponse ) if (-not $global:GreenRadiusApiSession) { Write-Error "You forgot to run Connect-GreenRadiusApi!" return $false; } $singleUser = $true $Body = @{} if ($Username) { # [array]$Username ensures even a single string is wrapped in an array $Body["users"] = @([array]$Username) } if ($Username.Count -ne 1) { $singleUser = $false } if ($Regex) { $Body["pattern"] = $Regex $singleUser = $false } if ($Status) { $Body["state"] = @([array]$Status) $singleUser = $false } $Params = @{ Uri = "https://$($global:GreenRadiusApiSession.HostName)/gras-api/v2/mgmt/blocked-status" Method = "Get" #Body = ($Body | ConvertTo-Json -Depth 5) # this particular API endpoint doesn't like an empty Body Credential = $global:GreenRadiusApiSession.Credential ContentType = "application/json" } #This is separate because we don't want to include a Body if no search terms were specified. if ($Body.Count -gt 0) { $Params["Body"] = $Body | ConvertTo-Json -Depth 5 } 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 ($singleUser) { if ($apiResult.$Username.status -eq "record_not_found") { Write-Warning "User $Username not found in GreenRADIUS!" } return ($apiResult.$Username.status -eq "unblocked") } else { if ($apiResult.short -eq "RECORD_NOT_FOUND") { Write-Host "No matching records found." } else { foreach ($user in $apiResult.psobject.Properties) { Write-Host "$([string]$user.Name) : $([string]$user.Value.status)" } } } } |