PowerShell InvokeGet the directory property cannot be found

╄→尐↘猪︶ㄣ 提交于 2021-02-05 11:44:23

问题


We needed to retrieve the information in active directory concerning 'Terminal Services'. For this I've created a function that works fine most of the time. However, with some users we have issues.

The code:

Function Get-ADTSProfile {
               [CmdletBinding()]
            Param(
                [Parameter(Mandatory=$true,Position=0)]
                [String] $DistinguishedName,
                [parameter(Mandatory=$true,Position=1)]
                [ValidateNotNullOrEmpty()]
                [ValidateSet('UserProfile','AllowLogon','HomeDirectory','HomeDrive')]
                [String]$Property
            )
            Begin {
                $User = [ADSI]"LDAP://$DistinguishedName"
            }
            Process {
                Switch ($Property) {
                    'AllowLogon'    {if ($($User.psbase.InvokeGet('allowLogon')) -eq '1'){$True}else{$False}}
                    'HomeDirectory' {$User.psbase.InvokeGet('TerminalServicesHomeDirectory')}
                    'HomeDrive'     {$User.psbase.InvokeGet('TerminalServicesHomeDrive')}
                    'UserProfile'   {$User.psbase.InvokeGet('TerminalServicesProfilePath')}
                }   
            }
        }

The error:

Get-ADTSProfile -DistinguishedName 'CN=test\, test (Den Bosch) NLD,OU=Users,OU=Disabled,OU=NLD,OU=EU,DC=domain,DC=net' -Property 'UserProfile'
Exception calling "InvokeGet" with "1" argument(s): "The directory property cannot be fo
und in the cache.
"
At S:\Test\Brecht\Testie.ps1:84 char:38
+                     'UserProfile'   {$User.psbase.InvokeGet('TerminalServicesPro ...
+                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodTargetInvocation

I can't really figure out why it works on some and not on all..


回答1:


I've been working on a recent project that uses ADSI to set and read Terminal Services attributes. From my testing anytime you perform a "InvokeGet({TS Attribute})" a COM exception will be thrown with the message "The directory property cannot be found in cache"

This seems to occur only when the "userParameters" attribute is not set in AD. Maybe the attribute internally checks the ADSI cache for userParameters? So i'm thinking logically you could check the DirectoryEntry for userParameters first, then try and read the properties, or else set it to construct the blob

if ($user.Properties.Contains("userParameters"))
{
 #Read the Property from ADSI
 Write-Host $user.InvokeGet("TerminalServicesProfilePath")
} else {
 #Set the property to construct the userParameter blob
 $user.InvokeSet("TerminalServicesProfilePath", "\\somepath")
 $user.CommitChanges()
}

Even if the userParameters attribute is not set, you can still perform an InvokeSet to have it constructed



来源:https://stackoverflow.com/questions/26160791/powershell-invokeget-the-directory-property-cannot-be-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!