86 lines
4.0 KiB
PowerShell
86 lines
4.0 KiB
PowerShell
#region Save-RackpadManagedState
|
|
Function Save-RackpadManagedState
|
|
{
|
|
<#
|
|
.SYNOPSIS
|
|
Persists the managed object state document so that subsequent runs can prune objects that this solution created.
|
|
|
|
.DESCRIPTION
|
|
The document is written using UTF8 encoding without a byte order mark so that it remains portable across platforms.
|
|
|
|
.PARAMETER ManagedState
|
|
The state object produced by Get-RackpadManagedState.
|
|
|
|
.EXAMPLE
|
|
$Null = Save-RackpadManagedState -ManagedState $ManagedState
|
|
|
|
.NOTES
|
|
Author: Grace Solutions
|
|
Version: 2026.08.02.0000
|
|
#>
|
|
|
|
[CmdletBinding(SupportsShouldProcess=$True)]
|
|
Param
|
|
(
|
|
[Parameter(Mandatory=$True)]
|
|
[ValidateNotNullOrEmpty()]
|
|
[PSObject]$ManagedState
|
|
)
|
|
|
|
Try
|
|
{
|
|
$StateEntryList = New-Object -TypeName 'System.Collections.Generic.List[PSObject]'
|
|
|
|
ForEach ($EntityTypeEntry In $ManagedState.EntryTable.GetEnumerator())
|
|
{
|
|
ForEach ($NaturalKeyEntry In $EntityTypeEntry.Value.GetEnumerator())
|
|
{
|
|
$StateEntryProperties = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary'
|
|
$StateEntryProperties.entityType = "$($EntityTypeEntry.Key)"
|
|
$StateEntryProperties.naturalKey = "$($NaturalKeyEntry.Key)"
|
|
$StateEntryProperties.id = "$($NaturalKeyEntry.Value)"
|
|
|
|
$StateEntryList.Add((New-Object -TypeName 'System.Management.Automation.PSObject' -Property ($StateEntryProperties)))
|
|
}
|
|
}
|
|
|
|
$StateDocumentProperties = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary'
|
|
$StateDocumentProperties.instanceName = "$($ManagedState.InstanceName)"
|
|
$StateDocumentProperties.labId = "$($ManagedState.LabID)"
|
|
$StateDocumentProperties.updatedAt = [System.DateTime]::UtcNow.ToString('o')
|
|
$StateDocumentProperties.entryCount = $StateEntryList.Count
|
|
$StateDocumentProperties.entries = $StateEntryList.ToArray()
|
|
|
|
$StateDocumentObject = New-Object -TypeName 'System.Management.Automation.PSObject' -Property ($StateDocumentProperties)
|
|
|
|
Switch ($PSCmdlet.ShouldProcess("$($ManagedState.Path.FullName)", 'Save the managed object state document'))
|
|
{
|
|
{($_ -eq $True)}
|
|
{
|
|
Switch ([System.IO.Directory]::Exists($ManagedState.Path.Directory.FullName))
|
|
{
|
|
{($_ -eq $False)}
|
|
{
|
|
$Null = [System.IO.Directory]::CreateDirectory($ManagedState.Path.Directory.FullName)
|
|
}
|
|
}
|
|
|
|
$StateDocumentContent = $StateDocumentObject | ConvertTo-Json -Depth 10
|
|
|
|
$UTF8EncodingWithoutBOM = New-Object -TypeName 'System.Text.UTF8Encoding' -ArgumentList ($False)
|
|
|
|
$Null = [System.IO.File]::WriteAllText($ManagedState.Path.FullName, $StateDocumentContent, $UTF8EncodingWithoutBOM)
|
|
|
|
Write-Verbose -Message "[$([System.DateTime]::UtcNow.ToString('yyyy/MM/dd HH:mm:ss.FFF'))] - [INFO] - Saved $($StateEntryList.Count) managed object state entry/entries to `"$($ManagedState.Path.FullName)`"."
|
|
}
|
|
}
|
|
|
|
Write-Output -InputObject ($StateEntryList.Count)
|
|
}
|
|
Catch
|
|
{
|
|
Throw
|
|
}
|
|
}
|
|
#endregion
|