Trim on array rows

This topic contains 5 replies, has 3 voices, and was last updated by  ssb 10 months, 2 weeks ago.

  • Author
  • #78982



    I have two arrays one comes from a sql stored procedure, another from ad group members. Then I use | out-string and @(). Each array has only one column.
    I try to trim the result array elements like this $c | foreach{ $_.Trim()} but this doesn't work.
    The only way it works is if I move the result to a file first, get the content and then trim (get-content C:\Temp\c.csv) | foreach{ $_.Trim()}
    Can anyone please tell me why?

    Thank you.

  • #78988


    1. How do you know the trimming isn't working?
    2. Can you show your code for how you're getting the AD group members, piping them to Out-String, and then trying to do the trimming?
    3. Can you give an example set of data before and after the trimming?
    4. What kind of file are you putting the text into before you then call Get-Content? Are you putting it into a Notepad txt file?

    I'm just guessing, but it sounds like maybe there are characters/formatting in the original data output (before the trim) that are being removed when you put the data into a file and, therefore, when you import the data back in to trim it, it's working. I'm curious to get the above answers so I can try to reproduce your issue.

    • #78991



      Here is what I am trying to do:

      $userssql = @(GetUserSqlFunctionCall | ft -HideTableHeaders | Select-Object -First 5 |Out-string) | foreach { $_.Trim() }

      $Usersad = @((Get-ADGroup -Identity "GroupName" -Properties Members).Members | Get-ADObject -Properties SamAccountName | ft -HideTableHeaders SamAccountName | Select-Object -first 5 | Out-string) | foreach { $_.Trim() }

      When I move the result of $userssql and $Usersad to a file I can visually see the extra spaces.
      If I do get content from the file and trim, the spaces are gone. (get-content C:\Temp\c.csv) | foreach{ $_.Trim()}

      Overall, I need to compare the two lists of users and get the differences.

  • #79009

    Max Kozlov

    get rid of format-table and out-string, use select-object -expandproperty

    you get multiline string and try to trim it instead of array of strings

    PS C:\> (" aaa `r`n bbb `r`n ccc ").Trim()
    PS C:\> (" aaa "," bbb "," ccc ").Trim()
    • #79025


      Thank you Max and Kevyn! This is exactly what I was looking for!

  • #79024


    I agree with Max. To get the AD member list, you might try the following:

    $Usersad = (Get-ADGroup -Identity "Stargate SG1" -Properties Members).Members | Get-ADObject -Properties SamAccountName | Select-Object -Property SamAccountName

    You should be able to do something similar to the results of your SQL query. Assuming all is well from there, you can look into using the Compare-Object cmdlet to compare the two lists.

    For example:

    $list1 = 'a','b','c'
    $list2 = 'b','c','d'
    Compare-Object $list1 $list2 -IncludeEqual
    InputObject SideIndicator
    ----------- -------------
    b           ==           
    c           ==           
    d           =>           
    a           < =

    Hope that helps.

You must be logged in to reply to this topic.