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?
This should do it.
Get-AdfsClaimsProviderTrust | ForEach-Object {"$($_.Name) $($_.ProtocolProfile) $([regex]::Matches($_.AcceptanceTransformRules,"@").Count)"}
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
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.)
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 } }
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!