a function containing switch statement and foreach-object loop

A "switch" statement does not test the existence of anything.

$CompanyRoot="ou=MyCompany, dc=mytestdomain,dc=lab'"
New-ADOrganizationalUnit -Name Computers -Path $companyroot -ErrorAction SilentlyContinue

February 15th, 2015 8:51am

Like this:

$CompanyRoot='ou=MyCompany, dc=mytestdomain,dc=lab'
New-ADOrganizationalUnit -Name MyCompany -ErrorAction SilentlyContinue $ous='Users', 'Computers','Servers','Administrators','Helpdesk' foreach($ou in $ous){ New-ADOrganizationalUnit -Name $ou -Path $companyroot -ErrorAction SilentlyContinue }

Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 8:54am

Start with this overview: https://msdn.microsoft.com/en-us/library/bb727085.aspx
February 15th, 2015 9:00am

Like this:

$CompanyRoot=tha'ou=MyCompany, dc=mytestdomain,dc=lab'
New-ADOrganizationalUnit -Name MyCompany -ErrorAction SilentlyContinue $ous='Users', 'Computers','Servers','Administrators','Helpdesk' foreach($ou in $ous){ New-ADOrganizationalUnit -Name $ou -Path $companyroot -ErrorAction SilentlyContinue }

Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 9:18am

i myself continued working on my script & change it many times so make it more accurate. now i have reached good results but finally end up in this state:

[Array]$OUs = 'ou-users','ou-computers','ou-helpdesks','ou-admins','ou-servers'
$ous | ForEach-Object {
if (-not(Get-ADOrganizationalUnit -Identity "ou=$_,dc=mytestdomain,dc=lab")) { New-ADOrganizationalUnit -Name $_ -Protected 0 }
}

here the problem is, for example when 'ou-users' currently doesn't exist and i run this code, here in this line, system encounters a terminating error so it doesn't continue to create that:

if (-not(Get-ADOrganizationalUnit -Identity "ou=$_,dc=mytestdomain,dc=lab"))

so the scenario has become complicated. i think i must bring Try, catch, finally structure to play but i don't know how to utilize them here in this special scenario.

any help ?

  • Edited by john.s2011 20 hours 57 minutes ago
  • Marked as answer by john.s2011 16 hours 43 minutes ago
  • Unmarked as answer by john.s2011 16 hours 43 minutes ago
February 15th, 2015 9:30am

$CompanyRoot='ou=MyCompany, dc=mytestdomain,dc=lab'
$ous='OUusers', 'OUcomputers','OUservers','OUadministrators','OUhelpdesks'

foreach($ou in $ous){
     try{
         New-ADOrganizationalUnit -Name $ou -Path $companyroot
     }
         Write-Host 'OU already exists' -fore green
     }
}

Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 9:32am

Faster:

if(Test-Path "AD:/OU=$ou,$companyroot"){
     # exisits
}else{
         New-ADOrganizationalUnit -Name $ou -Path $companyroot
}

February 15th, 2015 9:35am

Hello Guys

in my testdomain.lab, i have following five OUs in mt test AD structure:

'OUusers', 'OUcomputers','OUservers','OUadministrators','OUhelpdesks'.

i have created following five variables:

$OUusers = 'ou=OUusers,dc=mytestdomain,dc=lab' $OUcomputers = 'ou=OUcomputers,dc=mytestdomain,dc=lab'

and so on



now i have created a function which gets this names as input & foreach-object creates the related OU.

but the problem is sometimes when i run my function, maybe some of those five OUs currently exist in AD so this will cause a terminating error

so i know i must use switch statement to define conditions like this:

if $OUusers doesn't exist, execute this :  new-ADOrganizationalunit $OUusers

if $OUcomputers doesn't exist, execute this :  new-ADOrganizationalunit $OUcomputers

and so on...

it's a long time which i am trying to create a function which contains such switch statement & properly forEach-object loop.

i have studied switch and foreach-object loops , but this scenario is complicated for me & i was unable to get the correct code.

