【问题标题】:Function that checks if an OU exists in Active Directory always returns false even if there is an existing OU即使存在现有 OU,检查 Active Directory 中是否存在 OU 的函数也始终返回 false
【发布时间】:2021-10-27 18:58:06
【问题描述】:

我正在尝试创建一个 powershell 函数来检查活动目录 OU 是否存在,并让它返回一个 true 或 false 值,以便与另一个函数结合使用来创建用户。我遇到的问题是它总是返回一个错误值,即使 OU 存在。

function checkIfOuExists{

param(

    [parameter(Mandatory)]

    [string]$nameOfOU

)

$existingOU = Get-ADOrganizationalUnit -Filter 'Name -like "$nameOfOU"'

if($existingOU -eq $null){
    return $false
}
else{
    return $true
}

}

【问题讨论】:

  • 交换引号,即将过滤器用双引号括起来,变量用单引号括起来。 "Name -like '$nameOfOU'"
  • 非常感谢!
  • 值得一提的是大部分代码都是多余的。如果 OU 不存在,请尝试输入 If ($ExistingOU) {Write-Host "Ou exists"). Your variable will automatically be false`。

标签: powershell active-directory organizational-unit


【解决方案1】:

无需为此编写函数 - 该行为是 Powershell 固有的:

$NameofOU = 'Users'
$existingOU = Get-ADOrganizationalUnit -Filter "Name -like '$nameOfOU'"


If ($existingOU) {
    Write-Host "Execute this code if OU exists"
}
Else {
    Write-Host "OU does not exist"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多