0

I want to find out if an organisational unit exists or not so I wrote this following code but it shows up an error : Impossible to find object with the identity OU_Bloquage.Despite it does really exist(I've created it) Below is the code I've wrote

Import-Module ActiveDirectory
Import-Module 'Microsoft-PowerShell.Security'
$OUName = 'OU_Bloquage'
if([bool] (Get-ADOrganizationalUnit $OUName))
{ Write-Host 'true' }
else { Write-Host 'false' }
MKH_Cerbebrus
  • 51
  • 2
  • 10
  • 1
    Please add the actual code to your question, not a picture of it. – rpm192 Feb 28 '19 at 15:48
  • 1
    Please take a look at [ask] and put your code in the actual question, not as an image. Also please try to translate the error into englisch, not all people can understand multiple languages. My first guess would that you are looking for a OU that doesn't exist. This throws an error if you search for it over identity. – Paxz Feb 28 '19 at 15:50
  • 2
    Either use `try`&`catch` to catch the error when the ou doesn't exist. Or even better: Use the `-Filter` Parameter of the command to find OUs without throwing errors. – Paxz Feb 28 '19 at 15:51
  • Why import the module 'Microsoft-PowerShell.Security'? Everything you would need is in the ActiveDirectory module. –  Mar 03 '19 at 01:17

3 Answers3

1

@Bearded Brawler -- You're close, but missing the context of the rest of the question.

Instead:

$OUName = 'OU_Bloquage'                  # the OU your looking for.

$OUName = "Name -like '$($OUName)'"
if([bool](Get-ADOrganizationalUnit -Filter $OUName)) {
 Write-Host 'true'
} else {
  Write-Host 'false' }

Note: This assumes the OU is actually 'OU_Bloquage' and not actually 'Bloquage'. If it is just Bloquage then edit the first line to read as such.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

I'd use a filter instead to find an OU that you're not sure of the full path to

Get-ADOrganizationalUnit -Filter 'Name -like "*Bloquage*"' | Format-Table Name, DistinguishedName -A

  • 1
    my idea is to check if a OU exits then i should do a proper treatment and do another treatment if it's not the case. So can you tell me please how to use your expression in a if context? – MKH_Cerbebrus Feb 28 '19 at 17:19
0

This code should work Filter using Where-Object

Import-Module ActiveDirectory
$OUName = "OU_NAME"
if([bool] (Get-ADOrganizationalUnit -Filter * | ? {$_.Name -eq $OUName} ))
{ Write-Host 'true' }
else { Write-Host 'false' } 

Result:

PS C:\Windows\system32> Import-Module ActiveDirectory
$OUName = "CLOUD"
if([bool] (Get-ADOrganizationalUnit -Filter * | ? {$_.Name -eq $OUName} ))
{ Write-Host 'true' }
else { Write-Host 'false' } 
true

PS C:\Windows\system32> Import-Module ActiveDirectory
$OUName = "dsdsadasda"
if([bool] (Get-ADOrganizationalUnit -Filter * | ? {$_.Name -eq $OUName} ))
{ Write-Host 'true' }
else { Write-Host 'false' } 
false
Sergio Tanaka
  • 1,325
  • 1
  • 6
  • 18
  • 1
    It's best to [avoid the use of script blocks (`{ ... }`) as `-Filter` arguments](https://stackoverflow.com/a/44184818/45375). Also, it's inefficient to retrieve all OUs first and _then_ filter - see https://stackoverflow.com/a/54917665/45375 – mklement0 Mar 01 '19 at 02:40
  • 2
    @mklement0 makes a very goof point. If your in a large domain, this can waste minutes of time. Why bring in hundreds or even tens of thousands of data points when the desire is simply to see if one exists. Kind of like buying the entire grocery store just to see if they sell flour. –  Mar 03 '19 at 01:13