I am getting a dataset back from a SSAS model via AdomdClient which gives me an object with properties that have names with square brackets in them. This is preventing me from being able to use Select-Object to only select a subset of properties (or more specifically to use calculated properties to rename them so that they match another dataset/obj)
I am aware of issues around using square brackets in file names and have tried escaping the brackets. Ideally I would like to learn of a powershell way to handle these properties, although any answers that explain a way to get the adomdclient to return the data back without the brackets would also be very helpful for my specific goal of renaming the properties.
This code snippet below can recreate what I am seeing without the SSAS/adomdclient dependencies. I am using powershell 5.1 for this code due to a assembly dependency:
$bracketedTestObj = New-Object PSObject -Property @{
"MyProperty[WithBrackets]" = "bracketedValue"
"MyPropertyWithoutBrackets" = "nonbracketedValue"
}
Write-host "The whole obj"
$bracketedTestObj | ft
Write-host "Select just the non bracketed property"
$bracketedTestObj | select-Object "MyPropertyWithoutBrackets" | ft
# I cannot figure out how to escape the [] and select only the bracketed property
Write-host "Select just the bracketed property"
$bracketedTestObj | select-Object "MyProperty[WithBrackets]" | ft
Write-host "Select just the bracketed property, 1 backtick"
$bracketedTestObj | select-Object "MyProperty`[WithBrackets`]" | ft
Write-host "Select just the bracketed property, 2 backtick"
$bracketedTestObj | select-Object "MyProperty``[WithBrackets``]" | ft
Write-host "Select just the bracketed property, 3 backtick"
$bracketedTestObj | select-Object "MyProperty```[WithBrackets```]" | ft
Write-host "Select just the bracketed property, 4 backtick"
$bracketedTestObj | select-Object "MyProperty````[WithBrackets````]" | ft
results (I did not include the results with more than ` backtick, but the result is the same:
The whole obj
MyPropertyWithoutBrackets MyProperty[WithBrackets]
------------------------- ------------------------
nonbracketedValue bracketedValue
Select just the non bracketed property
MyPropertyWithoutBrackets
-------------------------
nonbracketedValue
Select just the bracketed property
MyProperty[WithBrackets]
------------------------
Select just the bracketed property, 1 backtick
MyProperty[WithBrackets]
------------------------
To my best knowledge, there's no way to force
Select-Objectto not attempt to expand wildcard sequences in property names.Safest workaround is to use a calculated property that evaluates the target property on the input item:
To keep it DRY, you might want to write a small utility function to generate such calculated property definitions:
Now you can do:
And get the expected results: