troubleshooting Question

Append data to an XML file

Avatar of Kasper Katzmann
Kasper KatzmannFlag for Denmark asked on
XMLPowershell
3 Comments1 Solution131 ViewsLast Modified:
I have a script which in the end creates an XML file. The XML file is supposed to contain information about more than 200 users, but when I run the script the XML file only contains information about one user.

How do I create an XML file that contains all users and not just the last in the array?

The part that creates the XML file starts at line 75.
Function ConvertTo-CustomXml($ADUser) {

    $SID = [STRING]$ADUser.SID
    $SID = $SID.Split("-")[-1]

	$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 = $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 = (@($ADUser.MemberOf | Get-ADGroup | Select-Object -ExpandProperty Sid | Select-Object -ExpandProperty Value | ForEach-Object {$_.Split("-")[-1] })) -join ','
	Return $xml
}

#region GROUPS
$groups =   “CU2701-SG-Proteus_BLH”,
            ”CU2701-SG-Proteus_BON”,
            ”CU2701-SG-Proteus_FYN”,
            ”CU2701-SG-Proteus_HIM”,
            ”CU2701-SG-Proteus_HST”,
            ”CU2701-SG-Proteus_KJY”,
            ”CU2701-SG-Proteus_MJY”,
            ”CU2701-SG-Proteus_NSJ”,
            ”CU2701-SG-Proteus_OSJ”,
            ”CU2701-SG-Proteus_SDJ”,
            ”CU2701-SG-Proteus_SHL”,
            ”CU2701-SG-Proteus_STS”,
            ”CU2701-SG-Proteus_THY”,
            ”CU2701-SG-Proteus_TRE”,
            ”CU2701-SG-Proteus_VAD”,
            ”CU2701-SG-Proteus_VJY”,
            ”CU2701-SG-Proteus_VSJ”,
            ”CU2701-SG-Proteus_VSY”,
            ”CU2701-SG-Proteus_DRC”,
            ”CU2701-SG-SNS_Proteus_Adm”,
            ”CU2701-SG-SNS_Proteus_Godkender”,
            ”CU2701-SG-SNS_Proteus_Markhold”,
            ”CU2701-SG-SNS_Proteus_Konsulent”,
            ”CTX_Proteus”,
            ”CTX_Natura2000”,
            ”NST_Natura2000”
#endregion

#region EXTRACT GROUP MEMBERS
foreach($group in $groups)
{
    $members = Get-ADGroupMember $group | select samAccountName
    $users   = $users + $members
    

}

$users = $users | select samAccountName -Unique
#endregion

#region EXPORT USER DATA TO XML
$data = @()
foreach($user in $users)
{
    $usr = $user.samAccountName

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

    #ConvertTo-CustomXml is a function
    $XMLdata = ConvertTo-CustomXml -ADUser $ud
    $data = $data += $XMLdata
   
}
$XMLdata.Save("C:\temp\proteus.xml")
#endregion

This is what I get
<?xml version="1.0"?>
-<User>
	<Name>U76454</Name>
	<Full_name>Poul Young</Full_name>
	<Comment> </Comment>
	<Id>35953</Id>
	<AccountType>Normal</AccountType>
	<AccountPropsEnabled>1</AccountPropsEnabled>
	<AccountPropsPasswdRequired>1</AccountPropsPasswdRequired>
	<AccountPropsPasswdCantChange>0</AccountPropsPasswdCantChange>
	<AccountPropsPasswdDontExpire>0</AccountPropsPasswdDontExpire>
	<AccountPropsCurrentlyLockedOut>0</AccountPropsCurrentlyLockedOut>
	<GroupIds>162771,151095,158803,152557,152555,156245</GroupIds>
</User>

This is what I want
<?xml version="1.0"?>
-<User>
	<Name>U76454</Name>
	<Full_name>Poul Young</Full_name>
	<Comment> </Comment>
	<Id>35953</Id>
	<AccountType>Normal</AccountType>
	<AccountPropsEnabled>1</AccountPropsEnabled>
	<AccountPropsPasswdRequired>1</AccountPropsPasswdRequired>
	<AccountPropsPasswdCantChange>0</AccountPropsPasswdCantChange>
	<AccountPropsPasswdDontExpire>0</AccountPropsPasswdDontExpire>
	<AccountPropsCurrentlyLockedOut>0</AccountPropsCurrentlyLockedOut>
	<GroupIds>162771,151095,158803,152557,152555,156245</GroupIds>
</User>
-<User>
	<Name>U09323</Name>
	<Full_name>Kim Carnes</Full_name>
	<Comment> </Comment>
	<Id>35987</Id>
	<AccountType>Normal</AccountType>
	<AccountPropsEnabled>1</AccountPropsEnabled>
	<AccountPropsPasswdRequired>1</AccountPropsPasswdRequired>
	<AccountPropsPasswdCantChange>0</AccountPropsPasswdCantChange>
	<AccountPropsPasswdDontExpire>0</AccountPropsPasswdDontExpire>
	<AccountPropsCurrentlyLockedOut>0</AccountPropsCurrentlyLockedOut>
	<GroupIds>162771,151095,158803,152557,152555,156245</GroupIds>
</User>
-<User>
	<Name>U12345</Name>
	<Full_name>John Doe</Full_name>
	<Comment> </Comment>
	<Id>35913</Id>
	<AccountType>Normal</AccountType>
	<AccountPropsEnabled>1</AccountPropsEnabled>
	<AccountPropsPasswdRequired>1</AccountPropsPasswdRequired>
	<AccountPropsPasswdCantChange>0</AccountPropsPasswdCantChange>
	<AccountPropsPasswdDontExpire>0</AccountPropsPasswdDontExpire>
	<AccountPropsCurrentlyLockedOut>0</AccountPropsCurrentlyLockedOut>
	<GroupIds>162771,151095,158803,152557,152555,156245</GroupIds>
</User>
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros