Manipulating a property in a collection.

This topic contains 5 replies, has 3 voices, and was last updated by  Troy Helms 2 years ago.

  • Author
    Posts
  • #29189

    Troy Helms
    Participant

    I'm working with ADFS (which isn't really that important for this question). I'm running a command that pulls back a large number of properties. (Get-ADFSClaimsProviderTrust). In the result set, you have various properties that have several values stored in them, Name, ProtocolProfile, AcceptanceTransformRules, etc. I'm struggling to manipulate my results and output them in the way I want.

    Now, I know how to return all the results matching the Name, and how to select the various properties I want.

    What I don't know how to do is simply manipulate the data in the most efficient way. I can do a big function to run through it, but I was hoping for something efficient.

    For example, I'm wanting to return a output that lists the Name, ProtocolProfile, and instead of returning the AcceptanceTransFormRules themselves, return a couple of a specific character in that property.

    Now, I can do one with:
    ($_.AcceptanceTransformRules.ToCharArray() | Where-Object {$_ -eq '@'} | Measure-Object).Count)

    I was hoping for something where I could do it all in one pipeline. Does that make sense?

  • #29190

    Curtis Smith
    Participant

    This should do it.

    Get-AdfsClaimsProviderTrust | ForEach-Object {"$($_.Name) $($_.ProtocolProfile) $([regex]::Matches($_.AcceptanceTransformRules,"@").Count)"}
    
  • #29191

    Curtis Smith
    Participant

    The previous was a quick an dirty example. You could make it a little more fancy by putting those properties into a custom powershell object so that it could be easily piped out to another cmdlet for further manipulation such as exporting to CSV.

    Get-AdfsClaimsProviderTrust | ForEach-Object {New-Object -TypeName PSObject -Property @{Name = $_.Name; ProtocolProfile = $_.ProtocolProfile; CharacterCount = [regex]::Matches($_.AcceptanceTransformRules,"@").Count}}
    
    Get-AdfsClaimsProviderTrust | ForEach-Object {New-Object -TypeName PSObject -Property @{Name = $_.Name; ProtocolProfile = $_.ProtocolProfile; CharacterCount = [regex]::Matches($_.AcceptanceTransformRules,"@").Count}} | ConvertTo-CSV
    
  • #29192

    Tim Curwick
    Participant

    Troy,

    You can define a custom property with Select-Object.

    Get-AdfsClaimsProviderTrust | Select Name, ProtocolProfile, @{ Label = 'AtCount'; Expression = { $_.AcceptanceTransformRules.Split( "@" ).Count - 1 } }
    

    (Edited to fix typo. Thanks, Curtis.)

  • #29193

    Curtis Smith
    Participant

    Tim that is another nice way. One slight adjustment, you left out the AcceptanceTransformRules in your split. See updated below.

    Get-AdfsClaimsProviderTrust | Select Name, ProtocolProfile, @{ Label = 'AtCount'; Expression = { $_.AcceptanceTransformRules.Split( "@" ).Count - 1 } }
    
  • #29215

    Troy Helms
    Participant

    OMG That's simple and beautiful! Thanks so much! I write quite a few PowerShell scripts & functions, but I really need to learn better, cleaner ways to do it. This is EXACTLY what I needed! Thanks so much!

You must be logged in to reply to this topic.