From 1a7913cac45ac8bf601db619f043c97f424bb7e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20=C5=BBbikowski?= Date: Thu, 2 Apr 2026 14:05:29 +0200 Subject: [PATCH] Add assets.ps1 --- assets.ps1 | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 assets.ps1 diff --git a/assets.ps1 b/assets.ps1 new file mode 100644 index 0000000..bf7a42c --- /dev/null +++ b/assets.ps1 @@ -0,0 +1,147 @@ +# 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 "" \ No newline at end of file