store list of AD OU Path and pass it to a $OU when user choose.

Hi

I modified a script to add AD & office 365 users, it works fine but it need manual input for OU path so it can create user in specific OU. How can I automate it so it automatically pick OU path? Is there a way to store and pre-define the number to a OU and helpdesk can choose than it pass it to Path variable?

#Set the title of the window.
$host.ui.RawUI.WindowTitle = "New User Creation Script"

#Give the user a quick description
Write-Host 
Write-Host *******************************************************************************************
Write-Host This script will create a new AD user, sync the user to Office 365, and license the user.
Write-Host To run this script, you will need the ActiveDirectory and MSOnline PowerShell modules
Write-Host *******************************************************************************************
Write-Host 

#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 "$first.$last@bnztest.com" -SamAccountName "$user" -UserPrincipalName "$user@bnztest.com" -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:$first.$last@bnztest.com"}


#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 = "admin@testbnz.onmicrosoft.com"
$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 outlook.office365.com/.../ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Start-Sleep -s 15
Set-MsolUser -UserPrincipalName "$user@bnztest.com" -UsageLocation 'US'
Set-MsolUserLicense -UserPrincipalName "$user@bnztest.com" -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

  • 'Is there a way to store and pre-define the number to a OU and helpdesk can choose than it pass it to Path variable?'

    Can you define your ask a bit more?

    AD, OU, et all, you can define / store / present whatever you choose as long as you have a resource (dynamic or static) to pull and present that information from.

    You could just sue AD cmdlets and filter for the OU's you want to present dynamically, or export to a file and pull statically from there.

    You could present the list using Out-GridView cmdlet and set it to allow for only single or multiple selection, pipe those selection to your code to execute on.

    See some examples of using OGV as hinted at above:

    'mikefrobbins.com/2014/09/11/creating-a-simplistic-gui-interface-with-out-gridview'
    'mcpmag.com/articles/2013/01/08/pshell-gridview.aspx'