Avatar of Kasper Katzmann
Kasper KatzmannFlag for Denmark

asked on 

Append data to an XML file

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

Open in new window


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>

Open in new window


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>

Open in new window

XMLPowershell

Avatar of undefined
Last Comment
oBdA
Avatar of oBdA
oBdA

"This is what I want" is not valid XML; it's a text file vaguely looking like XML. Are you absolutely sure you need that format?
A valid XML document requires a root node.
Since (as of your earlier question) the XML definition is not up to you, you need to first find out which format you need to incorporate multiple users, so that the output you produce can be used. The former specification was only for a single user.
Avatar of Kasper Katzmann

ASKER

You are right - The customer keeps calling it XML, so I tend to say the same.
For anyone interested, this is my earlier question: Customizing XML with Powershell

The customer want it in this particular format, but basically he just want a a textfile named .xml
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Blurred text
THIS SOLUTION IS 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
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo