Files
Tanium/API/RBAC_ListRole_Rest.ps1
2025-10-31 08:59:02 +01:00

107 lines
3.8 KiB
PowerShell

<#
.SYNOPSIS
List Tanium Roles via REST (uses /api/v2/content_set_roles), reading Url/Token from config.json.
.USAGE
.\List-Roles.ps1
.\List-Roles.ps1 -NameLike '^Ops' -Grid -ExportCsv C:\Temp\roles.csv
.\List-Roles.ps1 -Raw
#>
param(
[string]$Url,
[string]$Token,
[switch]$SkipCertCheck,
[string]$NameLike,
[switch]$Raw,
[switch]$Grid,
[string]$ExportCsv
)
$ErrorActionPreference = 'Stop'
try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } catch {}
function New-BaseUri([string]$HostLike) {
$h = $HostLike.Trim()
if ($h -match '^https?://') { $h = $h -replace '^https?://','' }
$h = $h.TrimEnd('/')
if ([string]::IsNullOrWhiteSpace($h)) { throw 'TaniumUrl empty after normalization.' }
"https://$h"
}
# TLS bypass for Windows PowerShell 5.1
$script:__oldCb = $null
function Enter-InsecureTls { param([switch]$Enable)
if (-not $Enable) { return }
$script:__oldCb = [System.Net.ServicePointManager]::ServerCertificateValidationCallback
$cb = [System.Net.Security.RemoteCertificateValidationCallback]{ param($s,$c,$ch,$e) $true }
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $cb
}
function Exit-InsecureTls {
if ($script:__oldCb -ne $null) {
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $script:__oldCb
$script:__oldCb = $null
}
}
# Load config if needed
$BaseDir = if ($PSScriptRoot) { $PSScriptRoot } else { $pwd.Path }
$configPath = Join-Path $BaseDir 'config.json'
if (-not $Url -or -not $Token) {
if (-not (Test-Path $configPath)) { throw "Missing -Url/-Token and config.json not found: $configPath" }
$cfg = Get-Content -Path $configPath -Raw | ConvertFrom-Json
if (-not $Url) { $Url = [string]$cfg.TaniumUrl }
if (-not $Token) { $Token = [string]$cfg.TaniumApiToken }
if (-not $PSBoundParameters.ContainsKey('SkipCertCheck')) { $SkipCertCheck = [bool]$cfg.SkipCertificateCheck }
}
$Base = New-BaseUri $Url
$Headers = @{ session = $Token; 'Content-Type' = 'application/json' }
function Invoke-Tanium([string]$Method,[string]$Path,[object]$BodyObj) {
$uri = if ($Path -match '^https?://') { $Path } else { "$Base$Path" }
$body = if ($null -ne $BodyObj) { $BodyObj | ConvertTo-Json -Depth 10 } else { $null }
$ps7 = ($PSVersionTable.PSVersion.Major -ge 7)
if ($ps7 -and $SkipCertCheck) {
return Invoke-RestMethod -SkipCertificateCheck -Method $Method -Uri $uri -Headers $Headers -Body $body
} else {
try { Enter-InsecureTls -Enable:$SkipCertCheck; return Invoke-RestMethod -Method $Method -Uri $uri -Headers $Headers -Body $body }
finally { Exit-InsecureTls }
}
}
# ---- Fetch roles (correct endpoint) ----
$resp = Invoke-Tanium GET "/api/v2/content_set_roles" $null
# Most Tanium APIs return .data here
$roles = if ($resp.data) { $resp.data } elseif ($resp.items) { $resp.items } else { $resp }
if ($Raw) { $roles | ConvertTo-Json -Depth 12; return }
if (-not $roles) { Write-Output "No roles returned."; return }
# ---- Shape output ----
$rows = foreach ($r in $roles) {
if ($NameLike -and -not ($r.name -match $NameLike)) { continue }
[pscustomobject]@{
Id = $r.id
Name = $r.name
Description = $r.description
}
}
if (-not $rows) { Write-Output "No matching roles."; return }
$rowsSorted = $rows | Sort-Object Name
if ($ExportCsv) {
try { $rowsSorted | Export-Csv -NoTypeInformation -Encoding UTF8 -Path $ExportCsv; Write-Host "Exported to: $ExportCsv" -ForegroundColor Green }
catch { Write-Warning "CSV export failed: $($_.Exception.Message)" }
}
if ($Grid) {
$title = "Roles — {0} item(s)" -f ($rowsSorted.Count)
if (Get-Command Out-GridView -ErrorAction SilentlyContinue) { $rowsSorted | Out-GridView -Title $title; return }
else { Write-Warning "Out-GridView not available. On PS7+: Install-Module Microsoft.PowerShell.GraphicalTools" }
}
$rowsSorted | Format-Table -AutoSize