【问题标题】:Exception calling InvokeSet, The directory property cannot be found in the cache调用 InvokeSet 异常,在缓存中找不到目录属性
【发布时间】:2021-08-30 19:27:19
【问题描述】:

运行以下代码时,我收到错误 Exception calling "InvokeSet" with "2" argument(s): "The directory property cannot be found in the cache."在线出现此错误是因为变量为空的问题。使用 Write-Host 后,我​​得出的结论是 $User = [ADSI]$LdapUser 行是导致问题的行。

#Set Remote Control Settings Permissions 
        $LdapUser = "LDAP://" + (Get-ADUser $username).distinguishedName
        Write-Host $LdapUser
        $User = [ADSI]$LdapUser
        Write-Host $User
        $User.InvokeSet("EnableRemoteControl",5)
        $User.InvokeSet("TerminalServicesHomeDrive","U:\")
        $User.setinfo()

$LdapUser 的 Write-Host 命令显示 "LDAP://CN=Test Taco,CN=Users,DC=blah,DC=org",这是正确的,但是 $User 的 Write-Host 命令显示 System.DirectoryServices.DirectoryEntry 为什么$User 变量显示错误信息,如何解决?

【问题讨论】:

    标签: powershell ldap adsi


    【解决方案1】:

    write-host 只能接受一串文本。

    $User 不是字符串,它更像一个数组,因为它包含两行信息。它包含“distinguishedName”和“Path”。

    您可以输入:

    Write-Host $User.path
    Write-Host $User.distinguishedName
    

    问题是这个命令:

    $User.InvokeSet("EnableRemoteControl",5)
    

    你为什么使用 5?用 2 测试,它似乎工作

    $User.InvokeSet("EnableRemoteControl",2)
    

    【讨论】:

    • 我使用 5 还是 2 似乎对我没有任何改变。 Write-Host $User.Path 将为我提供 LDAP 路径,但是当与调用集一起使用时它将不起作用。相反,我收到错误“方法调用失败,因为 [System.String] 不包含名为“InvokeSet”的方法。 ```
    • [System.String]???听起来那个变量不再是“DirectoryEntry”。尝试像这样声明变量 [ADSI]$LdapUser = "LDAP://" + (Get-ADUser $username).distinguishedName,然后不要再次指定类 [ADSI]。
    • 查看这个在线资源,他们使用 $user.psbase.invokeSet (devblogs.microsoft.com/scripting/…)
    【解决方案2】:

    我的代码的问题是我使用数字 5 来启用远程控制以及使用 U:\ 正确的代码使用 2 并且没有反斜杠所以 U:

    #Set Remote Control Settings Permissions 
            $LdapUser = "LDAP://" + (Get-ADUser $username).distinguishedName
            $User = [ADSI]$LdapUser
            Write-Host $User
            $User.InvokeSet("EnableRemoteControl",2)
            $User.InvokeSet("TerminalServicesHomeDrive","U:")
            $User.setinfo()
    
    pause
    

    感谢@Judd Davey 帮助我完成他的回答,以了解我的回答可能出错的地方。我不太确定为什么它给了我这么奇怪的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 2017-10-13
      • 2019-02-18
      • 2022-11-15
      相关资源
      最近更新 更多