使用外部参数调用[ADSI]

时间:2012-05-27 01:51:04

标签: powershell active-directory

我对PowerShell脚本编写不是很熟悉,而且我遇到了这个问题: 我需要对检索到的对象进行一些操作,如下所示:

$object = [ADSI]'LDAP://CN=Test User,OU=Dept,OU=Users,DC=example,DC=org'
...
$object.Commit()

这个工作正常,但我必须使用存储在变量中的专有名称 - 我的测试脚本看起来像这样,但它不起作用:

$object = [ADSI]'LDAP://$variable'
...
$object.Commit()

首次调用[ADSI]本身不会导致错误,但任何后续操作都会崩溃并显示消息:

The following exception occurred while retrieving member "commit": "The server is not operational.
"
At line:1 char:10
+ $object.commit <<<< ()
+ CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : CatchFromBaseGetMember

我很确定,参数是以某种错误的方式发送的,但我不知道,如何修复它,有人可以帮忙吗?

tahnks

1 个答案:

答案 0 :(得分:5)

尝试:

$object = [ADSI]"LDAP://$variable"

单引号不会扩展变量。