Powershell 要求用户从存储列表中选择选项(AD OU路径)

Powershell 要求用户从存储列表中选择选项(AD OU路径),powershell,ou,Powershell,Ou,我修改了PowerShell脚本以自动创建AD和Office 365帐户,它工作正常,但帮助台需要手动键入OU路径 是否有方法预先定义OU路径并为其分配编号,以便帮助台在按下1时选择分配给编号1的OU路径,依此类推 Name DistinguishedName ---- ----------------- Departments OU=Departments,OU=Users,OU=Test Enviorment,OU=New Ze

我修改了PowerShell脚本以自动创建AD和Office 365帐户,它工作正常,但帮助台需要手动键入OU路径

是否有方法预先定义OU路径并为其分配编号,以便帮助台在按下
1
时选择分配给编号1的OU路径,依此类推

Name DistinguishedName ---- ----------------- Departments OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM Operational OU=Operational,OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM Normal OU=Normal,OU=Operational,OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTE.. Sales OU=Sales,OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM Finance OU=Finance,OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM IT OU=IT,OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM Application OU=Application,OU=IT,OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,D.. Infrastructure OU=Infrastructure,OU=IT,OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTES.. Marketing OU=Marketing,OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM NewBusiness OU=NewBusiness,OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM ExisitingBusiness OU=ExisitingBusiness,OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,D.. Underwritter OU=Underwritter,OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM 您可以添加一个如下所示的:

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",
       "Exits the loop."    
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",
      "Allows to add another user."

$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

do 
{ 
    $user = New-Object System.Management.Automation.Host.ChoiceDescription "&User", "User"
    $it = New-Object System.Management.Automation.Host.ChoiceDescription "&IT", "IT"
    $sales = New-Object System.Management.Automation.Host.ChoiceDescription "&Sales", "Sales"
    $OUoptions = [System.Management.Automation.Host.ChoiceDescription[]]($user, $it, $sales)
    $OU = $host.ui.PromptForChoice("Which OU", "Which OU", $OUoptions, 0) 

    switch ($OU) 
    { 
        0 {Write-Host "The choise is User."} 
        1 {Write-Host "IT"} 
        2 {Write-Host "Sales"} 
        default {Write-Host "The color could not be determined."}
    }

    $result = $host.ui.PromptForChoice("Continue?", "Do you want to add another user?", $options, 1)        
}
while ($result -eq 1)

您可以使用
Out GridView-OutputMode Single
向帮助台提供GUI,以便从对象中进行选择。例如:

然后,您可以将OU与您的
New ADUser
命令一起使用:

New-ADUser [...] -OU $OU.DistinguishedName

基本上,你想让我们为你所有的OU创建一个字典?他已经在使用
Get ADOrganizationalUnit
来获取OU列表,它只需要用作“选择”的输入并显示给帮助台。将条件更改为
$result-eq 1
,您可以从代码中删除标签和
开关
语句。这里不需要无限循环。同意。谢谢你,安斯加。
$SearchBase = "OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM"
$OUList = Get-ADOrganizationalUnit -SearchBase $SearchBase -Filter * -Properties Name,DistinguishedName | Select-Object -Property Name,DistinguishedName

$OU = $OUList | Out-GridView -Title "Select OU and Click OK" -OutputMode Single
New-ADUser [...] -OU $OU.DistinguishedName