I wish to create a script to automatically configure a server and join it to a domain. Before joining, I wish the user to enter a valid OU distinguished name. So I need to check if this OU exists.
Problem is, this check will be done on a server not joined to the domain (of course...), and without the possibility to install AD Powershell Module (so no Get-ADOrganizationUnit...).
So I try to use [ADSI]::exist command to check if the OU does exist
Here is where I am:
Do{
$serverOU = read-host "Please, enter a valid Distinguished Name of the OU where to move the server in Active Directory"
$OUcheck = [adsi]::Exists("LDAP://$serverOU")
if ($OUcheck -ne "True"){
write-host "This OU does not exist" -ForegroundColor Red
}
}
But of course, -Credential doesn't work. I saw there is a possibility to do something with the command
New-Object System.DirectoryServices.DirectoryEntry
But I don't really understand how to use that.
Is there someone who can give me a direction?
Thanks in advance,
For those who wondering, here's how I got around the problem...
I created a loop with the error code in case of problem. If there is no error while joining the domain, the script continue normally. If there is any problem, it stops. If problem with the OU distinguished name, it goes back to the line to choose the OU.