How can I serialize a complex Powershell array to disk for processing by a second Script?

I collect statistics about the Exchange environment and collect statistics about all the DNS domains used.

This is a long and intensive operation that can take a lot of time. I want to save these values ​​to disk, so I can start the postprocessor in the summary.

How can I change the script that I wrote below so that I can start the postprocessor?

$dict = @{} $AcceptedDomains = Get-AcceptedDomain | select domainname foreach($ad in $AcceptedDomains) { $tmp = $dict[$ad.DomainName] if ($tmp -eq $null) { $obj1 = @{CountPrimary = 0; CountWindows =0; CountSecondary = 0; IsAcceptedDomain = 0; DNS_MX = ""; DNS_SRV= ""; SSL=""} $dict.Add($ad.DomainName.ToString(), $obj1) } $dict[$ad.DomainName.ToString()].IsAcceptedDomain = 1 } foreach($currentmb in get-mailbox -resultsize unlimited|select PrimarySMTPAddress, WindowsEmailAddress, EmailAddresses) { $domName = $currentmb.PrimarySmtpAddress.ToString().Split("@")[1].ToString(); $tmp = $dict[ $domName] if ($tmp -eq $null) { $obj1 = @{CountPrimary = 0; CountWindows =0; CountSecondary = 0; IsAcceptedDomain = 0; DNS_MX = ""; DNS_SRV= ""; SSL=""} $dict.Add( $domName, $obj1) } $dict[$domName].CountPrimary+= 1 $domName = $currentmb.WindowsEmailAddress.ToString().Split("@")[1].ToString(); $tmp = $dict[ $domName] if ($tmp -eq $null) { $obj1 = @{CountPrimary = 0; CountWindows =0; CountSecondary = 0; IsAcceptedDomain = 0; DNS_MX = ""; DNS_SRV= ""; SSL=""} $dict.Add( $domName, $obj1) } $dict[$domName].CountWindows += 1 $secondaries = New-Object System.Collections.ArrayList($null) $currentMBSmtp = $currentMB.EmailAddresses | where {$_.PrefixString -eq "SMTP"} | select SMTPAddress foreach($smtp1 in $currentMBSmtp) { $smtp1Domain = $smtp1.SmtpAddress.ToString().Split("@")[1].ToString() $secondaries.Add($smtp1Domain) } foreach($domName1 in $secondaries | select -unique) { $tmp = $dict[ $domName1] if ($tmp -eq $null) { $obj1 = @{CountPrimary = 0; CountWindows =0; CountSecondary = 0; IsAcceptedDomain = 0; DNS_MX = ""; DNS_SRV= ""; SSL=""} $dict.Add( $domName1, $obj1) } $dict[$domName1].CountSecondary += 1 } } 

If you can tell from the placeholders, I intend to look at MX records, SSL certificates and other aspects of each DNS domain located.

The goal is to get enough information about the domains used so that each domain can be prioritized as needed.

The $dict array when sent to the screen looks like this: (domains are truncated to ensure privacy)

 oup.com {DNS_SRV, CountSecondary, CountWindows, IsAcceptedDomain, DNS_MX, SSL, CountPrimary} isorygroup... {DNS_SRV, CountSecondary, CountWindows, IsAcceptedDomain, DNS_MX, SSL, CountPrimary} ces.com {DNS_SRV, CountSecondary, CountWindows, IsAcceptedDomain, DNS_MX, SSL, CountPrimary} nc.net {DNS_SRV, CountSecondary, CountWindows, IsAcceptedDomain, DNS_MX, SSL, CountPrimary} 

An individual object is as follows:

 [PS] C:\scripts>$dict["sol.com"] Name Value ---- ----- DNS_SRV CountSecondary 3 CountWindows 0 IsAcceptedDomain 0 DNS_MX SSL CountPrimary 0 
+5
source share
1 answer

You can use Export-CliXml to serialize and write values ​​to disk. Import-CliXml to deserialize it and bring it back.

You may need to use the -Depth parameter if your objects are nested, I believe that by default it is equal to two levels. Increasing levels can significantly increase the time spent on serialization; especially if you have round links.

If you want to serialize / deserialize in memory, you can use [System.Management.Automation.PSSerializer]::Serialize() and [System.Management.Automation.PSSerializer]::Deserialize() (they are used internally by cmdlets, so this is the same representation).

+4
source

Source: https://habr.com/ru/post/1235420/


All Articles