PowerCLI 4.1 introduces easier way to initialize ManagedObjectRefernce (MoRef) instances which are necessary while playing with the Get-View cmdlet.
Till now there were several ways to get MoRefs. The most popular one is to get the view object and to use its MoRef property. Another approach is to parse the values of the ID into two parts – type and value.
For example, to convert virtual machine hard disks to thin-provisioned format it is necessary to have the target datastore MoRef:
1 |
<span style="color: #000000;"> </span><span style="color: #008000;">#</span><span style="color: #008000;"> getting the datastore view object, later we use its MoRef </span><span style="color: #008000;"> </span><br /> |
1 2 3 4 5 |
<span style="color: #000000;"> </span><span style="color: #800080;">$dsView</span><span style="color: #000000;"> </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> </span><span style="color: #5F9EA0; font-weight: bold;">Get-View</span><span style="color: #000000;"> </span><span style="color: #800080;">$datastore</span><span style="color: #000000;"> </span><span style="color: #800080;">$relocateSpec</span><span style="color: #000000;"> </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> </span><span style="color: #5F9EA0; font-weight: bold;">New-Object</span><span style="color: #000000;"> </span><span style="color: #800000;">VMware.Vim.VirtualMachineRelocateSpec</span><span style="color: #000000;"><strong> </strong></span><span style="color: #800080;"><strong>$relocateSpec</strong></span><span style="color: #000000;"><strong>.Datastore </strong></span><span style="color: #FF0000;"><strong>=</strong></span><span style="color: #000000;"><strong> </strong></span><span style="color: #800080;"><strong>$dsView</strong></span><span style="color: #000000;"><strong>.MoRef</strong> </span><span style="color: #800080;">$relocateSpec</span><span style="color: #000000;">.Transform </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">sparse</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="font-family: arial, helvetica, clean, sans-serif; white-space: normal; "><span style="font-size: 14px; "><span style="line-height: 17px;"><br /></span></span></span> |
1 |
<span style="font-family: arial, helvetica, clean, sans-serif; white-space: normal; "><span style="font-size: 14px; "><span style="line-height: 17px;">With PowerCLI 4.1, it </span>is possible to initialize MoRefs from the ID or UId property of the objects. For example you can do this</span>:</span> |
1 2 3 4 5 6 7 8 9 |
<span style="white-space: normal; "><span style="font-family: Courier;"> PS> $datastore.Id<br /> Datastore-datastore-12523<br /> PS> [VMware.Vim.ManagedObjectReference] $moRef = $datastore.Id<br /> PS> $moRef | ft -AutoSize<br /> Type      Value<br /> ----      -----<br /> Datastore datastore-12523<br /> </span></span> |
1 |
<span style="font-family: arial, helvetica, clean, sans-serif; line-height: 17px; white-space: normal; font-size: 14px; ">Because the Datastore property of the $relocateSpec is of ManagedOjbectReference type, you can rewrite the previous rows to look like this:</span><br /> |
1 2 3 |
<span style="color: #000000;"> </span><span style="color: #800080;">$relocateSpec</span><span style="color: #000000;"> </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> </span><span style="color: #5F9EA0; font-weight: bold;">New-Object</span><span style="color: #000000;"> </span><span style="color: #800000;">VMware.Vim.VirtualMachineRelocateSpec</span><span style="color: #000000;"> <strong> </strong></span><span style="color: #800080;"><strong>$relocateSpec</strong></span><span style="color: #000000;"><strong>.Datastore </strong></span><span style="color: #FF0000;"><strong>=</strong></span><span style="color: #000000;"><strong> </strong></span><span style="color: #800080;"><strong>$datastore</strong></span><span style="color: #000000;"><strong>.Id</strong> </span><span style="color: #800080;">$relocateSpec</span><span style="color: #000000;">.Transform </span><span style="color: #FF0000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">sparse</span><span style="color: #800000;">"</span> |
1 |
<span style="color: #000000;"><span style="font-family: arial, helvetica, clean, sans-serif; line-height: 17px; white-space: normal; font-size: 14px; ">Skipping the Get-VIew call makes the code shorter and the scripts faster, because you avoid a unnecessary server roundtrip.</span></span> |
1 |
<span style="font-family: arial, helvetica, clean, sans-serif; line-height: 17px; white-space: normal; font-size: 14px; ">Enjoy it! </span><br /> |