ConvertFrom-Json2XmlBulk.psm1
Function Convert-PSObjectToXml { param ( [PSCustomObject]$object, [Int32]$depth = 1, [Int32]$indent = 1, [Boolean]$isTopLevel = $true ) $indentString = " " $xmlString = "" if ($isTopLevel) { $xmlString += "<Object>" + "`n" } foreach ($property in (Get-Member -InputObject $object -MemberType NoteProperty)) { $childObject = $object.($property.Name) if ($childObject.GetType().Name -eq "PSCustomObject" -and $depth -gt 1) { $xmlString += "$($indentString * $indent)<$($property.Name)>" + "`n" $xmlString += (Convert-PSObjectToXml $childObject -isRoot:$false -indent ($indent + 1) -depth ($depth + 1) -isTopLevel $false) $xmlString += "$($indentString * $indent)</$($property.Name)>" + "`n" } else { foreach ($element in $childObject) { $xmlString += "$($indentString * $indent)<$($property.Name)>$($element)</$($property.Name)>" + "`n" } } } if ($isTopLevel) { $xmlString += "</Object>" } return $xmlString } Function ConvertFrom-Json2XmlBulk([Parameter(Mandatory=$true)][string]$folderPath) { $sourceFolder = $folderPath $xmlFileLocation = "$folderPath\converted\" New-Item -Path $xmlFileLocation -ItemType Directory -Force -ErrorAction Stop if ((Test-Path $xmlFileLocation) -eq $false) { Write-Host "Could not create directory $xmlFileLocation - Try creating this manually and trying again." -ForegroundColor Red -BackgroundColor Black } else { Get-ChildItem -Path $sourceFolder -Filter *.json -Recurse -File | ForEach-Object { $newFileName = "$($xmlFileLocation)$($_.BaseName).xml" $json = Get-Content $_.FullName | ConvertFrom-Json $xml = Convert-PSObjectToXml $json -Depth 2048 New-Item -Path $xmlFileLocation -Name "$($_.BaseName).xml" -ItemType File -Force -ErrorAction Stop Set-Content $newFileName $xml } Write-Host "Conversion Completed. See directory $xmlFileLocation" -ForegroundColor Green } } New-Alias -Name Bulk_J2X -Value ConvertFrom-Json2XmlBulk |