Author Posts

September 1, 2015 at 1:02 pm

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?

September 1, 2015 at 2:18 pm

This should do it.

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

September 1, 2015 at 2:24 pm

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

September 1, 2015 at 2:53 pm

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.)

September 1, 2015 at 3:11 pm

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 } }

September 2, 2015 at 6:57 am

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!