【发布时间】: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 befalse`。
标签: powershell active-directory organizational-unit