I have read some very interesting posts on the topic of my question in the past few weeks and have gotten close, but I need a very specific XML created from a hash table that i create by comparing a CSV to Active directory that outputs a csv with specific user data.
The specific fields are these: XrefCode, EmployeeNumber, FirstName, LastName, BusinessEmail, StartDate.
The XML I need to have this data put in is extremely specific in format and I cannot find a way get this to work.
Here is the XML how it needs to end up like.
So it has to have a header of <EmployeeImport> at the top of the document and </EmployeeImport> at the bottom, as well as in the middle of the data <ContactInformation> and </ContactInformation>.
Here is a sample of the data i use, very basic. My code reaches to AD and populates the email field.
Number Display Name Status Location Ledger Code Business Email First Name Last Name 1234567 Doe, John Active 0GB50 John Doe 2345678 Smith, Jane Active 0GB50 Jane Smith
The hash table build looks like...
$match = New-Object PSObject
$match | Add-Member NoteProperty "XRefCode" $first.SamAccountName
$match | Add-Member NoteProperty "EmployeeNumber" $first.SamAccountName
$match | Add-Member NoteProperty "FirstName" $first.'First Name'
$match | Add-Member NoteProperty "LastName" $first.'Last Name'
$match | Add-Member NoteProperty "ContactInformationTypeXrefCode" 'BusinessEmail'
$match | Add-Member NoteProperty "EffectiveStart" $TodaysDay
$match | Add-Member NoteProperty "ElectronicAddress" $second.mail
$combine += $match
The csv looks like the following: Sorry it doesn't line up well
XRefCode EmployeeNumber FirstName LastName ContactInformationTypeXrefCode EffectiveStart IsForSystemCommunication ElectronicAddress
1234567 1234567 Joe Doe BusinessEmail 04/16/2021 0 [email protected]
2345678 2345678 Jane Smith BusinessEmail 04/16/2021 0 [email protected]
<EmployeeImport>
<Employee>
<XRefCode>1234567</XRefCode>
<EmployeeNumber>1234567</EmployeeNumber>
<FirstName>Joe</FirstName>
<LastName>Doe</LastName>
<ContactInformation>
<ContactInformationTypeXrefCode>BusinessEmail</ContactInformationTypeXrefCode>
<EffectiveStart>2020-04-16</EffectiveStart>
<ElectronicAddress>[email protected]</ElectronicAddress>
</ContactInformation>
</Employee>
<Employee>
<XRefCode>2345678</XRefCode>
<EmployeeNumber>2345678</EmployeeNumber>
<FirstName>Jane</FirstName>
<LastName>Smith</LastName>
<ContactInformation>
<ContactInformationTypeXrefCode>BusinessEmail</ContactInformationTypeXrefCode>
<EffectiveStart>2020-04-16</EffectiveStart>
<ElectronicAddress>[email protected]</ElectronicAddress>
</ContactInformation>
</Employee>
</EmployeeImport>
The code I found that was close was this...
$output |% {'<Objects>'} {$_.psobject.properties |% {'<Object>'} {"<$($_.name)>$($_.value)</$($_.name)>"} {' </Object>'} } {'</Objects>'}
However, this doesn't add the header and footer needed as well as the middle Object between for the ContactInformation.
I might need to do this as custom for my case, but i'm not sure how to build this out. If that's the case, could someone point me in the right direction to customize this for this specific case?
Thanks

$outputis a collection of objects that haveXrefCode,EmployeeNumber,FirstName, ... etc. as properties?