PowerShell测试组织单元


如果你的机器上安装了Microsoft RSAT工具,下面的脚本可以检查某个OU是否存在:

$OUPath = 'OU=TestOU,DC=train,DC=powershell,DC=local'
$exists = $(try { Get-ADOrganizationalUnit -Identity $OUPath -ErrorAction Ignore } catch{}) -ne $null
"$OUPath : $exists"

如果该OU存在,$exists变量的值为true,否则为false。注意,这里使用了  try/catch的异常处理:当组织单元不存在时,Get-ADOrganizationalUnit会抛出一个终止错误,而 try/catch正好可以捕获这个错误。

延伸阅读《再谈PowerShell终止与非终止错误》

原文连接:Testing Organizational Unit

本文链接: https://www.pstips.net/testing-organizational-unit.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注