Get-VMFolderPath to check in which vm folder our vm resides

Recently i wrote a function to check in which vm folder particular vm resides. I thought i could share it 😉 This function can output folder names or folder morefs when -moref switch is specified. Sample output would be this:
get-vm ‘vm123’ | get-vmfolderpath
MY DC1\vm\test123\ntest\btest\xtest\

Get-VMfolderPath (get-vm vm1).folderid
MY DC1\vm\test123\ntest\btest\xtest\

When you want to instead of names , folder morefs:

Get-VMfolderPath (get-vm vm1).folderid -moref or
get-vm ‘vm123’ |Get-VMfolderPath -moref
Datacenter-datacenter-242\Folder-group-v243\Folder-group-v740\Folder-group-v1434\Folder-group-v1435\Folder-group-v1436\
Continue reading

New-Folder in datacenter of vm type

I was trying to create a vm folder in Datacenter. This can not be achieved using new-folder directly without specifying proper location(as per MicaH comment) , because as per get-help from new-folder, this will result in creating a “yellow” host and cluster folder. In order to create a vm folder i had to use method CreateFolder(string)
Lets say i have a datacenter with name: “myDC” in which i want to create a folder “myTopLevelFolder”

(get-view (get-view -ViewType datacenter -Filter @{"name"="myDC"}).VmFolder).CreateFolder("myTopLevelFolder")

get-view -ViewType datacenter -Filter @{“name”=”myDC”}
We want to select only datacenter with name ‘myDC’
Then, we take the vmfolder from this datacenter
(get-view -ViewType datacenter -Filter @{“name”=”myDC”}).VmFolder
Then we create view from this:
get-view (get-view -ViewType datacenter -Filter @{“name”=”myDC”}).VmFolder
After that we can invoke method from this object using CreateFolder

It’s done 😉

How to check template settings without converting it to vm using get-teamplate and get-view?

So if we will look at a template vm using vSphere client we can’t see more than basic settings. We can check the guest OS, vm version, cpu, memory, vmware tools status, state, on which host was it registered, and on which datastore it resides. That’s it.
Ok, so in order to get some more information from template we can use get-view.

get-template vm2
-----
Name                Id
----                --
vm2            VirtualMachine-vm-738

Is this all ? Let’s see what else can we see using this view id

