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
Get-VM | where-object {$_.powerstate -eq "poweredon"} |Get-Annotation -CustomAttribute 'location','Host'

Open in new window

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!!
LVL 1
SquigglyMonkeyAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
You can turn the attribute names into properties:
Get-VM | Where-Object {$_.powerstate -eq "poweredon"} | Get-Annotation -CustomAttribute 'location', 'Host' |
	Group-Object -Property AnnotatedEntity |
	ForEach-Object {
		[pscustomobject]([ordered]@{
			AnnotatedEntity=$_.Name;
			'Location'=($_.Group | ? {$_.Name -eq 'Location'}).Value;
			'Host'=($_.Group | ? {$_.Name -eq 'Host'}).Value
		})
	}

Open in new window


Or as a one-liner:
Get-VM | Where-Object {$_.powerstate -eq "poweredon"} | Get-Annotation -CustomAttribute 'location', 'Host' | Group-Object -Property AnnotatedEntity | ForEach-Object {[pscustomobject]([ordered]@{AnnotatedEntity=$_.Name; 'Location'=($_.Group | ? {$_.Name -eq 'Location'}).Value; 'Host'=($_.Group | ? {$_.Name -eq 'Host'}).Value})}

Open in new window

1
 
Jose Gabriel Ortega CCEO J0rt3g4 Consulting ServicesCommented:
try

Get-VM | where-object {$_.powerstate -eq "poweredon"} |Get-Annotation -CustomAttribute 'location','Host'| Group-Object AnnotatedEntity

Open in new window

You should get something similar, try it and report to check how we can fix it :)
0
 
oBdACommented:
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-Annotation.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

Open in new window

[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
	}
}

Open in new window

0
 
SquigglyMonkeyAuthor Commented:
Thank you! Perfect
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.