We help IT Professionals succeed at work.

Customizing XML with Powershell

Kasper Katzmann
on
183 Views
Last Modified: 2017-03-14
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"
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Kasper KatzmannSeniorkonsulent

Author

Commented:
Thats pretty fantastic :-)
Thank you very much

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions