I have a PSCustomObject with properties and values. My goal is to quickly put the values in an array. I am fairly new to powershell so I am unsure if this is something I can do in a single-line.
My object:
object
propA : valA
propB : valB
propC : valC
I want to get an array of just the values as fast as possible, Ideally this would not involve a loop because this process will be done thousands of times.
valArray
valA, valB, valC
I have tried a couple things. $object | ft -HideTableHeaders
$object | ft -HideTableHeaders
seems to get me close to what I want, but it does not appear to return an array
Without using a loop you could do this:
$valArray = ($object.PSObject.Properties).Value
to give you an array of values
ValA ValB ValC
Here is my approach following your not using a loop guideline, even though I would use a loop otherwise.
$object = [pscustomobject]@{
propA = 'ValA'
propB = 'ValB'
propC = 'ValC'
propD = 'ValD'
propE = 'ValE'
}
[array]$Array123 = (($object | get-member -MemberType NoteProperty).definition).split("=")
[array]$Array123 = [array]$Array123 -replace "^string.*$","" | ? {$_}
[array]$Array123
So I made a dummy object called $object. Then I used type accelerator [array] on $Array123 so powershell is aware I'm looking to create an array with my command on the right of the "=" symbol. I used get-member
on the object to get the NoteProperties, and then I'm splitting the definition object on the equal symbol.
This creates an array that looks like this.
string propA
ValA
string propB
ValB
string propC
ValC
string propD
ValD
string propE
ValE
Then I regex to remove the lines that begin with "string"... and lastly pipe to ? {$._}
? {$._}
in order to remove the lines we emptied out with our regex.
After our regex the array will look like so:
ValA
ValB
ValC
ValD
ValE
I am doubtful this is faster than a simple foreach loop, but it does fall under your no loop in the answers rule.
另一个单线:
$variable = $object[0..$object.Count]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.