简体   繁体   中英

New User in AD with multiple OUs

My first post here.

So here is how it goes,

The end goal is to place the New User in the specific OU, under its distinguished name as path. I can resolve this with one subsequent OU selections, but once there are multiple, I would like to avoid the subsequent scripting, because the number of subsequent OUs can change.

Does anyone have an idea how to create a loop or any other solution that will continue to ask me through each layer of OU's until desired OU is chosen.

For example, User will be employed as Safety in US, under state IL, in city Chicago My OU configuration would be as follows: US IL Chicago Safety Users

I would like the script to loop through each layer until I've chosen Users, or a parameter to my liking. Note that each of subsequent OU's would have multiple choices, which I would like to sort and choose within a Grid. I already know this part, but what's tricky for me is creating that loop.

Not very familiar with While Do Until on powershell.

I will do my research but would appreciate any suggestions. It doesn't have to be a loop, but I'm looking for a solution with $ instead of a definite parameter.

Cheers,

if ($Location -eq $null )
{
[System.Windows.MessageBox]::Show("'You didn't enter the required parameter, script ending'","System Notification","Ok")
 exit
}
if ($Location -ne $null )               
{
while ($Location -ne $null) {
  $DomainOU1=Get-ADOrganizationalUnit -Filter "Name -like '$Location'" -SearchBase $DistinguishedGroupName -SearchScope OneLevel | select Name -ExpandProperty Name
  }
do {
  $Location1=@($DomainOU1) | sort
  $Location1=$Location1 | Out-GridView -Title "Choose Employee Location of Deployment"
  $Location1=$Location
}
Until ($Location -eq "Users")
}
Write-output "You chose $Location"

Not a full answer, but this looks problematic:

while ($Location -ne $null) {
  $DomainOU1=Get-ADOrganizationalUnit -Filter "Name -like '$Location'" -SearchBase $DistinguishedGroupName -SearchScope OneLevel | select Name -ExpandProperty Name
  }

It loops based on the $location variable, but nothing in the loop will ever change the $location variable, so it will loop forever.

So what I've decided to do in this case, is to add Users/Computers OU to all Destination OU's, and filter the $ by Users.

$EmployeeOU=Get-ADOrganizationalUnit -Filter "Name -like 'Users'" -SearchBase (Get-ADDomain).DistinguishedName | select Name,DistinguishedName

$Location=@($EmployeeOU) | sort
$Location=$Location | Out-GridView -Title "Choose Employee Location of Employment" -PassThru

This allows me to put the user in desired location by showing me the DistinguishedName OU details.

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