Ana*_*urs 8 powershell active-directory windows-server-2008-r2
我正在尝试在创建OU之前检查是否存在OU.我的问题是我有2个母亲OU"USER BY SITE"和"GROUP BY SITE",我需要在那些2,1中存储完全相同的OU用于存储用户,另一个用于存储组.
到目前为止我使用了这个函数:
function CheckOUExist
{
param($OUToSeek)
$LDAPPath = "LDAP://dc=Domain,dc=local"
$seek = [System.DirectoryServices.DirectorySearcher]$LDAPPath
$seek.Filter = “(&(name=$OUToSeek)(objectCategory=organizationalunit))”
$Result = $seek.FindOne()
return $Result
}
Run Code Online (Sandbox Code Playgroud)
有我的问题,我总是在"GROUP BY SITE"中获得OU,即使$ LDAPPath ="OU = USERS BY SITE,DC = Domain,DC = local".我错过了什么吗?有没有办法让[System.DirectoryServices.DirectorySearcher]只在我在$ LDAPPath中给出的OU中工作?
如果您需要更准确的细节,我很乐意为您提供.
先感谢您.
Ver*_*Ray 13
如Shay所建议的那样,如果你正在处理干净的数据,那么它的效果很好.
[string] $Path = 'OU=test,DC=domain,DC=com'
[adsi]::Exists("LDAP://$Path")
Run Code Online (Sandbox Code Playgroud)
感谢这个伟大的起点!但是,如果您验证可能不清洁的数据,则会引发错误.可能的错误的一些示例是:
应该捕获所有这些错误,[System.Management.Automation.RuntimeException]
或者您可以将catch语句留空以捕获所有错误.
快速示例:
[string] $Path = 'OU=test,DC=domain,DC=com'
try {
$ou_exists = [adsi]::Exists("LDAP://$Path")
} catch {
# If invalid format, error is thrown.
Throw("Supplied Path is invalid.`n$_")
}
if (-not $ou_exists) {
Throw('Supplied Path does not exist.')
} else {
Write-Debug "Path Exists: $Path"
}
Run Code Online (Sandbox Code Playgroud)
更多详情:http: //go.vertigion.com/PowerShell-CheckingOUExists
Sha*_*evy 12
尝试Exists方法,分别得到true/false:
[adsi]::Exists("LDAP://OU=test,DC=domain,DC=com")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35035 次 |
最近记录: |