VMware Cloud Community
timgerr
Contributor
Contributor

Has to be a better way to get objects

Hello all, I am loving Powershell , but there has to be a better way to do stuff. I have a few questions for ya if ya will.

lets say I want to get the memory of a vm, I do this

$v = get-vm "VM Name" | get-view

$v.Config.Hardware.MemoryMB

so I now have my output. I see people using $_.Config.Hardware.MemoryMB, how and when do can I do that?

One more thing, how can I use the object to round my answer.

Thanks,

Tim

Tags (2)
0 Kudos
1 Reply
LucD
Leadership
Leadership

You should be aware that there are 2 types of objects that you can encounter while using PowerCLI.

First the objects produced by the PowerCLI cmdlets, let's call them the "automation objects".

If you look at the name of these objecttypes, you will notice that they end with the string "Impl".

For example:

PS C:\> Get-VM MyVM | gm


   TypeName: VMware.VimAutomation.Client20.VirtualMachineImpl

Name                    MemberType Definition
----                    ---------- ----------
Equals                  Method     System.Boolean Equals(Object obj)
GetHashCode             Method     System.Int32 GetHashCode()
GetType                 Method     System.Type GetType()
get_CDDrives            Method     VMware.VimAutomation.Types.CDDrive[] get_CDDrives()
get_CustomFields        Method     System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b...
get_Description         Method     System.String get_Description()
get_DrsAutomationLevel  Method     System.Nullable`1[[VMware.VimAutomation.Types.DrsAutomationLevel, VMware.VimAutomation.Types, Version=4.0.0.0, Cultur...
get_FloppyDrives        Method     VMware.VimAutomation.Types.FloppyDrive[] get_FloppyDrives()
get_Guest               Method     VMware.VimAutomation.Types.VMGuest get_Guest()
get_HAIsolationResponse Method     System.Nullable`1[[VMware.VimAutomation.Types.HAIsolationResponse, VMware.VimAutomation.Types, Version=4.0.0.0, Cultu...
get_HardDisks           Method     VMware.VimAutomation.Types.HardDisk[] get_HardDisks()
get_HARestartPriority   Method     System.Nullable`1[[VMware.VimAutomation.Types.HARestartPriority, VMware.VimAutomation.Types, Version=4.0.0.0, Culture...
get_Host                Method     VMware.VimAutomation.Types.VMHost get_Host()
get_HostId              Method     System.String get_HostId()
get_Id                  Method     System.String get_Id()

At the top of the of the Get-Member cmdlet (alias gm) output you see the name, VirtualMachineImpl in thise case.

The PowerCLI has populated these objects with a selection of the properties that are available in the VI/vSphere objects.

You will notice that the amount of memory is also available in this object

Get-Vm MyVM | Select MemoryMB

The second type of objects that are available are the VI/vSphere internal objects, let's call them the "SDK objects".

With the Get-View cmdlet you go from a "automation object" to a "SDK object".

PS C:\> Get-VM MyVM | Get-View | gm


   TypeName: VMware.Vim.VirtualMachine

Name                             MemberType Definition
----                             ---------- ----------
AcquireMksTicket                 Method     VMware.Vim.VirtualMachineMksTicket AcquireMksTicket()
AnswerVM                         Method     System.Void AnswerVM(String questionId, String answerChoice)
CheckCustomizationSpec           Method     System.Void CheckCustomizationSpec(CustomizationSpec spec)
CloneVM                          Method     VMware.Vim.ManagedObjectReference CloneVM(ManagedObjectReference folder, String name, VirtualMachineCloneSpe...
CloneVM_Task                     Method     VMware.Vim.ManagedObjectReference CloneVM_Task(ManagedObjectReference folder, String name, VirtualMachineClo...
CreateScreenshot                 Method     System.String CreateScreenshot()
....

These "SDK objects" are all documented in the VMware vSphere API Reference Documentation.

There is a lot more that can be said about these 2 types of objects but that would take this thread too far.

Have a look at Hal's book where all this is explained in much more detail.

For the 2nd part of your question, yes you have access to all the .Net functions when working in PowerShell.

You could for example use the Round function this way

PS C:\> $t = 12.3456
PS C:\> [math]::Round($t)
12
PS C:\> [math]::Round($t,1)
12,3


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

0 Kudos