get-view -id (get-template vm2).id
-------
Capability           : VMware.Vim.VirtualMachineCapability
Config               : VMware.Vim.VirtualMachineConfigInfo
Layout               : VMware.Vim.VirtualMachineFileLayout
LayoutEx             : VMware.Vim.VirtualMachineFileLayoutEx
Storage              : VMware.Vim.VirtualMachineStorageInfo
EnvironmentBrowser   :
ResourcePool         :
ParentVApp           :
ResourceConfig       : VMware.Vim.ResourceConfigSpec
Runtime              : VMware.Vim.VirtualMachineRuntimeInfo
Guest                : VMware.Vim.GuestInfo
Summary              : VMware.Vim.VirtualMachineSummary
Datastore            : ...
Network              : {}
Snapshot             :
RootSnapshot         : {}
GuestHeartbeatStatus : gray
LinkedView           :
Parent               : Folder-group-v..0
CustomValue          : {..}
OverallStatus        : green
ConfigStatus         : green
ConfigIssue          : {}
EffectiveRole        : {-1}
Permission           : {}
Name                 : vm2
DisabledMethod       : {MakePrimaryVM_Task, TerminateFaultTol
RecentTask           : {}
DeclaredAlarmState   : {}
TriggeredAlarmState  : {}
AlarmActionsEnabled  : True
Tag                  : {}
Value                : {507}
AvailableField       : {}
MoRef                : VirtualMachine-vm-738
Client               : VMware.Vim.VimClient

So as you can see we can see similar output as if you would apply get-view to normal virtual machine. From here you can query your template just as it would be virtual machine. What is worth mentioning here is that it will be impossible to make any tasks on the template, but still we can get information.
So let’s check for example what network interfaces our templates has:

$templateview=get-view -id (get-template vm1).id
($templateview.config.hardware.device | ? {$_ -is [vmware.vim.VirtualVmxnet]})|%{$_.DeviceInfo}
--------
Label                      Summary
-----                      -------
Network adapter 1          VM Network

So we can see that we have 1 network adapter which is connected to ‘VM Network’ portgroup.
Now let’s see how were the disks configured

($templateview.config.hardware.device | ? {$_ -is [vmware.vim.VirtualDisk]})|%{$_.DeviceInfo}
---------
Label                         Summary
-----                         -------
Hard disk 1                   1,048,576 KB
Hard disk 2                   1,048,576 KB

Now again, we create some templates and we try to remind “have i put this floppy device or not ?” So normally you would have to mark template as a vm, then edit settings and so on..
Let’s check if i have put floppy to template:

($templateview.config.hardware.device | ? {$_ -is [vmware.vim.VirtualFloppy]})|%{$_.DeviceInfo}
--------
Label                                        Summary
-----                                        -------
Floppy drive 1                               Remote
Floppy drive 2                               Remote

Wow! Not one but two! 😉
Ok few times i have seen situation when people were surprised that some of vms have memory reservations/limit on them and they were wondering how ?! “i did not set it up”
Let’s see if we have some memory limits or reservations

$templateview.ResourceConfig.MemoryAllocation
----------
Reservation           : 100
ExpandableReservation : False
Limit                 : -1
Shares                : VMware.Vim.SharesInfo

Well it seems that my template has 100 mb memory reserved, and no limit set. You can query cpu allocation as well:

$templateview.ResourceConfig.CpuAllocation
----------
Reservation           : 0
ExpandableReservation : False
Limit                 : -1
Shares                : VMware.Vim.SharesInfo

What tools upgrade policy will my vms have after deployment ? We can also check this :

$templateview.config.tools.ToolsUpgradePolicy
----------
upgradeAtPowerCycle

Nice… And all this information AND MORE! we can grab without converting template to vm. So if you want really quickly take a look on some specific property of your template , get-view will help you out 😉

Adding roles with privileges from powercli

This will be very quick and short one 😉
We can easily create a role within our VC using the new-virole cmdlet. Since it accepts the privilege IDs as an array we can first fill up our array with privileges and then give it to new-virole.
In order to see from which we can run get-viprivilege to get all of them first. If you already know which one to include :

$privs_for_role=@(
'System.Anonymous',
'System.View',
'System.Read')
New-VIRole -Name custom_role1 -Privilege (Get-VIPrivilege -id $privs_for_role)

the -Privilege accepts only privileges, so we first need to get them using get-viprivilege and giving id name so we will receive privilege object.

Upgrade virtual machine hardware using powercli

Now, i know that there are some examples in internet about this but they were not working for me for some reason(still don’t know why).
UpgradeVM_Task SDK ref
So in order to upgrade vmware hardware version you have to execute method called UpgradeVM_Task from virtual machine object.

$myvmid=(get-vm 'myvm').id
(get-view -id $myvmid).UpgradeVM_Task('vmx-08') 

If you want to upgrade to version 7, then you have to use ‘vmx-07’. I am running this against vsphere 5 and it works very well. Now, reading manual it says:
Regarding the ‘Version’ parameter:
“If specified, upgrade to that specified version. If not specified, upgrade to the most current virtual hardware supported on the host.”
But if you will give $null or “” , or nothing, it will just not work. I hope i will get some more information why it behaves like this.
UPDATE 😉

$id=(get-vm 'myvm').id
$vmview=get-view -id $id
$param = @($null)
$vmview.gettype().GetMethod("UpgradeVM_Task").Invoke($vmview,$param)

This will run upgrade_VM_Task method without specifying the version string. It took me some time to figure this out 😉
$vmview.gettype().GetMethod(“UpgradeVM_Task”).GetParameters()

ParameterType : System.String
Name : version
DefaultValue :
RawDefaultValue :
Position : 0
Attributes : None
Member : VMware.Vim.ManagedObjectReference UpgradeVM_Task(System.String) -> 1 parameter

the parameter should be an array of 1 null here
$param=@($null)
as per
Invoke(System.Object obj, System.Object[] parameters)