Function createOUs {

# combination of ForEach-Object and Switch statement here

}

may someone give me the correct command please?

thanks a lot






  • Edited by john.s2011 Sunday, February 15, 2015 10:40 AM
Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 1:31pm

$CompanyRoot='ou=MyCompany, dc=mytestdomain,dc=lab'
$ous='OUusers', 'OUcomputers','OUservers','OUadministrators','OUhelpdesks'

foreach($ou in $ous){
     try{
         New-ADOrganizationalUnit -Name $ou -Path $companyroot
     }
         Write-Host 'OU already exists' -fore green
     }
}		
February 15th, 2015 1:49pm

Faster:

if(Test-Path "AD:/OU=$ou,$companyroot"){
     # exisits
}else{
         New-ADOrganizationalUnit -Name $ou -Path $companyroot
}

Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 1:51pm

by the way, sorry for delay, i was outside the home & hadn't access to internet. 

and here it's 10:00 pm . and i'am interested to know where are you from & where do you live now & what time is it there ?

maybe when i pose my questions in forum, you have slept there   !  :-)

February 15th, 2015 1:54pm

Thanks. Actually they are all pretty much equal but the Test-Path might be a little faster in a log loop. Not sure as I haven't tested it.

Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 2:00pm

by the way, sorry for delay, i was outside the home & hadn't access to internet. 

and here it's 10:00 pm . and i'am interested to know where are you from & where do you live now & what time is it there ?

maybe when i pose my questions in forum, you have slept there   !  :-)

10:00PM - you must be in Minsk.

At the North Pole there is no time.  It all depends on which direction you choose to look towards.

February 15th, 2015 2:03pm

i myself continued working on my script & change it many times so make it more accurate. now i have reached good results but finally end up in this state:

[Array]$OUs = 'ou-users','ou-computers','ou-helpdesks','ou-admins','ou-servers'
$ous | ForEach-Object {
if (-not(Get-ADOrganizationalUnit -Identity "ou=$_,dc=mytestdomain,dc=lab")) { New-ADOrganizationalUnit -Name $_ -Protected 0 }
}

here the problem is, for example when 'ou-users' currently doesn't exist and i run this code, here in this line, system encounters a terminating error so it doesn't continue to create that:

if (-not(Get-ADOrganizationalUnit -Identity "ou=$_,dc=mytestdomain,dc=lab"))

so the scenario has become complicated. i think i must bring Try, catch, finally structure to play but i don't know how to utilize them here in this special scenario.

any help ?

  • Edited by john.s2011 Sunday, February 15, 2015 2:32 PM
  • Marked as answer by john.s2011 Sunday, February 15, 2015 6:45 PM
  • Unmarked as answer by john.s2011 Sunday, February 15, 2015 6:46 PM
Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 5:28pm

by the way, sorry for delay, i was outside the home & hadn't access to internet. 

and here it's 10:00 pm . and i'am interested to know where are you from & where do you live now & what time is it there ?

maybe when i pose my questions in forum, you have slept there   !  :-)

10:00PM - you must be in Minsk.

At the North Pole there is no time.  It all depends on which direction you choose to look towards.

February 15th, 2015 9:41pm

Teheran was my second choice.  It is just a bit south of Minsk.

No, the poles have summer and winter just like everybody else.

Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 9:59pm

Teheran was my second choice.  It is just a bit south of Minsk.

No, the poles have summer and winter just like everybody

February 15th, 2015 10:32pm

Ancient place, great history.  Handsome, intelligent, well educated people.

Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 10:39pm

Ancient place, great history.  Handsome, intelligent, well educated people.

February 15th, 2015 10:53pm

Ancient place, great history.  Handsome, intelligent, well educated people.

Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 10:54pm

I agree that Islam is not terroristic but we are really a bit off topic,

Good luck and enjoy the coming spring,

February 15th, 2015 10:59pm

I agree that Islam is not terroristic but we are really a bit off topic,

Good luck and enjoy the coming s

Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 11:05pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics