147 lines
3.4 KiB
PowerShell
147 lines
3.4 KiB
PowerShell
# POLISH LANGUAGE
|
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
chcp 65001 | Out-Null
|
|
|
|
# LOAD CONFIG
|
|
$configPath = ".\config.json"
|
|
$config = Get-Content $configPath -Raw | ConvertFrom-Json
|
|
|
|
$apiUrl = $config."snipe-it".url.TrimEnd('/')
|
|
$apiKey = $config."snipe-it".apikey
|
|
$verifySSL = $config."snipe-it"."verify-ssl"
|
|
|
|
# SSL (optional)
|
|
if (-not $verifySSL) {
|
|
add-type @"
|
|
using System.Net;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
public class TrustAllCertsPolicy : ICertificatePolicy {
|
|
public bool CheckValidationResult(
|
|
ServicePoint srvPoint, X509Certificate certificate,
|
|
WebRequest request, int certificateProblem) {
|
|
return true;
|
|
}
|
|
}
|
|
"@
|
|
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
|
|
}
|
|
|
|
# HEADERS
|
|
$headers = @{
|
|
"Authorization" = "Bearer $apiKey"
|
|
"Accept" = "application/json"
|
|
"Content-Type" = "application/json"
|
|
}
|
|
|
|
# GET SEARCH VALUE
|
|
try {
|
|
$searchValue = Invoke-Expression $config."search-term".value
|
|
} catch {
|
|
Write-Host "Błąd pobierania search-term"
|
|
exit 1
|
|
}
|
|
|
|
if (-not $searchValue) {
|
|
Write-Host ""
|
|
Write-Host "Error getting search value" -ForegroundColor Red
|
|
Write-Host ""
|
|
exit 1
|
|
}
|
|
|
|
Write-Host ""
|
|
Write-Host "======== Getting Asset ========" -ForegroundColor Cyan
|
|
Write-Host ""
|
|
Write-Host "Search: $searchValue"
|
|
|
|
# FIND ASSET
|
|
$searchUrl = "$apiUrl/hardware?search=$searchValue"
|
|
|
|
try {
|
|
$response = Invoke-RestMethod -Uri $searchUrl -Headers $headers -Method GET
|
|
} catch {
|
|
Write-Host ""
|
|
Write-Host "Communication error with Snipe-IT API" -ForegroundColor Red
|
|
Write-Host ""
|
|
exit 1
|
|
}
|
|
|
|
if ($response.total -eq 0) {
|
|
Write-Host ""
|
|
Write-Host "Nie znaleziono assetu" -ForegroundColor Red
|
|
Write-Host ""
|
|
exit 0
|
|
}
|
|
|
|
$assetId = $response.rows[0].id
|
|
Write-Host "Asset ID: $assetId"
|
|
|
|
# COLLECT DATA
|
|
$customFields = @{}
|
|
|
|
foreach ($field in $config.fields.custom_fields.PSObject.Properties) {
|
|
|
|
$key = $field.Name
|
|
$fieldConfig = $field.Value
|
|
|
|
if (-not $fieldConfig.enabled) {
|
|
continue
|
|
}
|
|
|
|
try {
|
|
$value = Invoke-Expression $fieldConfig.ps_command
|
|
if ($value -is [int] -or $value -is [double]) {
|
|
$finalValue = $value
|
|
} else {
|
|
$finalValue = "$value"
|
|
}
|
|
$customFields[$key] = $finalValue
|
|
|
|
} catch {
|
|
Write-Host "Błąd pola: $key"
|
|
}
|
|
}
|
|
|
|
# PREVIEW
|
|
Write-Host ""
|
|
Write-Host "=== Data Preview ===" -ForegroundColor Cyan
|
|
Write-Host ""
|
|
Write-Host "Fields:" -ForegroundColor Yellow
|
|
|
|
foreach ($field in $config.fields.custom_fields.PSObject.Properties) {
|
|
|
|
$key = $field.Name
|
|
$fieldConfig = $field.Value
|
|
|
|
if (-not $fieldConfig.enabled) {
|
|
continue
|
|
}
|
|
|
|
if ($customFields.ContainsKey($key)) {
|
|
$name = if ($fieldConfig.name) { $fieldConfig.name } else { $key }
|
|
$value = $customFields[$key]
|
|
$namePadded = $name.PadRight(20)
|
|
Write-Host "- $namePadded : $value"
|
|
}
|
|
}
|
|
|
|
# BUILD BODY
|
|
$body = $customFields | ConvertTo-Json -Depth 5
|
|
|
|
# UPDATE ASSET
|
|
$updateUrl = "$apiUrl/hardware/$assetId"
|
|
|
|
Write-Host ""
|
|
Write-Host "======== Update Asset ========" -ForegroundColor Cyan
|
|
Write-Host ""
|
|
|
|
try {
|
|
Invoke-RestMethod -Uri $updateUrl -Headers $headers -Method PATCH -Body $body | Out-Null
|
|
Write-Host "Snipe-IT Update Completed" -ForegroundColor Green
|
|
|
|
} catch {
|
|
Write-Host "Błąd aktualizacji assetu ID: $assetId" -ForegroundColor Red
|
|
Write-Host $_.Exception.Message
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "" |