Link to home
Start Free TrialLog in
Avatar of SquigglyMonkey
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
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!!
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

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 :)
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oBdA
oBdA

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

Avatar of SquigglyMonkey

ASKER

Thank you! Perfect