Customizing XML with Powershell

Kasper Katzmann
Kasper Katzmann used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Kasper KatzmannSeniorkonsulent

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial