[CmdletBinding()] param( [string]$OutputPath, [ValidateSet('Json', 'Csv', 'Table')] [string]$Format = 'Table' ) $ErrorActionPreference = 'Stop' $os = Get-CimInstance Win32_OperatingSystem $computer = Get-CimInstance Win32_ComputerSystem $processors = @(Get-CimInstance Win32_Processor) $disks = @(Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3") $adapters = @() try { $adapters = @( Get-NetAdapter -ErrorAction Stop | Where-Object { $_.Status -eq 'Up' } | Select-Object Name, InterfaceDescription, LinkSpeed ) } catch { $adapters = @() } $data = [pscustomobject]@{ Hostname = $env:COMPUTERNAME Os = $os.Caption Version = $os.Version UptimeHours = [math]::Round(((Get-Date) - $os.LastBootUpTime).TotalHours, 1) Cpu = (($processors | ForEach-Object { $_.Name.Trim() }) -join '; ') CpuCores = ($processors | Measure-Object -Property NumberOfCores -Sum).Sum CpuLogical = ($processors | Measure-Object -Property NumberOfLogicalProcessors -Sum).Sum MemoryGB = [math]::Round($computer.TotalPhysicalMemory / 1GB, 1) Disks = @( $disks | ForEach-Object { [pscustomobject]@{ Drive = $_.DeviceID SizeGB = [math]::Round($_.Size / 1GB, 1) FreeGB = [math]::Round($_.FreeSpace / 1GB, 1) } } ) Network = @($adapters) } if ($OutputPath) { $full = [System.IO.Path]::GetFullPath($OutputPath) $parent = [System.IO.Path]::GetDirectoryName($full) if (-not (Test-Path -LiteralPath $parent -PathType Container)) { throw "Directory not found: $parent" } if ($Format -eq 'Csv') { $flat = [pscustomobject]@{ Hostname = $data.Hostname Os = $data.Os Version = $data.Version UptimeHours = $data.UptimeHours Cpu = $data.Cpu CpuCores = $data.CpuCores CpuLogical = $data.CpuLogical MemoryGB = $data.MemoryGB Disks = (($data.Disks | ForEach-Object { '{0} {1}GB free of {2}GB' -f $_.Drive, $_.FreeGB, $_.SizeGB }) -join '; ') Network = (($data.Network | ForEach-Object { '{0} {1}' -f $_.Name, $_.LinkSpeed }) -join '; ') } $flat | Export-Csv -LiteralPath $full -NoTypeInformation -Encoding UTF8 } else { $data | ConvertTo-Json -Depth 6 | Set-Content -LiteralPath $full -Encoding UTF8 } Write-Host "Inventory saved: $full" } if ($Format -eq 'Json') { $data | ConvertTo-Json -Depth 6 } elseif ($Format -eq 'Table') { $data | Format-List Hostname, Os, Version, UptimeHours, Cpu, CpuCores, CpuLogical, MemoryGB if ($data.Disks.Count -gt 0) { $data.Disks | Format-Table -AutoSize } if ($data.Network.Count -gt 0) { $data.Network | Format-Table -AutoSize } }