Guest User

Untitled

a guest
Oct 15th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. $a = New-Object PSObject -Property @{
  2. Name='New'
  3. Server = $null
  4. Database = $null
  5. UserName = $null
  6. Password = $null
  7. }
  8.  
  9. $b = $a | Convertto-XML -NoTypeInformation
  10.  
  11. <?xml version="1.0"?>
  12. <Objects>
  13. <Object>
  14. <Property Name="Password" />
  15. <Property Name="Name">New</Property>
  16. <Property Name="Server" />
  17. <Property Name="UserName" />
  18. <Property Name="Database" />
  19. </Object>
  20. </Objects>
  21.  
  22. filter XmlProperty([String]$Property) {
  23. $_.SelectSingleNode("/Objects/Object/Property[@Name='$Property']").InnerText
  24. }
  25.  
  26. $Name = $b | Xmlproperty Name
  27. $Server = $b | XmlProperty Server
  28. # etc...
  29.  
  30. function ConvertFrom-Xml($XML) {
  31. foreach ($Object in @($XML.Objects.Object)) {
  32. $PSObject = New-Object PSObject
  33. foreach ($Property in @($Object.Property)) {
  34. $PSObject | Add-Member NoteProperty $Property.Name $Property.InnerText
  35. }
  36. $PSObject
  37. }
  38. }
  39.  
  40. ConvertFrom-Xml $b
  41.  
  42. function ConvertFrom-Xml {
  43. <#
  44. .SYNOPSIS
  45. Converts XML object to PSObject representation for further ConvertTo-Json transformation
  46. .EXAMPLE
  47. # JSON->XML
  48. $xml = ConvertTo-Xml (get-content 1.json | ConvertFrom-Json) -Depth 4 -NoTypeInformation -as String
  49. .EXAMPLE
  50. # XML->JSON
  51. ConvertFrom-Xml ([xml]($xml)).Objects.Object | ConvertTo-Json
  52. #>
  53. param([System.Xml.XmlElement]$Object)
  54.  
  55. if (($Object -ne $null) -and ($Object.Property -ne $null)) {
  56. $PSObject = New-Object PSObject
  57.  
  58. foreach ($Property in @($Object.Property)) {
  59. if ($Property.Property.Name -like 'Property') {
  60. $PSObject | Add-Member NoteProperty $Property.Name ($Property.Property | % {ConvertFrom-Xml $_})
  61. } else {
  62. if ($Property.'#text' -ne $null) {
  63. $PSObject | Add-Member NoteProperty $Property.Name $Property.'#text'
  64. } else {
  65. if ($Property.Name -ne $null) {
  66. $PSObject | Add-Member NoteProperty $Property.Name (ConvertFrom-Xml $Property)
  67. }
  68. }
  69. }
  70. }
  71. $PSObject
  72. }
  73. }
  74.  
  75. function xmlNodeToPsCustomObject ($node){
  76. $hash = @{}
  77. foreach($attribute in $node.attributes){
  78. $hash.$($attribute.name) = $attribute.Value
  79. }
  80. $childNodesList = $node.ChildNodes.LocalName
  81. foreach($childnode in $node.childnodes){
  82. if(!($childnode -eq $null)){
  83. if(($childNodesList | ?{$_ -eq $childnode.LocalName}) -gt 1){
  84. if(!($hash.$($childnode.LocalName))){
  85. $hash.$($childnode.LocalName) += @()
  86. }
  87. if ($childnode.'#text' -ne $null) {
  88. $hash.$($childnode.LocalName) += $childnode.'#text'
  89. }else{
  90. $hash.$($childnode.LocalName) += xmlNodeToPsCustomObject($childnode)
  91. }
  92. }else{
  93. if ($childnode.'#text' -ne $null) {
  94. $hash.$($childnode.LocalName) = $childnode.'#text'
  95. }else{
  96. $hash.$($childnode.LocalName) = xmlNodeToPsCustomObject($childnode)
  97. }
  98. }
  99. }
  100. }
  101. return $hash | ConvertTo-PsCustomObjectFromHashtable
  102. }
  103.  
  104. function ConvertTo-PsCustomObjectFromHashtable {
  105. param (
  106. [Parameter(
  107. Position = 0,
  108. Mandatory = $true,
  109. ValueFromPipeline = $true,
  110. ValueFromPipelineByPropertyName = $true
  111. )] [object[]]$hashtable
  112. );
  113.  
  114. begin { $i = 0; }
  115.  
  116. process {
  117. foreach ($myHashtable in $hashtable) {
  118. if ($myHashtable.GetType().Name -eq 'hashtable') {
  119. $output = New-Object -TypeName PsObject;
  120. Add-Member -InputObject $output -MemberType ScriptMethod -Name AddNote -Value {
  121. Add-Member -InputObject $this -MemberType NoteProperty -Name $args[0] -Value $args[1];
  122. };
  123. $myHashtable.Keys | Sort-Object | % {
  124. $output.AddNote($_, $myHashtable.$_);
  125. }
  126. $output
  127. } else {
  128. Write-Warning "Index $i is not of type [hashtable]";
  129. }
  130. $i += 1;
  131. }
  132. }
  133. }
Add Comment
Please, Sign In to add comment