PowerShell

时间:2017-08-09 15:39:19

标签: xml powershell powershell-studio

现在我正在使用一个按钮为其创建的每个用户生成唯一的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>

我要做的是在<Group>下制作多个Groups代码;用户从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) }
}

1 个答案:

答案 0 :(得分:2)

假设$SelectedGroups填充了核对表选择的一组组(或者用你的数据替换它,你可以这样做):

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

然后只需在$Groups中的适当位置使用$GeneratedXML