简体   繁体   中英

Auto-generated XML code in powershell

Right now I am using a button to generate unique XML code for every user it creates. Right now my XML looks like this:

<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>

What I am trying to do is make multiple <Group> tags under the Groups ; one for each group that a user selects from a CheckedListBox.

What I need is something like this:

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

Each one of these checkboxes is populated with a resource group that I pull directly from Active Directory. I assume I'm going to need to write some sort of foreach loop to set everything, but I can't seem to figure it out. Here is what I have so far:

# 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"

And here is where I populate the CheckedListBox with the active directory resource groups:

$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) }
}

Assuming $SelectedGroups is populated with an array of groups selected by the checklist (or replace this with however you get that data), you can do this:

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

Then just use $Groups in the appropriate place in $GeneratedXML .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM