PowerShell InvokeGet找不到目录属性
我们需要在active directory中检索有关“终端服务”的信息。为此,我创建了一个在大多数情况下都能正常工作的函数。但是,对于一些用户,我们有一些问题 代码:PowerShell InvokeGet找不到目录属性,powershell,Powershell,我们需要在active directory中检索有关“终端服务”的信息。为此,我创建了一个在大多数情况下都能正常工作的函数。但是,对于一些用户,我们有一些问题 代码: Function Get-ADTSProfile { [CmdletBinding()] Param( [Parameter(Mandatory=$true,Position=0)] [String] $Disti
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')}
}
}
}
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
错误:
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')}
}
}
}
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
我真的不明白为什么它在某些情况下有效,而不是在所有情况下有效。我最近一直在从事一个项目,该项目使用ADSI设置和读取终端服务属性。在我的测试中,每当您执行“InvokeGet({TS Attribute})”时,都会抛出一个COM异常,并显示消息“在缓存中找不到目录属性” 这似乎仅在AD中未设置“userParameters”属性时才会发生。该属性可能会在内部检查ADSI缓存中的userParameters?因此,我认为从逻辑上讲,您可以先检查DirectoryEntry的用户参数,然后尝试读取属性,或者将其设置为构造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()
}
即使未设置userParameters属性,您仍然可以执行InvokeSet来构造它这似乎只发生在“用户配置文件”为空且以前从未填写过的用户对象上。可以说,这只会发生在没有“用户档案”设置的全新用户身上。需要弄清楚如何在这种情况下实现空白结果而不是错误..非常奇怪,我有一个用户,其中“允许登录到终端服务器”被选中,但它仍然给出一个错误。但是对于其他一些同样使用它的用户来说,这能帮到你吗?感谢CB,试图找出它,因为这个例子在C++中有点难以理解。似乎是相关的,它谈到了GetEx(),但我不知道如何使用它。它只对从未设置过此属性的用户失败。如果执行`(Get QadUser test).TSAllowLogon-eq$null`则返回
true
。AD中有许多参数具有这种行为,您必须在检索值之前对$null进行jest测试。希望能有帮助!