在 powershell 中自动生成 XML 代码

Auto-generated XML code in powershell

现在我正在使用一个按钮为其创建的每个用户生成唯一的 XML 代码。现在我的 XML 看起来像这样:

<NewUser>
    <ActiveDirectory>
        <User>
            <GivenName></GivenName>
            <Surname></Surname>
            <Title></Title>
            <Department></Department>
            <Company></Company>
            <OU></OU>
            <Office></Office>
            <Management></Management>
            <Groups>
                <Group=""/>
            </Groups>
            <Location></Location>
        </User>
    </ActiveDirectory>
    <Exchange>
        <Database></Database>
    </Exchange>
    <TMW>
        <SysAdmin></SysAdmin>
        <Supervisor></Supervisor>
        <CanDeletePay></CanDeletePay>
        <UseDateCalender></UseDateCalender>
    </TMW>
    <DriverRecruitment>
        <Admin></Admin>
        <ManageNews></ManageNews>
        <ManageEmployers></ManageEmployers>
        <ManageWorkflows></ManageWorkflows>
        <ManageCustomQuestions></ManageCustomQuestions>
    </DriverRecruitment>
</NewUser>

我想做的是在 Groups 下制作多个 <Group> 标签;一个用于用户从 CheckedListBox 中选择的每个组。

我需要的是这样的:

<Groups>
    <Group name="Group1"/>
    <Group name="Group2"/>
    <!-- Create the rest dynamically as they are checked -->
</Groups>

这些复选框中的每一个都填充了我直接从 Active Directory 中提取的资源组。我假设我需要编写某种 foreach 循环来设置所有内容,但我似乎无法弄清楚。这是我目前所拥有的:

# generates the xml
    $XML_Path = "\papertransport.com\files\UserDocuments\mneis\Code\XML\New User GUI XML Files\"
    $GeneratedXML = "
<NewUser>
    <ActiveDirectory>
        <User>
            <GivenName>$firstname</GivenName>
            <Surname>$lastname</Surname>
            <Title>$title</Title>
            <Department>$department</Department>
            <Company>$company</Company>
            <OU>$OU</OU>
            <Office>$office</Office>
            <Management>$management</Management>
            <Groups>
                <Group></Group> <!-- Here is where I need to loop -->
            </Groups>
            <Location>$location</Location>
        </User>
    </ActiveDirectory>
    <Exchange>
        <Database>$database</Database>
    </Exchange>
    <TMW>
        <SysAdmin></SysAdmin>
        <Supervisor></Supervisor>
        <CanDeletePay></CanDeletePay>
        <UseDateCalender></UseDateCalender>
    </TMW>
    <DriverRecruitment>
        <Admin></Admin>
        <ManageNews></ManageNews>
        <ManageEmployers></ManageEmployers>
        <ManageWorkflows></ManageWorkflows>
        <ManageCustomQuestions></ManageCustomQuestions>
    </DriverRecruitment>
</NewUser>"

    # Create file
    $GeneratedXML | Out-File "\papertransport.com\files\UserDocuments\mneis\Code\XML\New User GUI XML Files\NewUser-$firstname$lastname.xml"

这里是我用活动目录资源组填充 CheckedListBox 的地方:

$formPTINewUserCreation_Load={
    Import-Module ActiveDirectory

    # read in the XML files
    [xml]$NewUserTest = Get-Content -LiteralPath \papertransport.com\files\UserDocuments\mneis\Code\XML\NewUserTest.xml

    # popoulate the checklist with the groups from active directory
    $AD_ResourceGroups = Get-ADGroup -filter * -SearchBase "OU=Resource Groups,OU=Groups,OU=Paper Transport,DC=papertransport,DC=com"
    $AD_ResourceGroups | ForEach-Object { $chklistGroups.Items.Add($_.name) }
}

假设 $SelectedGroups 填充了清单选择的一组组(或者将其替换为您获得的数据),您可以这样做:

$Groups = $SelectedGroups | ForEach-Object {
    "               <Group>$_</Group>"
}

然后在$GeneratedXML中适当的地方使用$Groups即可。