This commit is contained in:
Thomas Peterson 2026-05-18 15:56:44 +02:00
parent 195d159fae
commit d9463e7975
2 changed files with 39 additions and 27 deletions

View File

@ -1,5 +1,5 @@
param( param(
[string]$ServerUrl = "http://192.168.178.24:8080" [string]$ServerUrl = "http://localhost:8080"
) )
$ComputerName = $env:COMPUTERNAME $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" $LastSeen = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
# Get installed software from registry (32 and 64 bit) # Get installed software from registry (32 and 64 bit)
$Software = @() $SoftwareList = New-Object System.Collections.Generic.List[PSCustomObject]
$RegistryPaths = @( $RegistryPaths = @(
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*", "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*",
@ -18,20 +18,15 @@ $RegistryPaths = @(
foreach ($Path in $RegistryPaths) { foreach ($Path in $RegistryPaths) {
Get-ItemProperty $Path -ErrorAction SilentlyContinue | Where-Object { $_.DisplayName -ne $null } | ForEach-Object { Get-ItemProperty $Path -ErrorAction SilentlyContinue | Where-Object { $_.DisplayName -ne $null } | ForEach-Object {
$Software += @{ $SoftwareList.Add([PSCustomObject]@{
Name = $_.DisplayName Name = [string]$_.DisplayName
Version = $_.DisplayVersion Version = [string]$_.DisplayVersion
} })
} }
} }
# Unique list and sort # Unique list and sort (Now works correctly with PSCustomObject)
$Software = $Software | Sort-Object Name -Unique $Software = $SoftwareList | 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 = @()
}
$Inventory = @{ $Inventory = @{
ComputerName = $ComputerName ComputerName = $ComputerName
@ -39,28 +34,32 @@ $Inventory = @{
IPAddress = $IPAddress IPAddress = $IPAddress
MacAddress = $MacAddress MacAddress = $MacAddress
LastSeen = $LastSeen 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. # Convert to JSON
# We can use a trick to ensure it stays an array or use PS7's -AsArray if available.
$Json = $Inventory | ConvertTo-Json -Depth 10 $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) { if ($Software.Count -eq 1) {
# Manual fix for PS 5.1 flattening single-item arrays in JSON # Find the Software block and wrap it in brackets if it's not already an array
# This is a bit hacky but effective for compatibility: # This regex is more robust: it looks for the Software key followed by an object start {
# If Software is a single object { ... }, replace it with [ { ... } ] $Json = $Json -replace '("Software":\s*)\{', '$1[{'
# We look for "Software": { and ensure it's "Software": [ { # And then closes the bracket after that object
$Json = $Json -replace '"Software":\s*\{', '"Software": [' $Json = $Json -replace '(\s+)(\d+|".*?"|null|true|false)\s+\},(\s+"[A-Z])', '$1$2$1}],$3'
if ($Json -match '"Software":\s*\[') { # If Software was the last element in the JSON
# If we replaced the start, we need to replace the end of that object $Json = $Json -replace '(\s+)(\d+|".*?"|null|true|false)\s+\}\s+\}', '$1$2$1}] }'
# This regex is simplified and assumes Software is at the end or followed by }
$Json = $Json -replace '(?ms)("Software":\s*\[.*?)\}(\s*\})', '$1}]$2'
}
} }
try { 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" Write-Host "Inventory sent successfully to $ServerUrl"
} catch { } catch {
Write-Error "Failed to send inventory: $_" 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
}
} }

13
clients/TP101.json Normal file
View File

@ -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"
}
]
}