【问题标题】:Putting together an ADSI LDAP query组合 ADSI LDAP 查询
【发布时间】:2014-07-11 13:15:32
【问题描述】:

我正在活动目录中搜索特定组织单位下的用户,我想使用 ADSI 进行更改。

# get all users from the organizational unit
$accounts = Get-ADObject -filter 'objectClass -eq "user"' -SearchBase $dsn 

# iterate over user objects 
foreach ($account in $accounts) {
    # unfortunately we have to use ADSI over the set-aduser cmdlet as we neeed to touch remote desktop attribues
    $user = [ADSI]"LDAP://" + ($account.DistinguishedName).ToString()

    # get logon name 
    $SamAccountName = $user.psbase.InvokeGet("SamAccountName")

    # Profile Attributes
    $user.psbase.InvokeSet("ProfilePath", "")
    $user.psbase.InvokeSet("ScriptPath", "DIR\Logon.cmd")
    $user.psbase.InvokeSet("HomeDrive", "H:")
    $user.psbase.InvokeSet("HomeDirectory", "\\host\users$\${SamAccountName}")

    # Remote Desktop Services Attributes
    $user.psbase.InvokeSet("TerminalServicesProfilePath", "")
    $user.psbase.InvokeSet("TerminalServicesHomeDirectory", "\\host\users$\${SamAccountName}")
    $user.psbase.InvokeSet("TerminalServicesHomeDrive", "H:")

    # Write attributes back to global catalog
    $user.SetInfo()
}

这一切都很好,直到涉及到$user = [ADSI]"LDAP://" + ($account.DistinguishedName).ToString() 部分。

Method invocation failed because [System.DirectoryServices.DirectoryEntry] does not contain a method named 'op_Addition'.
At \\tsclient\D\SourceCode\PowerShell\Set-ADUserAttributes.ps1:37 char:5
+     $user = [ADSI]"LDAP://" + ($account.DistinguishedName).ToString()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Exception calling "InvokeGet" with "1" argument(s): "Unspecified error
"
At \\tsclient\D\SourceCode\PowerShell\Set-ADUserAttributes.ps1:40 char:5
+     $SamAccountName = $user.psbase.InvokeGet("SamAccountName")
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

似乎没有执行查询。但是,$account.DistinguishedName 包含正确的 LDAP 路径(我已经手动测试过)。

那么我在这里做错了什么?

【问题讨论】:

    标签: powershell adsi


    【解决方案1】:

    在执行追加之前,您试图通过将“LDAP://”转换为 [ADSI] 来追加到 ADSI 对象。

    先拉弦,然后进行演员阵容:

    $user = [ADSI]("LDAP://" + $account.DistinguishedName)
    

    【讨论】:

      【解决方案2】:

      转换操作比连接操作有higher precedence,所以你需要在子表达式中进行连接,或者这样:

      [adsi]("LDAP://" + $account.DistinguishedName)
      

      或者像这样:

      [adsi]"LDAP://$($account.DistinguishedName)"
      

      这里会自动将专有名称转换为字符串,因此您无需手动调用ToString()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-12
        • 1970-01-01
        • 2023-02-22
        相关资源
        最近更新 更多