Search code examples
powershellexchange-server

Backing up of the "Mail flow - Rules" in the "Exchange admin center"


I need to take a backup of the "Mail flow - Rules" in the "Exchange admin center"

$TransCollect = Export-TransportRuleCollection
$TransCollect1 = [System.Text.Encoding]::Unicode.GetString($TransCollect.FileData)
$TransCollect1  | Set-Content -path c:\temp\2.xml

But I cannot extract anything from the XML file because at the start of the XML file is a special character. So If run .... [XML]$AppConfig = Get-Content –Path "c:\temp\2.xml" I get several errors. Is there is a problem in the "[System.Text.Encoding]::Unicode.GetString" line itself OR how do I remove this special character. See the screenshot for the special character. It shows up at the beginning of the file" enter image description here


Solution

  • I was able to use this and bypass the special character entirely as well as getting the EXCHANGE RULES into a extractable format.

    $File_Collect = "c:\temp\1.xml"
    $TransCollect = Export-TransportRuleCollection
    Set-Content -Path $File_Collect -Value $TransCollect.FileData -Encoding Byte
    [XML]$TransXMLCollect = Get-Content –Path $File_Collect
    $TransXMLCollect.SelectNodes("//rule") | % { $_.InnerText }