Avatar of Kasper Katzmann
Kasper Katzmann
Flag for Denmark asked on

Customizing XML with Powershell

I need to generate an XML file with user data from AD. The customer needs to get the data in a format where the object names are converted from their original names (i.e. Description is coverted to Comment).

This I accomplish with this script
$me    = Get-ADUser `
            -identity b012345 `
            -Properties `
                description, `
                SID,  `
                description,  `
                Name,  `
                samAccountName,  `
                ObjectClass,  `
                Enabled,  `
                PasswordNotRequired,  `
                CannotChangePassword,  `
                PasswordNeverExpires,  `
                LockedOut |
            Select `
                @{Name='Name';Expression={$_.samAccountName}}, 
                @{Name='Full_name';Expression={$_.Name}}, 
                @{Name='Comment';Expression={$_.description}}, 
                @{Name='Id';Expression={$_.SID}}, 
                @{Name='AccountType';Expression={"Normal"}}, 
                @{Name='AccountPropsEnabled';Expression={$_.PasswordNotRequired}}, 
                @{Name='AccountPropsPasswdRequired';Expression={$_.Enabled}}, 
                @{Name='AccountPropsPasswdCantChange';Expression={$_.CannotChangePassword}}, 
                @{Name='AccountPropsPasswdDontExpire';Expression={$_.PasswordNeverExpires}}, 
                @{Name='AccountPropsCurrentlyLockedOut';Expression={$_.LockedOut}}

$meXML = $me | ConvertTo-Xml
$meXML.save("c:\temp\me.xml")

Open in new window


Unfortunately the output doesn't quite meet the customers needs.
This is the output:
<?xml version="1.0"?>
<Objects>
      <Object Type="System.Management.Automation.PSCustomObject">
            <Property Type="System.String" Name="Name">B012345</Property>
            <Property Type="System.String" Name="Full_name">Kasper Katzmann</Property>
            <Property Type="System.String" Name="Comment">pw</Property>
            <Property Type="System.Security.Principal.SecurityIdentifier" Name="Id">S-1-5-21-2105555113-1573851820-874322375-112263</Property>
            <Property Type="System.String" Name="AccountType">Normal</Property>
            <Property Type="System.Boolean" Name="AccountPropsEnabled">False</Property>
            <Property Type="System.Boolean" Name="AccountPropsPasswdRequired">True</Property>
            <Property Type="System.Boolean" Name="AccountPropsPasswdCantChange">False</Property>
            <Property Type="System.Boolean" Name="AccountPropsPasswdDontExpire">False</Property>
            <Property Type="System.Boolean" Name="AccountPropsCurrentlyLockedOut">False</Property>
      </Object>
</Objects>

Open in new window

What the customer really want's is this:
<User>
    <Name>B012345</Name>
    <Full_name>Kasper Katzmann</Full_name>
    <Comment>pw</Comment>
    <Id>S-1-5-21-2105555113-1573851820-874322375-112263</Id>
    <AccountType>Normal</AccountType>
    <AccountPropsEnabled>1</AccountPropsEnabled>
    <AccountPropsLogonScript>1</AccountPropsLogonScript>
    <AccountPropsHomeDirRequired>0</AccountPropsHomeDirRequired>
    <AccountPropsPasswdRequired>1</AccountPropsPasswdRequired>
    <AccountPropsPasswdCantChange>0</AccountPropsPasswdCantChange>
    <AccountPropsPasswdDontExpire>1</AccountPropsPasswdDontExpire>
    <AccountPropsCurrentlyLockedOut>0</AccountPropsCurrentlyLockedOut>
    <GroupIds>50393,513,61866</GroupIds>
</User>

Open in new window

So my primary question is, how do I strip the output to meet the expectations and secondary, how do I convert "True" and "False" to "1" and "0"
PowershellXML

Avatar of undefined
Last Comment
Kasper Katzmann

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kasper Katzmann

ASKER
Thats pretty fantastic :-)
Thank you very much
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck