简体   繁体   中英

PowerShell InvokeGet the directory property cannot be found

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..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM