0

我正在尝试创建一个 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
}

}
4

1 回答 1

0

无需为此编写函数 - 该行为是 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"
}
于 2021-08-28T07:56:28.713 回答