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"
Kasper KatzmannSeniorkonsulentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Note that in your script, you swapped the values for 'AccountPropsEnabled' and 'AccountPropsPasswdRequired'. Furthermore, the customer attribute is 'AccountPropsPasswdRequired', while the AD attribute is 'PasswordNotRequired', so the logic needs to be reversed for that.
And just in case: note that XML is case sensitive, so make sure to spell the node names exactly as defined by the customer.
Function ConvertTo-CustomXml($ADUser) {
	$xml = [xml]"<User />"
	$NodeUser = $xml.SelectSingleNode('/User')
	$NewNode = $NodeUser.AppendChild($xml.CreateElement("Name"))
	$NewNode.InnerText = $ADUser.samAccountName
	$NewNode = $NodeUser.AppendChild($xml.CreateElement("Full_name"))
	$NewNode.InnerText = $ADUser.Name
	$NewNode = $NodeUser.AppendChild($xml.CreateElement("Comment"))
	$NewNode.InnerText = $ADUser.description
	$NewNode = $NodeUser.AppendChild($xml.CreateElement("Id"))
	$NewNode.InnerText = $ADUser.SID
	$NewNode = $NodeUser.AppendChild($xml.CreateElement("AccountType"))
	$NewNode.InnerText = 'Normal'
	$NewNode = $NodeUser.AppendChild($xml.CreateElement("AccountPropsEnabled"))
	$NewNode.InnerText = If ($ADUser.Enabled) {1} Else {0}
	$NewNode = $NodeUser.AppendChild($xml.CreateElement("AccountPropsPasswdRequired"))
	$NewNode.InnerText = If ($ADUser.PasswordNotRequired) {0} Else {1}
	$NewNode = $NodeUser.AppendChild($xml.CreateElement("AccountPropsPasswdCantChange"))
	$NewNode.InnerText = If ($ADUser.CannotChangePassword) {1} Else {0}
	$NewNode = $NodeUser.AppendChild($xml.CreateElement("AccountPropsPasswdDontExpire"))
	$NewNode.InnerText = If ($ADUser.PasswordNeverExpires) {1} Else {0}
	$NewNode = $NodeUser.AppendChild($xml.CreateElement("AccountPropsCurrentlyLockedOut"))
	$NewNode.InnerText = If ($ADUser.LockedOut) {1} Else {0}
	$NewNode = $NodeUser.AppendChild($xml.CreateElement("GroupIds"))
	$NewNode.InnerText = (@(513) + ($ADUser.MemberOf | Get-ADGroup | Select-Object -ExpandProperty Sid | Select-Object -ExpandProperty Value | ForEach-Object {$_.Split('-')[-1]})) -join ','
	Return $xml
}

$me = Get-ADUser -Identity jdoe -Properties `
		description,
		SID,
		description,
		Name,
		samAccountName,
		ObjectClass,
		Enabled,
		PasswordNotRequired,
		CannotChangePassword,
		PasswordNeverExpires,
		LockedOut,
		MemberOf

$meXML = ConvertTo-CustomXml -ADUser $me
$meXML.Save("C:\temp\me.xml")

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kasper KatzmannSeniorkonsulentAuthor Commented:
Thats pretty fantastic :-)
Thank you very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.