pc-inventory/agent.ps1
2026-05-18 15:24:46 +02:00

48 lines
1.5 KiB
PowerShell

param(
[string]$ServerUrl = "http://192.168.178.24:8080"
)
$ComputerName = $env:COMPUTERNAME
$User = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$IPAddress = (Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias "Ethernet*", "Wi-Fi*" | Select-Object -First 1).IPAddress
$MacAddress = (Get-NetAdapter | Where-Object { $_.Status -eq 'Up' } | Select-Object -First 1).MacAddress
$LastSeen = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
# Get installed software from registry (32 and 64 bit)
$Software = @()
$RegistryPaths = @(
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
)
foreach ($Path in $RegistryPaths) {
Get-ItemProperty $Path -ErrorAction SilentlyContinue | Where-Object { $_.DisplayName -ne $null } | ForEach-Object {
$Software += @{
Name = $_.DisplayName
Version = $_.DisplayVersion
}
}
}
# Unique list and sort
$Software = $Software | Sort-Object Name -Unique
$Inventory = @{
ComputerName = $ComputerName
User = $User
IPAddress = $IPAddress
MacAddress = $MacAddress
LastSeen = $LastSeen
Software = $Software
}
$Json = $Inventory | ConvertTo-Json -Depth 10
try {
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: $_"
}