VMware Cloud Community
RyanMcL
Enthusiast
Enthusiast
Jump to solution

Get-Folder & Get-VM

These are probably only difficult for me as I am not that familar with powershell (Yet). But here we go two Questions here:

1) If I run 'Get-Folder "IT"' I get two folders returned one from the datastore view and one from the VMs&Templates View. How do I restrict this to just the one in the Template View?

This also applies to some folders in the same View; Depart1/Archive & Depart2/Archive. If I want to create a VM from the CLI how to I tell it which one to use?

2) $vm = Get-VM "X" returns the VM (shocking I know) Now I want specific properties to be returned (VM Attrributes) this dosnt show teh attributes that I want to access however '$vm = Get-VM "X" | Get-View' and then ' $vm2.AvailableField' sort of does, I only want one of the fields under AvailableField not all of them to be listed.

Ryan

0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, RyanMcL-

That CustomFields property is a "ReadOnlyDictionary" object, which consists of key/value pairs.  So, you can return the values by accessing the desired key.

In your example, "Creation Date", "Date to Retire", "Department", and "Status" are the keys.  To get the desired value here, access the "Department" key as such:

PS C:\> (Get-VM "X").CustomFields["Department"]

IT

Enjoy.

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

1) Use the Location parameter on the Get-Folder cmdlet, like this

Get-Folder IT -Location (Get-Folder vm)

or

Get-Folder IT -Location (Get-Folder datastore)

Note that 'vm' and 'datastore' are 2 hidden folders in the vSphere environment.

In fact you can specify any VI container object for the Location parameter

2) The Get-VM cmdlet by default doesn't show all the available properties

You can see those with

Get-VM X | Get-Member

If you find the property, you use the Select-Object cmdlet to display it.

Get-VM X | Select <selected-property>

The Get-View object is directly accessible through the Extensiondata property

Like this

$vm = Get-VM X

$vm.Extensiondata


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

RyanMcL
Enthusiast
Enthusiast
Jump to solution

wow, Thanks LucD.

Almost there, answer 2 isn't quite there yet:

> Get-VM "X" | select customfields

CustomFields
------------
{[Creation Date, ], [Date to Retire, ], [Department, IT], [Status, ]}

How do I access the Deparment Field?

Regards,

Ryan

0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello, RyanMcL-

That CustomFields property is a "ReadOnlyDictionary" object, which consists of key/value pairs.  So, you can return the values by accessing the desired key.

In your example, "Creation Date", "Date to Retire", "Department", and "Status" are the keys.  To get the desired value here, access the "Department" key as such:

PS C:\> (Get-VM "X").CustomFields["Department"]

IT

Enjoy.

0 Kudos