57 lines
2.8 KiB
PowerShell
57 lines
2.8 KiB
PowerShell
#requires -Version 5.1
|
|
[CmdletBinding()]
|
|
param([ValidateSet('all','system','roles','services','network','tasks','software','storage','shares','certificates')][string]$Section = 'all')
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$ProgressPreference = 'SilentlyContinue'
|
|
$WarningPreference = 'SilentlyContinue'
|
|
$InformationPreference = 'SilentlyContinue'
|
|
$env:PSModulePath = Join-Path $PSHOME 'Modules'
|
|
[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)
|
|
if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) {
|
|
[Console]::Error.WriteLine('This collector requires a local Windows Server execution host.')
|
|
exit 2
|
|
}
|
|
. (Join-Path $PSScriptRoot 'Inventory.Common.ps1')
|
|
foreach ($file in @('System','Roles','Services','Network','Tasks','Software','Storage','Certificates')) {
|
|
. (Join-Path $PSScriptRoot ('Get-Server' + $file + '.ps1'))
|
|
}
|
|
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
|
|
try {
|
|
$principal = New-Object Security.Principal.WindowsPrincipal($identity)
|
|
$script:SamsaraIsElevated = $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
|
|
} finally { $identity.Dispose() }
|
|
$collectors = [ordered]@{
|
|
system = { param($r) Get-SamsaraSystem $r }; roles = { param($r) Get-SamsaraRoles $r }
|
|
services = { param($r) Get-SamsaraServices $r }; network = { param($r) Get-SamsaraNetwork $r }
|
|
tasks = { param($r) Get-SamsaraTasks $r }; software = { param($r) Get-SamsaraSoftware $r }
|
|
storage = { param($r) Get-SamsaraStorage $r }; shares = { param($r) Get-SamsaraShares $r }
|
|
certificates = { param($r) Get-SamsaraCertificates $r }
|
|
}
|
|
if ($Section -ne 'all') {
|
|
$result = Invoke-SamsaraSection $Section $collectors[$Section]
|
|
ConvertTo-Json -InputObject $result -Depth 12 -Compress
|
|
exit 0
|
|
}
|
|
# Direct PowerShell use emits the same complete inventory envelope as the Python backend.
|
|
$inventory = [ordered]@{ roles=@(); services=@(); network=@(); tasks=@(); software=@(); storage=@(); certificates=@() }
|
|
$checks = [ordered]@{}
|
|
$collectedAt = Get-SamsaraTimestamp
|
|
$server = $null
|
|
foreach ($name in $collectors.Keys) {
|
|
$result = Invoke-SamsaraSection $name $collectors[$name]
|
|
$checks[$name] = ConvertTo-SamsaraCheck $result
|
|
if ($name -eq 'system') {
|
|
$server = $result.data
|
|
if ([int]$server.product_type -notin @(2,3) -or $server.os_version -notin @('2016','2019','2022','2025')) {
|
|
[Console]::Error.WriteLine('Unable to verify a supported Windows Server identity. No inventory emitted.')
|
|
exit 2
|
|
}
|
|
} elseif ($name -eq 'shares') { $inventory.storage += @($result.data) }
|
|
else { $inventory[$name] = @($result.data) }
|
|
}
|
|
ConvertTo-Json -InputObject ([ordered]@{ schema_version='1.0'; synthetic=$false; collected_at=$collectedAt
|
|
server=$server; checks=$checks; inventory=$inventory }) -Depth 14 -Compress
|
|
|
|
exit 0
|