none
Simple script not rereiving more than one attribute RRS feed

  • Question

  • Hi,

    There is something wrong with the last line in the following script but i'm not sure what it is?

    Any help please?

    Many thanks

    $strFilter = "(objectCategory=User)"

    $objDomain = New-Object System.DirectoryServices.DirectoryEntry

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $objDomain
    $objSearcher.PageSize = 1000
    $objSearcher.Filter = $strFilter
    $objSearcher.SearchScope = "Subtree"


    $colProplist = "name", "mail"
    foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i) > $NULL}

    $colResults = $objSearcher.FindAll()

    foreach ($objResult in $colResults)
        {$objItem = $objResult.Properties + "," & $objItem.name + "," & $objItem.mail}

    Wednesday, October 26, 2011 11:58 AM

Answers

  • Just pipe the output to csv:

    $ADObjects | Export-CSV ... etc.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Marked as answer by Beefstew123 Wednesday, October 26, 2011 1:55 PM
    Wednesday, October 26, 2011 1:50 PM

All replies

  • What error message are you getting?
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Wednesday, October 26, 2011 12:05 PM
  • Unexpected token '&' in expression or statement.
    At C:\Script\getusers1.ps1:18 char:46
    +     {$objItem = $objResult.Properties + "," & <<<<  $objItem.name + "," & $objItem.mail}
        + CategoryInfo          : ParserError: (&:String) [], ParseException
        + FullyQualifiedErrorId : UnexpectedToken
    Wednesday, October 26, 2011 12:08 PM
  • Yes, I didn't notice that.  We don't concatinate with the & operator in Powershell.  That means 'Execute".

    Try this:

    {$objItem = $objResult.Properties + "," + $objItem.name + "," + $objItem.mail}


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Wednesday, October 26, 2011 12:10 PM
  • I'm getting the following back

     

    You can add another hash table only to a hash table.
    At C:\Script\getusers1.ps1:19 char:40
    +     {$objItem = $objResult.Properties + <<<<  "," + $objItem.name + "," + $objItem.mail}
        + CategoryInfo          : InvalidOperation: (,:String) [], RuntimeException
        + FullyQualifiedErrorId : AddHashTableToNonHashTable

    This is it how it looks now

    $strFilter = "(objectCategory=User)"

    $objDomain = New-Object System.DirectoryServices.DirectoryEntry

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $objDomain
    $objSearcher.PageSize = 1000
    $objSearcher.Filter = $strFilter
    $objSearcher.SearchScope = "Subtree"


    $colProplist = "name", "mail"
    foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i) > $NULL}

    $colResults = $objSearcher.FindAll()

    foreach ($objResult in $colResults)

        {$objItem = $objResult.Properties + "," + $objItem.name + "," + $objItem.mail}

    Then i'm running the following but its not creating rows for each user? Is this the best way to do it do you think?

    .\getuser.ps1 | Out-File \\filename

    Wednesday, October 26, 2011 12:24 PM
  • I can't really see what you're trying to do.

    I see you reading all users from AD, and adding 2 custom properties to the results.  Then this line I don't understand:

      {$objItem = $objResult.Properties + "," + $objItem.name + "," + $objItem.mail}

    ...What are you doing here?  At this point, $objItem.name and .mail have no value as far as I can see.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Wednesday, October 26, 2011 12:34 PM
  • All i'm trying to do is collect some atributes for users and export them to a csv file.

    The only attributes I want exported for now are the name and smtp address.

    I'm obviously very green on this so learning as I go along 

     

    Cheers

    Wednesday, October 26, 2011 12:39 PM
  • AFAIK, the smtp address is not part of the AD schema.  You need to retrieve this with the EMC.
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Wednesday, October 26, 2011 12:46 PM
  • Sorry, I mean the EMS (I always get them mixed up).
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Wednesday, October 26, 2011 12:57 PM
  • I mean the mail attribute which does reside in the AD

    I'm trying to get this working for any AD attribute for now just to understand it

    Cheers

    Wednesday, October 26, 2011 1:04 PM
  • When the same question was asked recently, my suggestion was to use this in the ForEach loop:

     

    foreach ($objResult in $colResults)
    {
        $objItem = $objResult.Properties
        $objItem.name + "," + $objItem.mail + "," + $objItem.userPrincipalName
    }

    -----

     

    However, this version of your script does not add userPrincipalName, so that should be eliminated here.

     


    Richard Mueller - MVP Directory Services
    Wednesday, October 26, 2011 1:22 PM
    Moderator
  • Active Directory attributes are not straight-forward to get to.  Use this script to get all the user properties, and then work from there:

    (Thanks to JRV for this script:)

    $searcher=[adsisearcher]'(&(objectCategory=person)(objectClass=user))'
    $searcher.SearchRoot='LDAP://dc=somedomain,dc=com'
    $results=$searcher.FindAll()
    
    $ADObjects = @()
    foreach($result in $results)
    {
     [Array]$propertiesList = $result.Properties.PropertyNames
     $obj = New-Object PSObject
     foreach($property in $propertiesList)
     { 
      $obj | add-member -membertype noteproperty -name $property -value ([string]$result.Properties.Item($property))
     }
     $ADObjects += $obj
    }
    
    $ADObjects | fl *
    

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Edited by Bigteddy Wednesday, October 26, 2011 1:59 PM Credit jv
    Wednesday, October 26, 2011 1:23 PM
  • Thanks Bigteddy that does spit out all the info but I just need clearing up whether or not Powershell has the ability to place each attribute nicely into a csv, or if further work needs to be done within excel after the export has taken place?
    Wednesday, October 26, 2011 1:42 PM
  • Just pipe the output to csv:

    $ADObjects | Export-CSV ... etc.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Marked as answer by Beefstew123 Wednesday, October 26, 2011 1:55 PM
    Wednesday, October 26, 2011 1:50 PM
  • Lol, thats exactly what I was after... Thanks alot!

    So simple yet so hard for a newbie

    Wednesday, October 26, 2011 1:55 PM
  • I was a newbie about 4 months ago. 

    That script is courtesey of jrv, by the way.  I didn't write it, but I use it as a starting point for all AD queries, including for computer objects.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Wednesday, October 26, 2011 1:57 PM
  • Sorry, just one more thing, If I wanted to only report on only a couple of attributes instead of all of them as the script you suppied does, where would I state the attributes?

     

    Cheers

    Wednesday, October 26, 2011 2:25 PM
  • Or if you know of any good websites that shows how to do this that would be great
    Wednesday, October 26, 2011 2:26 PM
  • Sorry, just one more thing, If I wanted to only report on only a couple of attributes instead of all of them as the script you suppied does, where would I state the attributes?

     

    Cheers


    When you type:

    $AdObjects | Format-List *

    ...you will see all the properies common to each user object.

    Use Select-Object to filter certain attributes:

    $adObjects | select name, pwdlastset | Export-CSV ...etc
    


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Wednesday, October 26, 2011 2:37 PM
  • There is a newer and more complete version of that script in the repository here:
    http://gallery.technet.microsoft.com/scriptcenter/Extract-arbitrary-list-of-6f59d3b4

    It has help and some commenting on usage along with online examples.

     


    jv
    Wednesday, October 26, 2011 3:29 PM
  • Thanks jrv

    What would be your advice on the best way to go about getting an understanding to what is actually happening here?

    I mean, books maybe, or any particular websites that break down what is actually happening?

    Many thanks

    Thursday, October 27, 2011 11:26 AM
  • I'm trying to gain an understanding on what for example "Position=1" means.

    I have brought a couple of books but they do not really explain whats goming on too well and wondered if there is anything out there that does?

    Cheers

    Thursday, October 27, 2011 11:29 AM
  • help about_Functions_Advanced_Parameters

     


    jv
    Thursday, October 27, 2011 11:49 AM