1

I am using PS 5 version and trying to create xml which should look as below:

<ConfigurationItemReport>
    <checkListItemCount>14</checkListItemCount>
    <checkListItems>
        <element>
            <checklistItemId>ID_1</checklistItemId>
            <checklistItemName>Name1</checklistItemName>
        </element>
        <element>
            <checklistItemId>ID_2</checklistItemId>
            <checklistItemName>Name2</checklistItemName>
        </element>
        ....  
    </checkListItems>
    <checkListName>CompCheck</checkListName>
    <serverIp>11.113.144.12</serverIp>
</ConfigurationItemReport>

I have written the following powershell 5.0 script so far which is not creating the xml

$allout = @()

$allout += New-Object -TypeName psobject -Property @{

    checklistItemName = 'Name1'
}

$output = [ordered]@{

    serverHostname = $serverhostname
    checkListItems = $allout
}

foreach ($check in $allout) {
    $check.checkListItemName  

    $hash = @{
        "Enclosure Model Type" = $check.checkListItemName  
    }
    $obj = New-Object -TypeName psobject -Property $hash
    Export-Clixml -Path E:\WindowsOAC\test.xml
}
11
  • Export-CliXml is going to tag it with all kinds of type information intended to be re-imported with Import-CliXml. Do you strictly want the output to be like your example? Commented May 14, 2018 at 16:30
  • the output can have more elements as well but elements name remain same Commented May 14, 2018 at 16:31
  • What is the intended use of the output xml? Commented May 14, 2018 at 16:31
  • it will be supplied as an input to another script written in different language which will parse the xml Commented May 14, 2018 at 16:32
  • 1
    I'd suggest using json: $object | ConvertTo-Json -Depth [int32]::MaxValue | Out-File -FilePath 'out.json' Commented May 14, 2018 at 16:38

1 Answer 1

2

Instead of utilizing xml, I'd suggest using json for language interop since your intention for this is serialization to be used from powershell in python. powershell has a built-in ConvertTo-Json cmdlet which will output your object as a string which can then be output to file. By default, it only goes two layers deep into your object, but that can be customized with the -Depth parameter (it accepts an int32 argument):

$MyObject | ConvertTo-Json -Depth 5 | Out-File -FilePath C:\myfile.json
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.