fix: inventory JSON without JavaScriptSerializer WARN (2.0.22-SAC)
Use ConvertTo-Json after CIM-safe Convert-AnyToJsonSerializable; pre-sanitize inventory details. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+6
-2
@@ -89,7 +89,7 @@ $script:SkipLogDetailLimit = 15
|
|||||||
# строки ниже, если правки «мелкие» и вы не хотите менять отображаемую версию в логах).
|
# строки ниже, если правки «мелкие» и вы не хотите менять отображаемую версию в логах).
|
||||||
# Рекомендация: при значимых релизах меняйте и $ScriptVersion, и version.txt одинаково; при только
|
# Рекомендация: при значимых релизах меняйте и $ScriptVersion, и version.txt одинаково; при только
|
||||||
# исправлениях на шаре — достаточно поднять patch в version.txt (например 1.3.0.1).
|
# исправлениях на шаре — достаточно поднять patch в version.txt (например 1.3.0.1).
|
||||||
$ScriptVersion = "2.0.21-SAC"
|
$ScriptVersion = "2.0.22-SAC"
|
||||||
|
|
||||||
# Логи (все под InstallRoot)
|
# Логи (все под InstallRoot)
|
||||||
$LogFile = Join-Path $script:InstallRoot "Logs\login_monitor.log"
|
$LogFile = Join-Path $script:InstallRoot "Logs\login_monitor.log"
|
||||||
@@ -2212,8 +2212,12 @@ function Send-HostInventoryToSac {
|
|||||||
$label = Get-MonitorServerLabelWithIp
|
$label = Get-MonitorServerLabelWithIp
|
||||||
$summary = "Inventory snapshot for $label"
|
$summary = "Inventory snapshot for $label"
|
||||||
$title = "Host inventory: $label"
|
$title = "Host inventory: $label"
|
||||||
|
$details = @{ inventory = $inv }
|
||||||
|
if (Get-Command Convert-AnyToJsonSerializable -ErrorAction SilentlyContinue) {
|
||||||
|
$details = Convert-AnyToJsonSerializable @{ inventory = $inv }
|
||||||
|
}
|
||||||
$ok = Send-SacEvent -EventType 'agent.inventory' -Severity 'info' -Title $title -Summary $summary `
|
$ok = Send-SacEvent -EventType 'agent.inventory' -Severity 'info' -Title $title -Summary $summary `
|
||||||
-Details @{ inventory = $inv }
|
-Details $details
|
||||||
if ($ok) {
|
if ($ok) {
|
||||||
Write-Log "SAC: agent.inventory отправлен (RAM=$($inv.memory_gb) GB, CPU=$($inv.processor.Count), disks=$($inv.disks.Count))."
|
Write-Log "SAC: agent.inventory отправлен (RAM=$($inv.memory_gb) GB, CPU=$($inv.processor.Count), disks=$($inv.disks.Count))."
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+20
-14
@@ -187,14 +187,31 @@ function Convert-AnyToJsonSerializable {
|
|||||||
if ($Value.GetType().IsPrimitive) {
|
if ($Value.GetType().IsPrimitive) {
|
||||||
return $Value
|
return $Value
|
||||||
}
|
}
|
||||||
if ($Value -is [datetime] -or $Value -is [datetimeoffset] -or $Value -is [guid]) {
|
if ($Value -is [enum]) {
|
||||||
|
return [string]$Value
|
||||||
|
}
|
||||||
|
if ($Value -is [decimal] -or $Value -is [single] -or $Value -is [double]) {
|
||||||
|
return [double]$Value
|
||||||
|
}
|
||||||
|
if ($Value -is [datetime] -or $Value -is [datetimeoffset] -or $Value -is [guid] -or $Value -is [version]) {
|
||||||
return [string]$Value
|
return [string]$Value
|
||||||
}
|
}
|
||||||
if ($Value -is [System.Management.Automation.PSMethod] -or
|
if ($Value -is [System.Management.Automation.PSMethod] -or
|
||||||
$Value -is [System.Management.Automation.ScriptBlock] -or
|
$Value -is [System.Management.Automation.ScriptBlock] -or
|
||||||
|
$Value -is [System.Management.Automation.PSMemberInfo] -or
|
||||||
$Value -is [Delegate]) {
|
$Value -is [Delegate]) {
|
||||||
return $null
|
return $null
|
||||||
}
|
}
|
||||||
|
$typeName = $Value.GetType().FullName
|
||||||
|
if ($typeName -like 'Microsoft.Management.Infrastructure.CimInstance*' -or
|
||||||
|
$typeName -like 'Microsoft.Management.Infrastructure.CimProperty*') {
|
||||||
|
$out = @{}
|
||||||
|
foreach ($prop in $Value.CimInstanceProperties) {
|
||||||
|
if ($null -eq $prop -or $null -eq $prop.Name) { continue }
|
||||||
|
$out[$prop.Name] = Convert-AnyToJsonSerializable $prop.Value
|
||||||
|
}
|
||||||
|
return $out
|
||||||
|
}
|
||||||
if ($Value -is [pscustomobject]) {
|
if ($Value -is [pscustomobject]) {
|
||||||
$out = @{}
|
$out = @{}
|
||||||
foreach ($prop in $Value.PSObject.Properties) {
|
foreach ($prop in $Value.PSObject.Properties) {
|
||||||
@@ -225,16 +242,7 @@ function Convert-AnyToJsonSerializable {
|
|||||||
function ConvertTo-SacJsonText {
|
function ConvertTo-SacJsonText {
|
||||||
param([Parameter(Mandatory = $true)]$Payload)
|
param([Parameter(Mandatory = $true)]$Payload)
|
||||||
$serializable = Convert-AnyToJsonSerializable $Payload
|
$serializable = Convert-AnyToJsonSerializable $Payload
|
||||||
try {
|
return ,($serializable | ConvertTo-Json -Depth 16 -Compress)
|
||||||
Add-Type -AssemblyName System.Web.Extensions -ErrorAction Stop
|
|
||||||
$ser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
|
|
||||||
$ser.MaxJsonLength = 16777216
|
|
||||||
$ser.RecursionLimit = 32
|
|
||||||
return ,$ser.Serialize($serializable)
|
|
||||||
} catch {
|
|
||||||
Write-SacLog "WARN: JavaScriptSerializer unavailable, fallback ConvertTo-Json ($($_.Exception.Message))"
|
|
||||||
return ,($serializable | ConvertTo-Json -Depth 12 -Compress)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function Get-SacCategoryForType {
|
function Get-SacCategoryForType {
|
||||||
@@ -499,9 +507,7 @@ function Get-SacPostBodyBytes {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$text = [System.Text.Encoding]::UTF8.GetString($bytes)
|
$text = [System.Text.Encoding]::UTF8.GetString($bytes)
|
||||||
Add-Type -AssemblyName System.Web.Extensions -ErrorAction Stop
|
$null = $text | ConvertFrom-Json -ErrorAction Stop
|
||||||
$ser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
|
|
||||||
$null = $ser.DeserializeObject($text)
|
|
||||||
} catch {
|
} catch {
|
||||||
Write-SacLog "WARN: SAC POST aborted: local JSON parse failed ($($_.Exception.Message))"
|
Write-SacLog "WARN: SAC POST aborted: local JSON parse failed ($($_.Exception.Message))"
|
||||||
return $null
|
return $null
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
2.0.21-SAC
|
2.0.22-SAC
|
||||||
|
|||||||
Reference in New Issue
Block a user