Powershell New ADOrganizationalUnit:服务器不愿意处理该请求

Powershell New ADOrganizationalUnit:服务器不愿意处理该请求,powershell,Powershell,我试图创建一个函数来检查OU是否存在,如果不存在,它将在AD中创建新的OU 但是,我收到一个错误“服务器不愿意处理请求”。我想这可能与我的人生道路有关,但我已经修修补补了,到目前为止还没有任何运气。在以前的结果中搜索错误代码会产生具有类似问题的代码,但是他们推荐的修复方法不起作用 function CreateOU ([string]$name, [string]$path, [string]$description) { $newOU = "OU=$name,$path"

我试图创建一个函数来检查OU是否存在,如果不存在,它将在AD中创建新的OU

但是,我收到一个错误“服务器不愿意处理请求”。我想这可能与我的人生道路有关,但我已经修修补补了,到目前为止还没有任何运气。在以前的结果中搜索错误代码会产生具有类似问题的代码,但是他们推荐的修复方法不起作用

function CreateOU ([string]$name, [string]$path, [string]$description) {
$newOU = "OU=$name,$path"

# Checking if the OU exists
try {
    Get-ADOrganizationalUnit -Identity $newOU | Out-Null
    Write-Verbose "OU '$newOU' already exists."
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
    Write-Verbose "New OU '$newOU has been created'"
    New-ADOrganizationalUnit -Name $name -Path $path -Description $description
}
}
CreateOU-名称“组”-路径“DC=ad,DC=example,DC=com”-描述“example description”

您是否只需要在Get ADOrg命令中添加
-ErrorAction Stop