Select statement from variables

This topic contains 8 replies, has 4 voices, and was last updated by  Scott Windmiller 1 year ago.

  • Author
    Posts
  • #55004

    Scott Windmiller
    Participant

    Simple question, why would this not work:

    $SelectARG = 'Name'
    $SelectARG += ', Samaccountname'

    get-aduser tuser | Select $SelectARG

    This is just a sample of something I am trying.
    $SelectARG does return: Name, Samaccountname
    so I am not sure why it does not work.

    Thanks,
    Scott

  • #55005

    Olaf Soyk
    Participant

    Try it this way:

    $SelectARG = 'Name','Samaccountname'
    Get-ADUser tuser | Select-Object $SelectARG

    Please read the following post: How to Format Code in the Forums

    • This reply was modified 1 year ago by  Olaf Soyk. Reason: added help
    • #55007

      Scott Windmiller
      Participant

      Sorry I should have been clearer.

      I separated them out because I was going to use IF statements to determine which properties I wanted returned, like:

      $SelectARG = 'Name'
      
      IF(Something) {
          $SelectARG += ', Samaccountname'
      }
      
      IF(Somethingelse) {
          $SelectARG += ', Surname'
      }
          get-aduser tuser | Select $SelectARG

      So I could wind up with:

      get-aduser tuser | Select Name

      or:

      get-aduser tuser | Select Name, Surname

      or:

      get-aduser tuser | Select Name, Samaccountname, Surname

      depending on the If statements.

      Does that make sense?
      Scott

    • #55009

      Olaf Soyk
      Participant
      IF(Something) {
          $SelectARG = 'Name','Samaccountname'
      }
      IF(Somethingelse) {
          $SelectARG = 'Name','Surname'
      }
      get-aduser tuser | Select $SelectARG

      Could you PLEASE format your code as code??

      • This reply was modified 1 year ago by  Olaf Soyk. Reason: added content
  • #55014

    Scott Windmiller
    Participant

    Sorry about that, it was not clear to me that the PRE button was the code button even through it's clearly stated 🙁

    The above would work but is there no way to construct the statement using +=?

    I have a gui with checkboxes for each property and I want to only show the properties for the checked properties.
    I thought the If statements would be what I needed. Using the += would just give me the properties I needed.

    • #55015

      Olaf Soyk
      Participant

      You will have to pass the properties you want as an array. Arrays have a fixed size when you create them. You could use following trick to 'create' a new array with the content of the old one added by something you want:

      $SelectARG = 'Name','Surname'
      $SelectARG = $SelectARG,'DisplayName'
  • #55016

    TomBaker
    Participant
    # @() is an array. 
    # Force Selectarg to be a 1 element array
    $SelectARG = @() + 'Name'
    
    # add samaccountname as a element to the array
    $SelectARG += 'Samaccountname'
    
    # now that $Selectarg is an [string[]] 
    # you can pass it to -property which accepts arrays
    get-aduser tuser | Select -property $SelectARG
    

    Your attempt wasn't working because you were creating a string containing commas.

    # this creates a string
    $SelectARG = 'Name'
    
    # this appends a comma space into the string
    $SelectARG += ', Samaccountname'
    
    #what you wrote was getting interpreted as
    Select -property 'Name, Samaccountname'
    
  • #55017

    Rob Simmers
    Participant

    Select(-Object) Property is expecting a string array. You can do something like this:

    $props = @()
    $props += "One"
    $props += "Two"
    $props += "Three"
    
    $props
    

    or

    [array]$props = "One"
    $props += "Two"
    $props += "Three"
    
    $props
    

    or

    $props = @("One")
    $props += "Two"
    $props += "Three"
    
    $props
    

    In your posted example, you were doing this:

    $props = "One"
    $props += ",Two"
    $props += ",Three"
    
    $props
    

    This is taking a string and then doing string concatenation. If you use $props.GetType(), you'll see that the last example will show as String:

    PS C:\Users\Rob> $props.GetType()
    
    
    IsPublic IsSerial Name                                     BaseType                                                                                                      
    -------- -------- ----                                     --------                                                                                                      
    True     True     String                                   System.Object
    
  • #55018

    Scott Windmiller
    Participant

    Ahhhh, I get it now. That makes perfect sense and works great.

    Thanks to all three of you, I am still learning.

    Have a great night,
    Scott

You must be logged in to reply to this topic.