VMware Cloud Community
GrantOrchardVMw
Commander
Commander
Jump to solution

Unwanted data in output

Hey guys,

I'm running a basic VM report, per below.

$VMName = @{
    Name = "Virtual Machine"
    Expression = {$_.Name}
}
$NumCPU = @{
    Name = "CPUs"
    Expression = {$_.NumCPU}
}
$MemoryMB = @{
    Name = "Memory Allocation"
    Expression = {$_.MemoryMB}
}
$StorageCapacity = @{
    Name = "Storage Capacity"
    Expression = {Get-VM $_ | Get-VMGuest | Select-Object -expandproperty Disks | select Capacity}
}
#
Get-Datacenter datacentername | get-vm | select $VMName, $NumCPU, $MemoryMB, $StorageCapacity

This is working pretty well - apart from where I select $StorageCapacity. I'm getting "@{Capacity=" and then the capacity value all in the output. Can someone explain what I'm doing wrong?  I know my expression is a little nasty, I'm still learning the finer (basic!) points of powercli. A correction of the code would also be appreciated!

Thanks a lot,

Grant

Grant http://grantorchard.com
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That is because of the Select-Object cmdlet you have in there.

If you do it like this, you will not see the @{} construct

$VMName = @{
    Name = "Virtual Machine"
    Expression = {$_.Name}
}
$NumCPU = @{
    Name = "CPUs"
    Expression = {$_.NumCPU}
}
$MemoryMB = @{
    Name = "Memory Allocation"
    Expression = {$_.MemoryMB}
}
$StorageCapacity = @{
    Name = "Storage Capacity"
    Expression = {Get-VM $_ | Get-VMGuest | Select-Object -expandproperty Disks | %{$_.Capacity}}
}
#
Get-Datacenter datacentername | get-vm | select $VMName, $NumCPU, $MemoryMB, $StorageCapacity


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

That is because of the Select-Object cmdlet you have in there.

If you do it like this, you will not see the @{} construct

$VMName = @{
    Name = "Virtual Machine"
    Expression = {$_.Name}
}
$NumCPU = @{
    Name = "CPUs"
    Expression = {$_.NumCPU}
}
$MemoryMB = @{
    Name = "Memory Allocation"
    Expression = {$_.MemoryMB}
}
$StorageCapacity = @{
    Name = "Storage Capacity"
    Expression = {Get-VM $_ | Get-VMGuest | Select-Object -expandproperty Disks | %{$_.Capacity}}
}
#
Get-Datacenter datacentername | get-vm | select $VMName, $NumCPU, $MemoryMB, $StorageCapacity


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
GrantOrchardVMw
Commander
Commander
Jump to solution

Thanks Luc. Just to get my head around this, the problem is that Select-Object was picking up the whole object (Name and Value) and piping that into my Expression? Whereas using ForEach-Object utilising the current pipeline only inserts the value?

Thanks a lot,

Grant

PS Looking forward to your book!

Grant http://grantorchard.com
0 Kudos
admin
Immortal
Immortal
Jump to solution

Hi there,

Just noticed that there's redundant Get-VM call inside one of the expression statements.

In this expression there's no need to call Get-VM again, because you have it in $_ variable.

$StorageCapacity = @{
    Name = "Storage Capacity"
    Expression = { Get-VM $_ | Get-VMGuest | Select-Object -expandproperty Disks | %{$_.Capacity}}
}

Vitali

PowerCLI Team

0 Kudos