Answered Powershell 2.0 For Dummies Code Error

  • samedi 5 mai 2012 01:35
     
     

    Hello I hope someone can answer this question. I bought Windows Powershell for Dummies today and I wanted to test out querying Active Director.

    In chapter 20 the first code example fails. Here is the code.

    $objADSI = [adsi]"" $domain = $objADSI.distinguishedname $userContainer = [adsi]("LDAP://cn=users," + $domain) foreach($child in $userContainer) {    Write-Host $child.samaccountname }

    \\ Seguis, Steve (2009-07-21). Windows PowerShell 2 For Dummies (Kindle Locations 6366-6373). John Wiley and Sons. Kindle Edition.

    The Error I get when I try to run this...

    Unexpected token 'domain' in expression or statement.
    At line:1 char:28
    + $objADSI = [adsi]"" $domain <<<<  = $objADSI.distinguishedname $userContainer = [adsi]("LDAP://cn=users," + $domain)
    foreach($child in $userContainer) {    Write-Host $child.samaccountname }
        + CategoryInfo          : ParserError: (domain:String) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : UnexpectedToken

    I checked the books website and no joy there, I also contacted their Tech support again no joy. I am running Powershell 2.0 on a Win7 box.

    I hope someone can help me.

    Thanks

Toutes les réponses

  • samedi 5 mai 2012 01:53
    Modérateur
     
     Traitée A du code

    The code appears to be run on one line. I believe it is actually 4 separate statements (line). It should be similar to:

    $objADSI = [adsi]""
    $domain = $objADSI.distinguishedname
    $userContainer = [adsi]("LDAP://cn=users," + $domain)
    foreach($child in $userContainer) {
        Write-Host $child.samaccountname
    }

    -----



    Richard Mueller - MVP Directory Services

    • Proposé comme réponse Al Dunbar samedi 5 mai 2012 04:45
    • Non proposé comme réponse Al Dunbar samedi 5 mai 2012 04:46
    • Marqué comme réponse TheFatman samedi 5 mai 2012 14:19
    •  
  • samedi 5 mai 2012 02:03
    Modérateur
     
     Traitée A du code

    I thought the code was a bit too brief, so I tested it. Of course it did not work. You need to specify children of the $userContainer object. Here is the corrected code:

    $objADSI = [adsi]""
    $domain = $objADSI.distinguishedname
    $userContainer = [adsi]("LDAP://cn=users," + $domain)
    foreach($child in $userContainer.children) {
        Write-Host $child.samaccountname
    }

    -----

    Even this assumes that all objects in the container have a sAMAccountName attribute. Any contact objects, for example, will not.


    Richard Mueller - MVP Directory Services

    • Proposé comme réponse Al Dunbar samedi 5 mai 2012 04:46
    • Marqué comme réponse TheFatman samedi 5 mai 2012 14:18
    •  
  • samedi 5 mai 2012 02:33
     
     Traitée

    Richard

    Thanks for your help. I have a lot to learn but now I know that I am on the right track with what I want to do.

    Thanks again

    • Marqué comme réponse TheFatman samedi 5 mai 2012 02:34
    •  
  • samedi 5 mai 2012 08:08
     
     

    Richard

    Thanks for your help. I have a lot to learn but now I know that I am on the right track with what I want to do.

    Thanks again

    Including how to use a forum.

    You are supposed to mark the answer that helped you as the answer and not just you own comments.  This is to help other people to  find answers.


    ¯\_(ツ)_/¯

  • samedi 5 mai 2012 14:18
     
     

    JRV

    I'm sorry I thought I marked it as answered when I clicked on the MARK AS ANSWERED green check box. Evidently I only marked my response as answered.

    I will correct that now. Thanks for pointing that out.

  • samedi 5 mai 2012 14:58
     
     

    JRV

    I'm sorry I thought I marked it as answered when I clicked on the MARK AS ANSWERED green check box. Evidently I only marked my response as answered.

    I will correct that now. Thanks for pointing that out.

    No problem.  Many new people do that.  The correctly marked answer is to help peple see th e solution quickly without haveing to go through the whole thread.

    Thank you.


    ¯\_(ツ)_/¯