在过去的几周里,我读了一些关于我的问题的非常有趣的帖子,并且已经接近了,但是我需要一个非常具体的XML,它是从一个哈希表中创建的,这个哈希表是我通过比较CSV和Active directory来创建的,Active directory输出具有特定用户数据的csv。
具体的字段有: XrefCode、EmployeeNumber、FirstName、LastName、BusinessEmail、StartDate。我需要将此数据放入的XML在格式上非常具体,我找不到一种方法来实现这一点。下面是XML需要如何结束。因此,它必须在文档顶部具有<EmployeeImport>标头,在底部具有</EmployeeImport>标头,以及在数据<ContactInformation>和</ContactInformation>的中间。
这是我使用的数据示例,非常基本。我的代码到达AD并填充电子邮件字段。
号码显示名称状态地点分类帐代码业务电子邮件名字姓1234567无名氏,约翰活跃0GB50约翰无名氏2345678史密斯,简活跃0GB50简·史密斯
哈希表的构建看起来像...
$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 += $matchcsv看起来如下所示:对不起,它没有很好地对齐
XRefCode EmployeeNumber FirstName LastName ContactInformationTypeXrefCode EffectiveStart IsForSystemCommunication ElectronicAddress
1234567 1234567 Joe Doe BusinessEmail 04/16/2021 0 Jdoe@Company.com
2345678 2345678 Jane Smith BusinessEmail 04/16/2021 0 Jsmith@Company.com<EmployeeImport>
<Employee>
<XRefCode>1234567</XRefCode>
<EmployeeNumber>1234567</EmployeeNumber>
<FirstName>Joe</FirstName>
<LastName>Doe</LastName>
<ContactInformation>
<ContactInformationTypeXrefCode>BusinessEmail</ContactInformationTypeXrefCode>
<EffectiveStart>2020-04-16</EffectiveStart>
<ElectronicAddress>JoeDoe@Company.com</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>Jsmith@Company.com</ElectronicAddress>
</ContactInformation>
</Employee>
</EmployeeImport>我发现接近的代码是这样的.
$output |% {'<Objects>'} {$_.psobject.properties |% {'<Object>'} {"<$($_.name)>$($_.value)</$($_.name)>"} {' </Object>'} } {'</Objects>'}但是,这并没有为ContactInformation添加所需的页眉和页脚以及中间对象。
对于我的情况,我可能需要这样做,但我不确定如何构建它。如果是这样的话,有没有人可以给我指出正确的方向,以便为这个特定的情况定制这个?
谢谢
发布于 2021-04-17 06:13:37
首先创建一个包含EmployeeImport根级别元素的[xml]文档:
$xml = [xml]'<EmployeeImport/>'
$emplImport = $xml.DocumentElement现在,您需要将一个<Employee>节点添加到每个输入对象的根节点:
$output = Import-Csv .\path\to\objects.csv
foreach($object in $output){
# Create `<Employee>` node
$employee = $emplImport.AppendChild($xml.CreateElement('Employee'))
foreach($property in $object.psobject.Properties){
# Create an xml child-element for each property on the input object
$childNode = $employee.AppendChild($xml.CreateElement($property.Name))
$childNode.InnerText = $property.Value
}
}最后将生成的xml文档保存到磁盘:
# Important: only pass full/rooted paths to $xml.Save()
$outputPath = Resolve-Path .\path\to\output.csv
$xml.Save($outputPath)生成的文件应如下所示:
<EmployeeImport>
<Employee>
<XRefCode>1234567</XRefCode>
<EmployeeNumber>1234567</EmployeeNumber>
<FirstName>Joe</FirstName>
<LastName>Doe</LastName>
<ContactInformationTypeXrefCode>BusinessEmail</ContactInformationTypeXrefCode>
<EffectiveStart>04/16/2021</EffectiveStart>
<IsForSystemCommunication>0</IsForSystemCommunication>
<ElectronicAddress>Jdoe@Company.com</ElectronicAddress>
</Employee>
<Employee>
<XRefCode>2345678</XRefCode>
<EmployeeNumber>2345678</EmployeeNumber>
<FirstName>Jane</FirstName>
<LastName>Smith</LastName>
<ContactInformationTypeXrefCode>BusinessEmail</ContactInformationTypeXrefCode>
<EffectiveStart>04/16/2021</EffectiveStart>
<IsForSystemCommunication>0</IsForSystemCommunication>
<ElectronicAddress>Jsmith@Company.com</ElectronicAddress>
</Employee>
</EmployeeImport>发布于 2021-04-17 07:14:05
这就是我在你的问题上的方法,我更喜欢Mathias解决方案,因为它是干净的代码,这是相当丑陋的,但它的目的。
$csv=@'
XRefCode,EmployeeNumber,FirstName,LastName,ContactInformationTypeXrefCode,EffectiveStart,IsForSystemCommunication,ElectronicAddress
1234567,1234567,Joe,Doe,BusinessEmail,04/16/2021,0,Jdoe@Company.com,
2345678,2345678,Jane,Smith,BusinessEmail,04/16/2021,0,Jsmith@Company.com
'@|ConvertFrom-Csv
$properties=$csv[0].PSObject.Properties.Name
[xml]$xml=@"
<EmployeeImport>
$(foreach($line in $csv){
@'
<Employee>
<XRefCode>{0}</XRefCode>
<EmployeeNumber>{1}</EmployeeNumber>
<FirstName>{2}</FirstName>
<LastName>{3}</LastName>
<ContactInformation>
<ContactInformationTypeXrefCode>{4}</ContactInformationTypeXrefCode>
<EffectiveStart>{5}</EffectiveStart>
<IsForSystemCommunication>{6}</IsForSystemCommunication>
<ElectronicAddress>{7}</ElectronicAddress>
</ContactInformation>
</Employee>
'@ -f ($properties|%{$line.$_})
})
</EmployeeImport>
"@

https://stackoverflow.com/questions/67131830
复制相似问题