2014-07-11 2 views
1

ADSI를 사용하여 변경하려는 특정 조직 단위의 사용자가 활성 디렉터리를 통해 검색하고 있습니다.ADSI LDAP 쿼리 사용하기

# 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 경로 (수동으로 테스트 한 경로)가 들어 있습니다.

그래서 내가 뭘 잘못하고 있니?.

답변

2

추가하기 전에 "LDAP : //"를 [ADSI]로 캐스팅하여 ADSI 개체에 추가하려고합니다.

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

당신이 서브 표현식에 연결을 할 필요가 있으므로 주조 작업의 연결 작업에 비해 higher precedence이 어느 같은 :

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

고양이 당신의 문자열을 먼저 한 후 캐스팅을

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

:

또는 같은 고유 이름은 여기서 자동으로 문자열로 변환되므로 수동으로 ToString()을 호출 할 필요가 없습니다.