$Port = 51528 $CsvFile = "C:\hw_info\devices.csv"
New-Item -ItemType Directory -Path "C:\hw_info" -Force | Out-Null
if (!(Test-Path $CsvFile)) { "client_id,hostname,user_name,location,os,kernel,cpu_model,cpu_cores,memory_total,ip_address,mac_address,timestamp" | Out-File -FilePath$CsvFile -Encoding UTF8
}
Write-Host "UDP server listening on port $Port..." -ForegroundColor Green
$localEP = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Any, [int]$Port)
$udp = New-Object System.Net.Sockets.UdpClient($localEP)
$remoteEP = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Any, 0)
while ($true) { $bytes = $udp.Receive([ref]$remoteEP)
if ($bytes.Length -eq 0) { continue }
$json = [System.Text.Encoding]::UTF8.GetString($bytes)
$obj = $json | ConvertFrom-Json
$client_id = $obj.client_id
$hostname = $obj.hostname
$user_name = $obj.user_name
$location = $obj.location
$os = $obj.os
$kernel = $obj.kernel
$cpu_model = $obj.cpu_model
$cpu_cores = $obj.cpu_cores
$memory_total = $obj.memory_total
$timestamp = $obj.timestamp
$ip_address = ""
$mac_address = ""
if ($obj.network -ne $null) {
foreach ($nic in $obj.network) {
if ($nic.name -ne "lo" -and $nic.ip -and $nic.ip -ne "") {
$ip_address = $nic.ip
$mac_address = $nic.mac
break
}
}
}
$csvLine = '"' + $client_id + '","' + $hostname + '","' + $user_name + '","' + $location + '","' + $os + '","' + $kernel + '","' + $cpu_model + '",' + $cpu_cores + ',' + $memory_total + ',"' + $ip_address + '","' + $mac_address + '","' + $timestamp + '"'
Add-Content -Path $CsvFile -Value $csvLine
Write-Host "Saved: $hostname ($user_name @ $location) IP: $ip_address MAC: $mac_address" -ForegroundColor Cyan
}