为什么不追求这样的结果呢?
<attribute name="Role">
<attributeValues>
<attributeValueRef>"Operator"</attributeValueRef>
<attributeValueRef>"Admin"</attributeValueRef>
</attributeValues>
</attribute>
无论如何,由于您正在使用Here String模板构建xml,我建议:
通过为添加单个值或值数组来更改数据
Role
房地产类
$data = @(
[pscustomobject]@{Type='P';ID='Fred';Role='Operator', 'Admin';Domain='Home'}
...
)
然后重写函数
function BuildXML {
param(
[char]$Type,
[string]$ID,
[string[]]$Role, # string array or single value
[string]$Domain
)
$CC = "1234"
$Account = "SNOW_CC$CC" + "_BNK0000_BLAH"
# Change the templates by adding a placeholder text you can replace later
$typeN = @"
<account id="$Account">
<name><![CDATA[$ID@$Domain]]></name>
<endPoint>UNC</endPoint>
<domain>UNC</domain>
<comments/>
<attributes>
<attribute name="appUserName">
<attributeValues>
<attributeValue><value><![CDATA[$ID]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="CostCentre">
<attributeValues>
<attributeValue><value><![CDATA[$CC]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="Bank_Number">
<attributeValues>
<attributeValue><value><![CDATA[0000]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="Directory">
<attributeValues>
<attributeValue><value><![CDATA[BLAH]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="Role">
<attributeValues>
@@ROLES@@
</attributeValues>
</attribute>
</attributes>
</account>
"@
$typeP = @"
<account id="$ID">
<name><![CDATA[$ID@$Domain]]></name>
<endPoint>ABC</endPoint>
<domain>ABC</domain>
<comments/>
<attributes>
<attribute name="AppBoRID">
<attributeValues>
<attributeValue><value><![CDATA[$ID]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="Role">
<attributeValues>
@@ROLES@@
</attributeValues>
</attribute>
</attributes>
</account>
"@
if ($Type -eq "N") { $xmlTemplate = $typeN }
else { $xmlTemplate = $typeP }
# Replace the @@ROLES@@ placeholder
# adjust this spacer to the number of tabs in your actual xml do it looks nice
$spacer = "`t" * 5 # 5 TABS in this example
$roleAttribs = $(foreach ($userRole in $Role) {
'{0}<attributeValueRef id="Role={1}"/>' -f $spacer, $userRole
}) -join [environment]::NewLine
# save to file
$xmlTemplate -replace '@@ROLES@@', $roleAttribs | Add-Content $WORKPATH$FEEDFILENAME
}
# for demo:
BuildXML -Type 'P' -ID 'Fred' -Role 'Admin', 'Operator' -Domain 'whatever.com'
结果将是
<account id="Fred">
<name><![CDATA[[email protected]]]></name>
<endPoint>ABC</endPoint>
<domain>ABC</domain>
<comments/>
<attributes>
<attribute name="AppBoRID">
<attributeValues>
<attributeValue><value><![CDATA[Fred]]></value></attributeValue>
</attributeValues>
</attribute>
<attribute name="Role">
<attributeValues>
<attributeValueRef id="Role=Admin"/>
<attributeValueRef id="Role=Operator"/>
</attributeValues>
</attribute>
</attributes>
</account>
我还建议您将函数重命名为以下内容
Approved Verbs
.
也许
Format-UserXml
会是个主意吗?