|
| 1 | +param ( |
| 2 | + [string]$PVConfiguration, |
| 3 | + [string]$Output = '.' |
| 4 | +) |
| 5 | + |
| 6 | +function Convert-ConnectionComponents { |
| 7 | + [CmdletBinding()] |
| 8 | + param ( |
| 9 | + [Parameter(ValueFromRemainingArguments, DontShow)] |
| 10 | + $CatchAll, |
| 11 | + [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)] |
| 12 | + [string]$PVConfiguration, |
| 13 | + [Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)] |
| 14 | + [string]$Output = '.' |
| 15 | + ) |
| 16 | + begin { |
| 17 | + $PSBoundParameters.Remove("CatchAll") | Out-Null |
| 18 | + if (Test-Path "$Output\Exported.log") { |
| 19 | + Remove-Item -Force "$Output\Exported.log" |
| 20 | + } |
| 21 | + function Format-Xml { |
| 22 | + #.Synopsis |
| 23 | + # Pretty-print formatted XML source |
| 24 | + #.Description |
| 25 | + # Runs an XmlDocument through an auto-indenting XmlWriter |
| 26 | + #.Example |
| 27 | + # [xml]$xml = get-content Data.xml |
| 28 | + # C:\PS>Format-Xml $xml |
| 29 | + #.Example |
| 30 | + # get-content Data.xml | Format-Xml |
| 31 | + #.Example |
| 32 | + # Format-Xml C:\PS\Data.xml -indent 1 -char `t |
| 33 | + # Shows how to convert the indentation to tabs (which can save bytes dramatically, while preserving readability) |
| 34 | + #.Example |
| 35 | + # ls *.xml | Format-Xml |
| 36 | + # |
| 37 | + [CmdletBinding()] |
| 38 | + param( |
| 39 | + # The Xml Document |
| 40 | + [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Document')] |
| 41 | + $Xml, |
| 42 | + |
| 43 | + # The path to an xml document (on disc or any other content provider). |
| 44 | + [Parameter(Position = 0, Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'File')] |
| 45 | + [Alias('PsPath')] |
| 46 | + [string]$Path, |
| 47 | + |
| 48 | + # The indent level (defaults to 2 spaces) |
| 49 | + [Parameter(Mandatory = $false)] |
| 50 | + [int]$Indent = 2, |
| 51 | + |
| 52 | + # The indent character (defaults to a space) |
| 53 | + [char]$Character = ' ' |
| 54 | + ) |
| 55 | + process { |
| 56 | + ## Load from file, if necessary |
| 57 | + if ($Path) { [xml]$xml = Get-Content $Path } |
| 58 | + |
| 59 | + $StringWriter = New-Object System.IO.StringWriter |
| 60 | + $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter |
| 61 | + $xmlWriter.Formatting = 'indented' |
| 62 | + $xmlWriter.Indentation = $Indent |
| 63 | + $xmlWriter.IndentChar = $Character |
| 64 | + $xml.WriteContentTo($XmlWriter) |
| 65 | + $XmlWriter.Flush() |
| 66 | + $StringWriter.Flush() |
| 67 | + Write-Output $StringWriter.ToString() |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + process { |
| 73 | + [xml]$xmlData = Get-Content -Path $PVConfiguration |
| 74 | + $components = $xmlData.PasswordVaultConfiguration.ConnectionComponents.ConnectionComponent |
| 75 | + $components | ForEach-Object { |
| 76 | + [xml]$Work = $PSItem.OuterXml |
| 77 | + ".\CC-$($Work.ConnectionComponent.Id).zip" | Out-File "$Output\Exported.log" -Append |
| 78 | + Format-XML $Work | Out-File "$Output\CC-$($Work.ConnectionComponent.Id).xml" -Force |
| 79 | + Compress-Archive -Path "$Output\CC-$($work.ConnectionComponent.Id).xml" -DestinationPath "$Output\CC-$($Work.ConnectionComponent.Id).zip" -Force |
| 80 | + Remove-Item "$Output\CC-$($Work.ConnectionComponent.Id).xml" -Force |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +If (-not [string]::IsNullOrEmpty($PVConfiguration)) { |
| 86 | + Convert-ConnectionComponents @PSBoundParameters |
| 87 | +} |
| 88 | + |
0 commit comments