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
Technology Tips and News
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
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 }
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 }
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 ?
$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 } }
Faster:
if(Test-Path "AD:/OU=$ou,$companyroot"){
# exisits
}else{
New-ADOrganizationalUnit
-Name $ou -Path $companyroot
}
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
$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 } }
Faster:
if(Test-Path "AD:/OU=$ou,$companyroot"){
# exisits
}else{
New-ADOrganizationalUnit -Name $ou -Path $companyroot
}
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 ! :-)
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.
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.
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 ?
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.
Teheran was my second choice. It is just a bit south of Minsk.
No, the poles have summer and winter just like everybody else.
Teheran was my second choice. It is just a bit south of Minsk.
No, the poles have summer and winter just like everybody
Ancient place, great history. Handsome, intelligent, well educated people.
Ancient place, great history. Handsome, intelligent, well educated people.
Ancient place, great history. Handsome, intelligent, well educated people.
I agree that Islam is not terroristic but we are really a bit off topic,
Good luck and enjoy the coming spring,
I agree that Islam is not terroristic but we are really a bit off topic,
Good luck and enjoy the coming s