update agents_ip.ps1

This commit is contained in:
smolkik_adm
2026-09-03 03:13:57 +00:00
parent dbe09db841
commit 8a62a60005

View File

@@ -1,6 +1,5 @@
$BASE_URL = "https://nameserver:9877" $BASE_URL = "https://nameserver:9877"
# Отключаем проверку SSL-сертификатов
add-type @" add-type @"
using System.Net; using System.Net;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
@@ -11,30 +10,28 @@ public class TrustAllCertsPolicy : ICertificatePolicy {
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
# Запрос логина и пароля $username = Read-Host "Enter username"
$username = Read-Host "Введите имя пользователя" $password = Read-Host "Enter password" -AsSecureString
$password = Read-Host "Введите пароль" -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password) $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)
$plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) $plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
if (-not $username -or -not $plainPassword) { if (-not $username -or -not $plainPassword) {
Write-Host "Не введён User или Password" -ForegroundColor Red Write-Host "No username or password provided" -ForegroundColor Red
exit 1 exit 1
} }
# Аутентификация
$authUrl = "$BASE_URL/idp/token" $authUrl = "$BASE_URL/idp/token"
$body = "username=$([uri]::EscapeDataString($username))&password=$([uri]::EscapeDataString($plainPassword))&grant_type=password" $body = "username=$([uri]::EscapeDataString($username))&password=$([uri]::EscapeDataString($plainPassword))&grant_type=password"
try { try {
$authResponse = Invoke-RestMethod -Uri $authUrl -Method Post -Body $body -ContentType "application/x-www-form-urlencoded" $authResponse = Invoke-RestMethod -Uri $authUrl -Method Post -Body $body -ContentType "application/x-www-form-urlencoded"
} catch { } catch {
Write-Host "Ошибка аутентификации: $_" -ForegroundColor Red Write-Host "Auth error: $_" -ForegroundColor Red
exit 1 exit 1
} }
if (-not $authResponse.token_type -or -not $authResponse.access_token) { if (-not $authResponse.token_type -or -not $authResponse.access_token) {
Write-Host "Ошибка: в ответе отсутствует токен" -ForegroundColor Red Write-Host "Error: no token in response" -ForegroundColor Red
exit 1 exit 1
} }
@@ -43,38 +40,32 @@ $headers = @{
"Authorization" = "$($authResponse.token_type) $($authResponse.access_token)" "Authorization" = "$($authResponse.token_type) $($authResponse.access_token)"
} }
# Получение списка агентов
try { try {
Write-Host "Сбор информации об агентах..." Write-Host "Collecting agents..."
$agentsUrl = "$BASE_URL/api/resource_manager/v1/agents" $agentsUrl = "$BASE_URL/api/resource_manager/v1/agents"
$response = Invoke-RestMethod -Uri $agentsUrl -Method Get -Headers $headers $response = Invoke-RestMethod -Uri $agentsUrl -Method Get -Headers $headers
} catch { } catch {
if ($_.Exception.Response.StatusCode.value__ -eq 401) { if ($_.Exception.Response.StatusCode.value__ -eq 401) {
Write-Host "Ошибка 401: токен недействителен или истёк." -ForegroundColor Red Write-Host "Error 401: token invalid or expired." -ForegroundColor Red
} else { } else {
Write-Host "Ошибка: $_" -ForegroundColor Red Write-Host "Error: $_" -ForegroundColor Red
} }
exit 1 exit 1
} }
$agents = $response.items | Where-Object { $_.type -eq "agent" } | ForEach-Object { $agents = $response.items | Where-Object { $_.type -eq "agent" } | ForEach-Object {
$status = if ($_.communication.online) { "Online" } else { "Offline" }
[PSCustomObject]@{ [PSCustomObject]@{
Name = $_.name Name = $_.name
Type = $_.type Type = $_.type
Online = $_.communication.online Status = $status
OS = $_.details.os.name OS = $_.details.os.name
IP = ($_.details.ipAddresses -join ", ") IP = ($_.details.ipAddresses -join ", ")
} }
} }
Write-Host "" Write-Host ""
Write-Host "Всего найдено агентов/устройств: $($agents.Count)" -ForegroundColor Green Write-Host "Total agents found: $($agents.Count)" -ForegroundColor Green
Write-Host "" Write-Host ""
$agents | Format-Table -Property @( $agents | Format-Table -Property Name, Type, Status, OS, IP -AutoSize
@{Label="Имя устройства"; Expression={$_.Name}; Width=40}
@{Label="Тип"; Expression={$_.Type}; Width=15}
@{Label="Статус"; Expression={if($_.Online){"Онлайн"}else{"Офлайн"}}; Width=8}
@{Label="ОС"; Expression={$_.OS}; Width=30}
@{Label="IP-адреса"; Expression={$_.IP}}
) -AutoSize