SquigglyMonkey
asked on
powershell output to one line
I am running some powershell one liners (with vmware powercli) to look at data, here is what I am running
AnnotatedEntity Name Value
--------------- ---- -----
server1 location datacenterE
server1 host host1
server2 location datacenterW
server2 host host3
I would like to look like this:
AnnotatedEntity Name Value Name value
--------------- ---- ----- ------- ------
server1 host host1 location datacenterE
server2 host host3 location datacenterW
Thanks!!
Get-VM | where-object {$_.powerstate -eq "poweredon"} |Get-Annotation -CustomAttribute 'location','Host'
The output looks like this, and I also tried it in out-gridview, and export-csv all are like this.AnnotatedEntity Name Value
--------------- ---- -----
server1 location datacenterE
server1 host host1
server2 location datacenterW
server2 host host3
I would like to look like this:
AnnotatedEntity Name Value Name value
--------------- ---- ----- ------- ------
server1 host host1 location datacenterE
server2 host host3 location datacenterW
Thanks!!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For the fun of it, here's a script that will do everything automatically, even if you query for different attributes. Save it, for example, as C:\PS\ConvertFrom-Annotati on.ps1 or wherever, then pipe the results from Get-Annotation to it:
Get-VM | Where-Object {$_.powerstate -eq "poweredon"} | Get-Annotation -CustomAttribute 'location', 'Host' | C:\PS\ConvertFrom-Annotation.ps1
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[PSObject[]]$InputObject
)
Begin {
$AnnotationList = @()
}
Process {
$InputObject | ForEach-Object {$AnnotationList += $_}
}
End {
$CustomAttributes = $AnnotationList | Select-Object -ExpandProperty Name -Unique
$AnnotationList | Group-Object -Property 'AnnotatedEntity' | ForEach-Object {
$Result = '' | Select-Object -Property (@('AnnotatedEntity') + $CustomAttributes)
$Result.AnnotatedEntity = $_.Name
ForEach ($Attribute In $CustomAttributes) {
$Result.$Attribute = ($_.Group | Where-Object {$_.Name -eq $Attribute}).Value
}
$Result
}
}
ASKER
Thank you! Perfect
Open in new window
You should get something similar, try it and report to check how we can fix it :)