diff --git a/agent.ps1 b/agent.ps1 index 7580e0f..5113894 100644 --- a/agent.ps1 +++ b/agent.ps1 @@ -1,5 +1,5 @@ param( - [string]$ServerUrl = "http://192.168.178.24:8080" + [string]$ServerUrl = "http://localhost:8080" ) $ComputerName = $env:COMPUTERNAME @@ -9,7 +9,7 @@ $MacAddress = (Get-NetAdapter | Where-Object { $_.Status -eq 'Up' } | Select-Obj $LastSeen = Get-Date -Format "yyyy-MM-dd HH:mm:ss" # Get installed software from registry (32 and 64 bit) -$Software = @() +$SoftwareList = New-Object System.Collections.Generic.List[PSCustomObject] $RegistryPaths = @( "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*", @@ -18,20 +18,15 @@ $RegistryPaths = @( foreach ($Path in $RegistryPaths) { Get-ItemProperty $Path -ErrorAction SilentlyContinue | Where-Object { $_.DisplayName -ne $null } | ForEach-Object { - $Software += @{ - Name = $_.DisplayName - Version = $_.DisplayVersion - } + $SoftwareList.Add([PSCustomObject]@{ + Name = [string]$_.DisplayName + Version = [string]$_.DisplayVersion + }) } } -# Unique list and sort -$Software = $Software | Sort-Object Name -Unique - -# Force Software to be an array even if it has only one element (for PS 5.1 compatibility) -if ($null -eq $Software) { - $Software = @() -} +# Unique list and sort (Now works correctly with PSCustomObject) +$Software = $SoftwareList | Sort-Object Name -Unique $Inventory = @{ ComputerName = $ComputerName @@ -39,28 +34,32 @@ $Inventory = @{ IPAddress = $IPAddress MacAddress = $MacAddress LastSeen = $LastSeen - Software = @($Software) # Ensure it's wrapped in an array + Software = $Software } -# In PowerShell 5.1, ConvertTo-Json still flattens single-item arrays in some cases. -# We can use a trick to ensure it stays an array or use PS7's -AsArray if available. +# Convert to JSON $Json = $Inventory | ConvertTo-Json -Depth 10 + +# FIX FOR POWERSHELL 5.1: Ensure Software is always an array +# If Software has only 1 item, ConvertTo-Json creates an object {} instead of an array [{}] if ($Software.Count -eq 1) { - # Manual fix for PS 5.1 flattening single-item arrays in JSON - # This is a bit hacky but effective for compatibility: - # If Software is a single object { ... }, replace it with [ { ... } ] - # We look for "Software": { and ensure it's "Software": [ { - $Json = $Json -replace '"Software":\s*\{', '"Software": [' - if ($Json -match '"Software":\s*\[') { - # If we replaced the start, we need to replace the end of that object - # This regex is simplified and assumes Software is at the end or followed by } - $Json = $Json -replace '(?ms)("Software":\s*\[.*?)\}(\s*\})', '$1}]$2' - } + # Find the Software block and wrap it in brackets if it's not already an array + # This regex is more robust: it looks for the Software key followed by an object start { + $Json = $Json -replace '("Software":\s*)\{', '$1[{' + # And then closes the bracket after that object + $Json = $Json -replace '(\s+)(\d+|".*?"|null|true|false)\s+\},(\s+"[A-Z])', '$1$2$1}],$3' + # If Software was the last element in the JSON + $Json = $Json -replace '(\s+)(\d+|".*?"|null|true|false)\s+\}\s+\}', '$1$2$1}] }' } try { - Invoke-RestMethod -Uri "$ServerUrl/inventory" -Method Post -Body $Json -ContentType "application/json" + $Response = Invoke-RestMethod -Uri "$ServerUrl/inventory" -Method Post -Body $Json -ContentType "application/json" Write-Host "Inventory sent successfully to $ServerUrl" } catch { Write-Error "Failed to send inventory: $_" + if ($_.Exception.Response) { + $Reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream()) + $ErrorBody = $Reader.ReadToEnd() + Write-Host "Error Body: $ErrorBody" -ForegroundColor Red + } } diff --git a/clients/TP101.json b/clients/TP101.json new file mode 100644 index 0000000..8ce88a6 --- /dev/null +++ b/clients/TP101.json @@ -0,0 +1,13 @@ +{ + "ComputerName": "TP101", + "User": "TP\\user11", + "IPAddress": "192.168.178.126", + "MacAddress": "BC-24-11-58-62-78", + "LastSeen": "2026-05-18 15:38:14", + "Software": [ + { + "Name": "Microsoft Edge WebView2-Laufzeit", + "Version": "148.0.3967.70" + } + ] +} \ No newline at end of file