none
Why is a zero returned???? RRS feed

  • Question

  • Hi, at the top of this script a zero is always returned first, could anyone tell me why this is please and how to stop it

    Thanks

    $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"
    foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

    $colResults = $objSearcher.FindAll()

    foreach ($objResult in $colResults)
        {$objItem = $objResult.Properties; $objItem.name}

    Tuesday, October 25, 2011 12:08 PM

Answers

  • The PropertiesToLoad.Add method outputs a zero (for unknown reasons). You can pipe or redirect the output from the statement to $Null. For example:

    foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i) > $Null}


    Richard Mueller - MVP Directory Services
    • Marked as answer by Beefstew123 Tuesday, October 25, 2011 12:44 PM
    Tuesday, October 25, 2011 12:13 PM
    Moderator

All replies

  • The PropertiesToLoad.Add method outputs a zero (for unknown reasons). You can pipe or redirect the output from the statement to $Null. For example:

    foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i) > $Null}


    Richard Mueller - MVP Directory Services
    • Marked as answer by Beefstew123 Tuesday, October 25, 2011 12:44 PM
    Tuesday, October 25, 2011 12:13 PM
    Moderator
  • So basically it's just a quirk of some sort and nothing to worry about?

    Cheers

    Tuesday, October 25, 2011 12:15 PM
  • I thought it was something like the off by one error
    Tuesday, October 25, 2011 12:16 PM
  • The specialized collection types seem to do this. 

     The PropertiesToLoad property is of type System.Collections.Specialized.StringCollection.  When you add an element to this type of collection, if the add opertion is successful, it outputs the element number of the added element.

    I've noticed the same thing using system.collections.specialized.ordereddictionary.

     

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, October 25, 2011 12:39 PM
    Moderator
  • The Net collections add method returns the index of teh item being added, You need to consume this to keep it out of the output stream.  In compiled programs and at times in  PosH we may want to keep this value for use later on.

    You can also cast to null anything that you want to ignore:

    [void]$searcher.PropertiesToLoad.Add($i)

    I prefer using the  batch method.

    $properties=@('name','mail','sn','phone')
    $searcher.PropertiesToLoad.AddRange($properties)

    AddRange does not return a value amd can load all properties in one line.

    In PowerShell we generally try to lose the antiquated hungarian notaion since everything in PowerSHell is an object.  Drop the obj from your names and save yourself a lot of extra typing.

    For a more extensive demo of how to manipulate propertries on teh ADSI Searcher see:
    http://gallery.technet.microsoft.com/scriptcenter/Extract-arbitrary-list-of-6f59d3b4

     


    jv
    Tuesday, October 25, 2011 2:42 PM
  • Not all collection types seem to do this. 

    One noteable exception is the generic PSObject collection type:

    System.Collections.ObjectModel.Collection`1[[System.Management.Automation.PSObject, System.Management.Automation, Versi
    on=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, October 25, 2011 3:07 PM
    Moderator
  • Corrext but all can be cast to [void] or sent to Out-Null.  The point being that when adding something blindly it might dump something unwanted into your pipeline.

    In this case thogh PSObject is not a collection but is a decendant of System.Object.  When captured as  bunch it is generally seen as an array.

    [System.Management.Automation.PSObject[]]

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     PSObject[]                               System.Array

    System.Collection.Add returns void by  design but is overridden many times to return an int which is usually the insex of the addition.

     


    jv
    • Edited by jrv Tuesday, October 25, 2011 3:38 PM
    Tuesday, October 25, 2011 3:30 PM
  • PS:
    $searcher.PropertiesToLoad

    Is a very interesting and highly specialized type of collection.

    $searcher.PropertiesToLoad.GetType()

     


    jv
    • Edited by jrv Tuesday, October 25, 2011 3:44 PM
    Tuesday, October 25, 2011 3:44 PM
  • Invocation of a scriptblock using the .invoke() method will always produce that collection type. 

     Invocation using the invoke operator, or using the .invokereturnasis() method will return [object[]].

    The pipeline will always accumulate multiple returns as [object[]].


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, October 25, 2011 3:44 PM
    Moderator
  • Invocation of a scriptblock using the .invoke() method will always produce that collection type. 

     Invocation using the invoke operator, or using the .invokereturnasis() method will return [object[]].

    The pipeline will always accumulate multiple returns as [object[]].


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    That is interesting.  Do you know why?


    jv
    Tuesday, October 25, 2011 3:45 PM
  • I do not know why.  I just know that it always does.  Even an empty scrptblock will return a collection if you use the invoke method:

    PS C:\> ({}.invoke()).gettype()

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Collection`1                             System.Object

     


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, October 25, 2011 3:49 PM
    Moderator
  • It is just creqating a custom collection of 'type'.  The type must be extrapolated somehow.  Prehaps from the first object???

    In the case of PropertiesToLoad the custom type is StringCollection. This i may be chosen because it implement IList interface so the collecion can be reordered and edited dynamically.

    With ScriptBkock this is not clear.  It is so explicit that there must be some advantage to this deviation from the normal model.  Maybe there is something useful hidden here....

     

     


    jv
    Tuesday, October 25, 2011 4:02 PM
  • It's occasionally useful if you need the methods of a collection instead of an array.
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, October 25, 2011 4:10 PM
    Moderator
  • It's occasionally useful if you need the methods of a collection instead of an array.
    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    That is a good point.

     


    jv
    Tuesday, October 25, 2011 4:18 PM