none
PowerShell ADSI Provider Script not working RRS feed

  • Question

  • Hi everyone,

    I've found a post online that is doing something very similar to what I'm trying to accomplish.  The post that I'm referring to can be found here:

    https://eaglebcr.wordpress.com/2013/07/18/add-computer-to-ad-group-inside-task-sequence/

    Anyways, I corrected some syntax with some missing "" and cleaned it up a little bit.  However, I'm getting an error (I get the same error before cleaning up the syntax", but I don't have much experience with ADSI provider syntax.  Can someone help?

    Here's the code I've modified below:

    #Connect to Domain
    $objDomain = [adsi]"LDAP://OU=Foo,DC=Global,DC=Company,DC=Com"
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $objDomain
    $objSearcher.PageSize = 1000
    $objSearcher.Filter = "(&(objectClass=Computer)(name=$env:COMPUTERNAME))"
    $colResults = $objSearcher.FindOne()
    
    #Get Computer DN
    $CompDN = $colResults.properties.distinguishedname
    
    #Get objects
    $WSUSGroup = [ADSI]”LDAP://CN=GroupName,OU=Groups,OU=Foo,DC=Global,DC=Company,DC=Com”
    $Machine = [ADSI]"LDAP://$CompDN"
    
    #Add computer to group
    $WSUSGroup.Add($Machine.Path)
    $WSUSGroup.SetInfo()

    The error I get is:

    Exception calling "Add" with "1" argument(s): "Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))"
    At line:17 char:1
    + $WSUSGroup.Add($Machine.Path) #> $null
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

    Monday, March 9, 2015 10:34 PM

Answers

  • Why is it that everyone thinks they have to add more lines to every simple script.  It is really much easier when you understand what is being done.

    $searcher = [adsisearcher]"SamAccountName=$env:COMPUTERNAME$"
    if($machine=$searcher.FindOne()){
    	$WSUSGroup=[ADSI]”LDAP://CN=GroupName,OU=Groups,OU=Foo,DC=Global,DC=Company,DC=Com”
    	$WSUSGroup.Add($Machine.Path)
    }


    ¯\_(ツ)_/¯


    • Proposed as answer by jrv Monday, March 9, 2015 10:52 PM
    • Marked as answer by Thomas Garrity Tuesday, March 10, 2015 2:18 PM
    • Edited by jrv Tuesday, March 10, 2015 2:48 PM
    Monday, March 9, 2015 10:50 PM

All replies

  • Why is it that everyone thinks they have to add more lines to every simple script.  It is really much easier when you understand what is being done.

    $searcher = [adsisearcher]"SamAccountName=$env:COMPUTERNAME$"
    if($machine=$searcher.FindOne()){
    	$WSUSGroup=[ADSI]”LDAP://CN=GroupName,OU=Groups,OU=Foo,DC=Global,DC=Company,DC=Com”
    	$WSUSGroup.Add($Machine.Path)
    }


    ¯\_(ツ)_/¯


    • Proposed as answer by jrv Monday, March 9, 2015 10:52 PM
    • Marked as answer by Thomas Garrity Tuesday, March 10, 2015 2:18 PM
    • Edited by jrv Tuesday, March 10, 2015 2:48 PM
    Monday, March 9, 2015 10:50 PM
  • Brilliant, that worked!
    Tuesday, March 10, 2015 2:18 PM
  • Good luck.  Look into using less keystrokes.  Your keyboards will last longer. ;)

    ¯\_(ツ)_/¯

    Tuesday, March 10, 2015 2:49 PM