# ============================================ # ONE-LINE INSTALLER # Brug denne kommando for at køre scriptet direkte fra URL # ============================================ # WINDOWS COMMAND (Run as Administrator): # PowerShell -ExecutionPolicy Bypass -Command "Invoke-Expression (Invoke-WebRequest -Uri 'https://apigateway.bmcnetworks.dk/scripts/collect-inventory.ps1' -UseBasicParsing).Content" # ELLER kortere version: # iex (iwr 'https://apigateway.bmcnetworks.dk/scripts/collect-inventory.ps1' -UseBasicParsing).Content # ============================================ # Hardware Inventory Collection Script # Sender hardware info til API Gateway # ============================================ # Configuration $API_URL = "https://apigateway.bmcnetworks.dk/api/inventory" $API_KEY = "apigw_live_P8LXyeV5BDjmP0X-2YqOeEEcHAhe4xerTBklHVUuors" Write-Host "╔═══════════════════════════════════════════╗" -ForegroundColor Cyan Write-Host "║ Hardware Inventory Collection Script ║" -ForegroundColor Cyan Write-Host "╚═══════════════════════════════════════════╝" -ForegroundColor Cyan Write-Host "" # --- COLLECT HARDWARE INFORMATION --- Write-Host "→ Indsamler hardware information..." -ForegroundColor Yellow try { $computerSystem = Get-CimInstance Win32_ComputerSystem -ErrorAction Stop $os = Get-CimInstance Win32_OperatingSystem -ErrorAction Stop $bios = Get-CimInstance Win32_BIOS -ErrorAction Stop $cpu = Get-CimInstance Win32_Processor -ErrorAction Stop | Select-Object -First 1 $ram = Get-CimInstance Win32_PhysicalMemory -ErrorAction Stop $disks = Get-CimInstance Win32_DiskDrive -ErrorAction Stop $gpu = Get-CimInstance Win32_VideoController -ErrorAction Stop | Select-Object -First 1 $net = Get-CimInstance Win32_NetworkAdapterConfiguration -ErrorAction Stop | Where-Object { $_.IPEnabled } Write-Host "✓ Hardware information indsamlet" -ForegroundColor Green # Get WAN IP (public IP) Write-Host "→ Henter WAN IP adresse..." -ForegroundColor Yellow try { $wanIP = (Invoke-WebRequest -Uri "https://api.ipify.org?format=text" -TimeoutSec 5 -UseBasicParsing).Content.Trim() Write-Host "✓ WAN IP: $wanIP" -ForegroundColor Green } catch { $wanIP = $null Write-Host "⚠ Kunne ikke hente WAN IP" -ForegroundColor Yellow } # Get installed software Write-Host "→ Indsamler installeret software..." -ForegroundColor Yellow try { $software = @() # Registry paths for installed software $registryPaths = @( 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*', 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' ) foreach ($path in $registryPaths) { $software += Get-ItemProperty $path -ErrorAction SilentlyContinue | Where-Object { $_.DisplayName -and $_.DisplayName.Trim() -ne "" } | Select-Object @{N='name';E={$_.DisplayName}}, @{N='version';E={$_.DisplayVersion}}, @{N='publisher';E={$_.Publisher}}, @{N='install_date';E={$_.InstallDate}} } # Remove duplicates and sort $software = $software | Sort-Object -Property name -Unique | Select-Object -First 200 Write-Host "✓ Fundet $($software.Count) software pakker" -ForegroundColor Green } catch { $software = @() Write-Host "⚠ Kunne ikke indsamle software liste" -ForegroundColor Yellow } # Get Windows Defender status Write-Host "→ Tjekker Windows Defender status..." -ForegroundColor Yellow try { $defender = Get-MpComputerStatus -ErrorAction SilentlyContinue $defenderInfo = @{ enabled = $defender.AntivirusEnabled updated = if ($defender.AntivirusSignatureLastUpdated) { $defender.AntivirusSignatureLastUpdated.ToString("yyyy-MM-dd HH:mm:ss") } else { $null } real_time_protection = $defender.RealTimeProtectionEnabled } Write-Host "✓ Defender status hentet" -ForegroundColor Green } catch { $defenderInfo = @{ enabled = $null; updated = $null; real_time_protection = $null } Write-Host "⚠ Kunne ikke hente Defender status" -ForegroundColor Yellow } } catch { Write-Host "✗ Fejl ved indsamling af hardware data: $($_.Exception.Message)" -ForegroundColor Red exit 1 } # --- BUILD DATA OBJECT --- Write-Host "→ Opbygger data struktur..." -ForegroundColor Yellow $data = [ordered]@{ collected_at = (Get-Date).ToString("s") identity = @{ computer_name = $env:COMPUTERNAME domain = $computerSystem.Domain user = "$($computerSystem.UserName)" } hardware = @{ manufacturer = $computerSystem.Manufacturer model = $computerSystem.Model serial = $bios.SerialNumber bios_version = $bios.SMBIOSBIOSVersion bios_date = ([datetime]$bios.ReleaseDate).ToString("yyyy-MM-dd") cpu = @{ name = $cpu.Name cores = $cpu.NumberOfCores threads= $cpu.NumberOfLogicalProcessors } ram_gb = [math]::Round(($ram.Capacity | Measure-Object -Sum).Sum / 1GB, 2) disks = $disks | ForEach-Object { @{ model = $_.Model size_gb = [math]::Round($_.Size / 1GB, 2) interface = $_.InterfaceType } } gpu = $gpu.Name } os = @{ caption = $os.Caption version = $os.Version build = $os.BuildNumber install_date = ([datetime]$os.InstallDate).ToString("yyyy-MM-dd") last_boot = ([datetime]$os.LastBootUpTime).ToString("yyyy-MM-dd HH:mm:ss") uptime_hours = [math]::Round(((Get-Date) - [datetime]$os.LastBootUpTime).TotalHours, 2) } network = @{ wan_ip = $wanIP adapters = $net | ForEach-Object { @{ mac = $_.MACAddress ip = $_.IPAddress description = $_.Description dhcp_enabled = $_.DHCPEnabled } } } security = @{ windows_defender = $defenderInfo } software = $software } Write-Host "✓ Data struktur opbygget" -ForegroundColor Green Write-Host "" Write-Host "📊 Indsamlet data:" -ForegroundColor Cyan Write-Host " Computer: $($data.identity.computer_name)" -ForegroundColor White Write-Host " Domæne: $($data.identity.domain)" -ForegroundColor White Write-Host " Model: $($data.hardware.manufacturer) $($data.hardware.model)" -ForegroundColor White Write-Host " CPU: $($data.hardware.cpu.name)" -ForegroundColor White Write-Host " RAM: $($data.hardware.ram_gb) GB" -ForegroundColor White Write-Host " OS: $($data.os.caption)" -ForegroundColor White Write-Host " WAN IP: $($data.network.wan_ip)" -ForegroundColor White Write-Host " Software: $($software.Count) pakker" -ForegroundColor White Write-Host " Defender: $($defenderInfo.enabled)" -ForegroundColor White Write-Host "" # --- CONVERT TO JSON --- $json = $data | ConvertTo-Json -Depth 6 # Optional: Save local copy for debugging $localBackup = "$env:TEMP\inventory_$(Get-Date -Format 'yyyyMMdd_HHmmss').json" $json | Out-File $localBackup -Encoding UTF8 Write-Host "💾 Lokal kopi gemt: $localBackup" -ForegroundColor Gray Write-Host "" # --- SEND TO API GATEWAY --- Write-Host "→ Sender data til API Gateway..." -ForegroundColor Yellow $headers = @{ "Authorization" = "Bearer $API_KEY" "Content-Type" = "application/json" } try { $response = Invoke-RestMethod -Method POST -Uri $API_URL -Body $json -Headers $headers -ErrorAction Stop Write-Host "" Write-Host "╔═══════════════════════════════════════════╗" -ForegroundColor Green Write-Host "║ ✓ SUCCESS! ║" -ForegroundColor Green Write-Host "╚═══════════════════════════════════════════╝" -ForegroundColor Green Write-Host "" Write-Host "✓ Inventory data sendt succesfuldt" -ForegroundColor Green Write-Host "" Write-Host "📝 Server respons:" -ForegroundColor Cyan Write-Host " Computer: $($response.computer_name)" -ForegroundColor White Write-Host " Serial: $($response.serial_number)" -ForegroundColor White Write-Host " Inventory ID: $($response.inventory_id)" -ForegroundColor White Write-Host " Tidspunkt: $($response.collected_at)" -ForegroundColor White Write-Host "" # Clean up local backup on success Remove-Item $localBackup -ErrorAction SilentlyContinue exit 0 } catch { Write-Host "" Write-Host "╔═══════════════════════════════════════════╗" -ForegroundColor Red Write-Host "║ ✗ FEJL! ║" -ForegroundColor Red Write-Host "╚═══════════════════════════════════════════╝" -ForegroundColor Red Write-Host "" Write-Host "✗ Kunne ikke sende data til API Gateway" -ForegroundColor Red Write-Host "" Write-Host "Fejlbesked:" -ForegroundColor Yellow Write-Host $_.Exception.Message -ForegroundColor Red Write-Host "" if ($_.Exception.Response) { $statusCode = $_.Exception.Response.StatusCode.value__ Write-Host "HTTP Status Code: $statusCode" -ForegroundColor Yellow if ($statusCode -eq 401) { Write-Host "⚠ Authentication fejlede - tjek API key er korrekt" -ForegroundColor Yellow } elseif ($statusCode -eq 403) { Write-Host "⚠ Adgang nægtet - mangler rettigheder" -ForegroundColor Yellow } elseif ($statusCode -eq 404) { Write-Host "⚠ Endpoint ikke fundet - er /api/inventory deployed?" -ForegroundColor Yellow } } Write-Host "" Write-Host "💾 Data gemt lokalt til manuel upload:" -ForegroundColor Cyan Write-Host " $localBackup" -ForegroundColor White Write-Host "" exit 1 }