Book Image

Learning PowerCLI for VMware VSphere

By : Robert van den Nieuwendijk
Book Image

Learning PowerCLI for VMware VSphere

By: Robert van den Nieuwendijk

Overview of this book

Table of Contents (17 chapters)
Learning PowerCLI
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating calculated properties


You can use the Select-Object cmdlet to select certain properties of the objects that you want to return. For example, you can use the following code to return the name and the used space, in GB, of your virtual machines:

PowerCLI C:\> Get-VM | Select-Object -Property Name,UsedSpaceGB

But what if you want to return the used space in MB? The PowerCLI VirtualMachineImpl object has no UsedSpaceMB property. This is where you can use a calculated property. A calculated property is a PowerShell hash table with two elements: Name and Expression. The Name element contains the name that you want to give the calculated property. The Expression element contains a scriptblock with PowerCLI code to calculate the value of the property. To return the name and the used space in MB for all of your virtual machines, run the following command:

PowerCLI C:\> Get-VM |
>> Select-Object -Property Name,
>> @{Name="UsedSpaceMB";Expression={1KB*$_.UsedSpaceGB}}
&gt...