2017-11-11 174 views
1

我修改了一个PowerShell脚本来自动创建AD和Office 365帐户,它工作正常,但帮助台需要手动输入OU路径。要求用户从存储列表中选择选项

有没有办法预先定义OU路径&分配给它的号码,所以如果帮助台按1它选择OU路径分配给号码1等?

 
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 
#Import needed module. 
Import-Module ActiveDirectory 

#Prompt for needed information to use as variables below 
$fullname = Read-Host "Enter Full Name" 
$first = Read-Host "First name" 
$last = Read-Host "Last name" 
$user = Read-Host "Username" 
$title = Read-Host "Title" 
Get-ADOrganizationalUnit -Filter * -Properties * -SearchBase "OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM" | 
    Select-Object -Property Name 
$department = Read-Host "Enter department from above list" 
$manager = Read-Host "Manager userame" 
$srcuser = Read-Host "Username to copy" 
Get-ADOrganizationalUnit -Filter * -Properties * -SearchBase "OU=Departments,OU=Users,OU=Test Enviorment,OU=New Zealand,OU=BNZ,DC=BNZTEST,DC=COM" | 
    Select-Object -Property Name, DistinguishedName | 
    Format-Table -Auto 
$OU = Read-Host "Select OU from above list" 

#Create a new user with the provided information and some static information 
New-ADUser -Name "$fullname" -GivenName "$first" -Surname "$last" -DisplayName "$first $last" -Description "$title" -EmailAddress "[email protected]" -SamAccountName "$user" -UserPrincipalName "[email protected]" -Manager "$manager" -Title "$title" -AccountPassword (Read-Host -AsSecureString "Please enter the desired password") -Enabled $true -Path $OU 

#Add multiple ProxyAddresses if needed 
Set-ADUser "$user" -Add @{ProxyAddresses="smtp:[email protected]"} 

#Copy group membership of the source user above 
Get-ADUser -Identity "$srcuser" -Properties memberof | 
    Select-Object -ExpandProperty memberof | 
    Add-ADGroupMember -Members "$user" -PassThru | 
    Select-Object -Property SamAccountName >$null 
Write-Host 'CHECK AD REPLICATION BEFORE CONTINUING!' 
pause 

#Sync user to Office 365 using Dir Sync on a remote server 
Import-Module ADSync 
Start-ADSyncSyncCycle -PolicyType Initial 
Start-Sleep -s 100 

#License user in Office 365 
$AdminName = "[email protected]" 
$Pass = Get-Content "C:\Users\Administrator\Desktop\CreateUser\Cred.txt" | 
     ConvertTo-SecureString 
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AdminName, $Pass 
Import-Module MSOnline 
Connect-MsolService -Credential $cred 
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection 
Import-PSSession $Session 
Start-Sleep -s 15 
Set-MsolUser -UserPrincipalName "[email protected]" -UsageLocation 'US' 
Set-MsolUserLicense -UserPrincipalName "[email protected]" -AddLicenses "TESTBNZ:O365_BUSINESS_PREMIUM" 
Start-Sleep 90 
Write-Host 'ENSURE THERE ARE NO ERRORS AND THAT THE MAILBOX HAS BEEN CREATED BEFORE CONTINUING!' 
pause 
+0

基本上,你要我们创建所有的OU的字典? – wp78de

+0

他已经在使用'Get-ADOrganizationalUnit'来获取列表OU,它只需要用作'选择'的输入并呈现给帮助台。 –

回答

2

你可以添加一个simple menu这样的:

$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) 
+0

将条件更改为'$ result -eq 1',您可以从代码中删除标签和'switch'语句。这里不需要无限循环。 –

+0

同意。谢谢Ansgar。 – wp78de

2

您可以使用Out-GridView -OutputMode Single呈现服务台有GUI从对象选择。例如:

enter image description here

$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 

然后你可以使用OU与New-ADUser命令:

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