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
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
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
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!
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