admin管理员组文章数量:1191814
I want to store various outputs from Invoke-SSHCommands as objects.
Let's say i get the result formatted as CSV and i want to convert it to PSObject but the difficulty for me is that:
- Not the header but the first column should be interpreted as property names.
- The header names are always different.
The output from ssh looks like this but the header and values can differ depending of the ssh command:
PS> $mySSHResult
attribute |value
---------------------+-----
hostname |foo
version |1.1
variant_id |bar
I allready have converted the output to an Object[] with ConvertFrom-CSV
but that's not practical for me. It looks like this:
PS> $mySSHResult | ForEach-Object {$_ -replace '\s*\|\s*', '|'} | ConvertFrom-Csv -Delimiter "|" | select -Skip 1
attribute value
--------- -----
hostname foo
version 1.1
variant_id bar
What i want is the value of the first column as property name of the object. It should look like this:
PS> $myObject
hostname version variant_id
-------- ------- ----------
foo 1.1 bar
I tried my best with foreach()
and | foreach-object
but i'm to dumb too figure it out.
Can somebody help me?
I want to store various outputs from Invoke-SSHCommands as objects.
Let's say i get the result formatted as CSV and i want to convert it to PSObject but the difficulty for me is that:
- Not the header but the first column should be interpreted as property names.
- The header names are always different.
The output from ssh looks like this but the header and values can differ depending of the ssh command:
PS> $mySSHResult
attribute |value
---------------------+-----
hostname |foo
version |1.1
variant_id |bar
I allready have converted the output to an Object[] with ConvertFrom-CSV
but that's not practical for me. It looks like this:
PS> $mySSHResult | ForEach-Object {$_ -replace '\s*\|\s*', '|'} | ConvertFrom-Csv -Delimiter "|" | select -Skip 1
attribute value
--------- -----
hostname foo
version 1.1
variant_id bar
What i want is the value of the first column as property name of the object. It should look like this:
PS> $myObject
hostname version variant_id
-------- ------- ----------
foo 1.1 bar
I tried my best with foreach()
and | foreach-object
but i'm to dumb too figure it out.
Can somebody help me?
- Just for grins, have you tried $mySSHresult | format-list This is not an answer, but it may help you find an answer. – Walter Mitty Commented Jan 26 at 11:24
2 Answers
Reset to default 0You could use ConvertFrom-StringData
here, if you manage to replace pipes |
with an =
that could work. You will also need to skip the first 3 or 2 lines (unclear), so the headers.
Assuming you have the sample:
$mySSHResult = @'
attribute |value
---------------------+-----
hostname |foo
version |1.1
variant_id |bar
'@ -split '\r?\n'
Here you would use skip 3, since the output starts with a blank line, then for every line you can replace the |
with an =
and from there you convert all the lines into a single string with Out-String
, this is so ConvertFrom-StringData
outputs a single hashtable instead of an array of hashtables and lastly you can convert it to a psobject:
$data = $mySSHResult |
Select-Object -Skip 3 |
ForEach-Object { $_.Replace('|', '=') } |
Out-String |
ConvertFrom-StringData
[pscustomobject] $data
Another option can be via split by |
and trim the white space then populate an ordered dictionary and lastly convert it to a psobject:
$data = [ordered]@{}
$mySSHResult |
Select-Object -Skip 3 |
ForEach-Object {
$key, $value = $_.Split('|').Trim()
$data[$key] = $value
}
[pscustomobject] $data
Working under the Spheric Cow assumption of the SSH always returning tables with only 2 columns
$mySSHResult = @'
attribute |value
---------------------+-----
hostname |foo
version |1.1
variant_id |bar
'@
# Split the incoming table by NewLine.
$CustomCsvObject = $mySSHResult -split [System.Environment]::NewLine |
# Skip the first 3 lines. Test it's always the same number.
Select-Object -Skip 3 |
# convert to a PSObject array using customized header names.
ConvertFrom-Csv -Delimiter '|' -Header 'Header', 'Value'
# Make a new empty hashtable.
# We could make a PSObject directly, but adding elements to a Hashtable and then
# converting it is MUCH faster and lighter than adding members to a PSObject.
$NewTable = [hashtable]::new()
# Add the various elements.
foreach ($Line in $CustomCsvObject) {
$NewTable[$Line.Header.Trim()] = $Line.Value.Trim()
}
# Convert to a PSCustomObject.
$newobject = [pscustomobject]$NewTable
$newobject
results into
variant_id hostname version
----------- --------- --------
bar foo 1.1
本文标签:
版权声明:本文标题:powershell - How to convert CSV or Object-Array to an object but with the first column as properties - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738374932a2083193.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论