Files
CyberbackupAPI/agents_ip.ps1
2026-09-03 03:13:57 +00:00

71 lines
2.4 KiB
PowerShell

$BASE_URL = "https://nameserver:9877"
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate cert, WebRequest req, int certProblem) { return true; }
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
$username = Read-Host "Enter username"
$password = Read-Host "Enter password" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
if (-not $username -or -not $plainPassword) {
Write-Host "No username or password provided" -ForegroundColor Red
exit 1
}
$authUrl = "$BASE_URL/idp/token"
$body = "username=$([uri]::EscapeDataString($username))&password=$([uri]::EscapeDataString($plainPassword))&grant_type=password"
try {
$authResponse = Invoke-RestMethod -Uri $authUrl -Method Post -Body $body -ContentType "application/x-www-form-urlencoded"
} catch {
Write-Host "Auth error: $_" -ForegroundColor Red
exit 1
}
if (-not $authResponse.token_type -or -not $authResponse.access_token) {
Write-Host "Error: no token in response" -ForegroundColor Red
exit 1
}
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "$($authResponse.token_type) $($authResponse.access_token)"
}
try {
Write-Host "Collecting agents..."
$agentsUrl = "$BASE_URL/api/resource_manager/v1/agents"
$response = Invoke-RestMethod -Uri $agentsUrl -Method Get -Headers $headers
} catch {
if ($_.Exception.Response.StatusCode.value__ -eq 401) {
Write-Host "Error 401: token invalid or expired." -ForegroundColor Red
} else {
Write-Host "Error: $_" -ForegroundColor Red
}
exit 1
}
$agents = $response.items | Where-Object { $_.type -eq "agent" } | ForEach-Object {
$status = if ($_.communication.online) { "Online" } else { "Offline" }
[PSCustomObject]@{
Name = $_.name
Type = $_.type
Status = $status
OS = $_.details.os.name
IP = ($_.details.ipAddresses -join ", ")
}
}
Write-Host ""
Write-Host "Total agents found: $($agents.Count)" -ForegroundColor Green
Write-Host ""
$agents | Format-Table -Property Name, Type, Status, OS, IP -AutoSize