120 lines
4.3 KiB
PowerShell
120 lines
4.3 KiB
PowerShell
<#
|
||
.SYNOPSIS
|
||
List Tanium **Content Sets** via the REST API, using Url/Token from `config.json` by default.
|
||
Supports optional filter by name (regex), Out‑GridView, and CSV export.
|
||
|
||
.USAGE
|
||
# Basic (reads config.json in the same folder)
|
||
./List-ContentSets.ps1
|
||
|
||
# Filter by name (regex), show grid and export CSV
|
||
./List-ContentSets.ps1 -NameLike '^Deploy' -Grid -ExportCsv C:\Temp\content_sets.csv
|
||
|
||
# Raw JSON
|
||
./List-ContentSets.ps1 -Raw
|
||
|
||
# Override Url/Token explicitly
|
||
./List-ContentSets.ps1 -Url tanium.pp.dktinfra.io -Token 'token-xxxx'
|
||
#>
|
||
|
||
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 {}
|
||
|
||
# ---------- Helpers ----------
|
||
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 Content Sets ----------
|
||
# Common endpoint for content sets
|
||
$resp = Invoke-Tanium GET "/api/v2/content_sets" $null
|
||
|
||
# Tanium REST often returns either .data or .items
|
||
$contentSets = if ($resp.data) { $resp.data } elseif ($resp.items) { $resp.items } else { $resp }
|
||
|
||
if ($Raw) { $contentSets | ConvertTo-Json -Depth 12; return }
|
||
if (-not $contentSets) { Write-Output 'No content sets returned.'; return }
|
||
|
||
# ---------- Shape output ----------
|
||
$rows = foreach ($cs in $contentSets) {
|
||
if ($NameLike -and -not ($cs.name -match $NameLike)) { continue }
|
||
[pscustomobject]@{
|
||
Id = $cs.id
|
||
Name = $cs.name
|
||
Description = $cs.description
|
||
}
|
||
}
|
||
|
||
if (-not $rows) { Write-Output 'No matching content sets.'; 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 = "Content Sets — {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 |