0

我想知道一个组织单位是否存在,所以我写了下面的代码,但它显示了一个错误:Impossible to find object with the identity OU_Bloquage.Despite it does really exist(I've created it) 下面是代码我写过

Import-Module ActiveDirectory
Import-Module 'Microsoft-PowerShell.Security'
$OUName = 'OU_Bloquage'
if([bool] (Get-ADOrganizationalUnit $OUName))
{ Write-Host 'true' }
else { Write-Host 'false' }
4

3 回答 3

1

@Bearded Brawler - 您很接近,但缺少其余问题的上下文。

反而:

$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' }

注意:这里假设 OU 实际上是“OU_Bloquage”,而不是“Bloquage”。如果它只是 Bloquage,那么编辑第一行以这样读取。

于 2019-02-28T17:17:12.357 回答
0

我会使用过滤器来查找您不确定完整路径的 OU

Get-ADOrganizationalUnit -Filter 'Name -like "*Bloquage*"' | 格式表名称,可分辨名称 -A

于 2019-02-28T16:06:36.253 回答
0

此代码应使用 Where-Object 进行过滤

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

结果:

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
于 2019-02-28T21:51:51.303 回答