{"id":239,"date":"2010-05-25T21:25:36","date_gmt":"2010-05-25T21:25:36","guid":{"rendered":"http:\/\/read.virtualizeplanet.com\/?p=239"},"modified":"2011-12-02T21:35:21","modified_gmt":"2011-12-02T21:35:21","slug":"powercli-lesson-6","status":"publish","type":"post","link":"http:\/\/read.virtualizeplanet.com\/?p=239","title":{"rendered":"PowerCLI: Lesson #6"},"content":{"rendered":"<p>This lesson I thought I&#8217;d talk about some realworld stuff.\u00a0 In my lab I had a need to use VMware DPM to reduce the heat my home servers where creating by shutting down 1 server while not needed. What I wanted though was to control at what point&#8217;s through the day DPM was enabled or disabled, and when disabled both my lab systems should be powered up.<\/p>\n<p>Asked for help on this one from Luc Dekens \u2013 <a href=\"http:\/\/www.lucd.info\/\">http:\/\/www.lucd.info<\/a> as control of DPM wasn&#8217;t so obvious and Luc pointed me to this:<\/p>\n<p><a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/initscript.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-241\" title=\"initscript\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/initscript.jpg\" alt=\"\" width=\"601\" height=\"90\" srcset=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/05\/initscript.jpg 601w, http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/05\/initscript-300x44.jpg 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/a><\/p>\n<p>I looked at it and thought uh! But then a few memories started to trigger in my brain. Having previously programmed the SDK in C# I recognized that this was calling the SDK direct some way. \u00a0So let&#8217;s break it down line by line an try and find out what&#8217;s going on:<\/p>\n<p><strong><em>$cluster = Get-Cluster &lt;clustername&gt; | Get-View<\/em><\/strong><\/p>\n<p>DPM is configured in the settings of a cluster. The line above declares a variable <strong><em>$cluster<\/em><\/strong> and then we pass in the results of the <strong><em>Get-Cluster<\/em><\/strong> cmdlet which in turn was piped through the <strong><em>Get-View<\/em><\/strong> cmdlet.<\/p>\n<p>The <strong><em>Get-Cluster<\/em><\/strong> part will return the status of the specified cluster but it&#8217;s not enough and this is where piping it into <strong><em>Get-View<\/em><\/strong> helps. This will return the fine detail about the cluster including its&#8217; MoRef.<\/p>\n<p><a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/Get-View.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-240\" title=\"Get-View\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/Get-View.jpg\" alt=\"\" width=\"497\" height=\"487\" srcset=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/05\/Get-View.jpg 497w, http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/05\/Get-View-300x293.jpg 300w, http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/05\/Get-View-50x50.jpg 50w\" sizes=\"auto, (max-width: 497px) 100vw, 497px\" \/><\/a><\/p>\n<p>In the script where we see <strong><em>VMware.Vim.~~~ <\/em><\/strong>this refers to the use of the VI SDK API.<\/p>\n<p>The next line sets up a custom specification of type: ClusterConfigSpecEx . A custom specification is an object which directs the SDK calls down the right road.<\/p>\n<p><strong><em>$spec = New-Object VMware.Vim.ClusterConfigSpecEx<\/em><\/strong><\/p>\n<p>So now <strong><em>$spec<\/em><\/strong> is an object in its own right with properties.<\/p>\n<p>The next thing we have to do is declare to .<strong><em>dpmconfig<\/em><\/strong> property of the <strong><em>$spec<\/em><\/strong> object as a ClusterDpmConfigInfo property.<\/p>\n<p><strong><em>$spec.dpmConfig = New-Object VMware.Vim.ClusterDpmConfigInfo<\/em><\/strong><\/p>\n<p>This will allow us to influence the properties of Clusters DPM properties.<\/p>\n<p>Now we even have properties of $spec.dpmconfig object and in our case it&#8217;s the enabled property which is of interest to us:<\/p>\n<p><strong><em>$spec.dpmConfig.enabled = $true<\/em><\/strong><\/p>\n<p>Changing this from <strong><em>$true<\/em><\/strong> to <strong><em>$false <\/em><\/strong>is the same as enabling and disabling DPM.<\/p>\n<p>The last line performs the changes.<\/p>\n<p><strong><em>$taskMoRef = $cluster.ReconfigureComputeResource_Task($spec, $true)<\/em><\/strong><\/p>\n<p>The $taskMoref is a pointer to the Task object which will perform the task based on the criteria. The criteria being \u00a0the cluster object . The task is delivered through method execution, in our case ReconfigureComputeResource. This method requires the parameters as specified in the $spec object.<\/p>\n<p>When there isn&#8217;t a relevant PowerCLI cmdlet you can fall back on calling the SDK APIs. In this example we are using the <a href=\"http:\/\/www.vmware.com\/support\/developer\/vc-sdk\/visdk400pubs\/ReferenceGuide\/vim.ComputeResource.html#reconfigureEx\">ReconfigureComputeResource<\/a> method that is available on the <a href=\"http:\/\/www.vmware.com\/support\/developer\/vc-sdk\/visdk400pubs\/ReferenceGuide\/vim.ComputeResource.html\">ComputeResource<\/a> managed object. One of the parameters for this method are passed through the <a href=\"http:\/\/www.vmware.com\/support\/developer\/vc-sdk\/visdk400pubs\/ReferenceGuide\/vim.cluster.ConfigSpecEx.html\">ClusterConfigSpecEx<\/a> object.<\/p>\n<p>Now this is a bit much to ingest for a beginner but this is something we are going to have to get used to over time.<\/p>\n<p>My best advice to you is study the following resources:<\/p>\n<p><a href=\"http:\/\/www.vmware.com\/pdf\/GettingStarted201.pdf\" target=\"_blank\">http:\/\/www.vmware.com\/pdf\/GettingStarted201.pdf<\/a><\/p>\n<p><a href=\"http:\/\/www.amazon.com\/gp\/product\/0137153635?ie=UTF8&amp;tag=doublecloudor-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0137153635\" target=\"_blank\">http:\/\/www.amazon.com\/gp\/product\/0137153635?ie=UTF8&amp;tag=doublecloudor-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0137153635<\/a><\/p>\n<p>Now back to the job in hand.<\/p>\n<p>We still need to do a bit more to fully automate this operation.<\/p>\n<p>I needed to execute the\u00a0scripts\u00a0I create by scheduling them. After saving my scripts (DPMOFF.ps1 &amp; DPMON.ps1) I used the windows scheduler (boooo, I know) to execute them at given times. To execute them, the scripts have to be passed into the <strong><em>powershell.exe<\/em><\/strong> command like so:<\/p>\n<p><strong><em>powershell.exe c:\\dpmon.ps1<\/em><\/strong><\/p>\n<p>But executing the script as shown above isn&#8217;t enough. It would fail in its current state. First of all we have to tell powershell to use the VMware API snapin with the following line:<\/p>\n<p><strong><em>Add-PSSnapin VMware.VimAutomation.Core<\/em><\/strong><\/p>\n<p>Next thing we need to do is connect to the relevant vCenter server so you don&#8217;t need to be there to manually connect:<\/p>\n<p><strong><em>Connect-VIServer -server vctest -user administrator -password passwd<\/em><\/strong><\/p>\n<p>Now on the DPMOFF (not DPMON.ps1) script I needed to make sure the hosts where out of stand-by mode:<\/p>\n<p><strong><em>Start-VMHost esx1.virtualplanet.local -RunAsync -Confirm:$false<\/em><\/strong><\/p>\n<p><strong><em>Start-VMHost esx2.virtualplanet.local -RunAsync -Confirm:$false<\/em><\/strong><\/p>\n<p>This starts each host, doesn&#8217;t prompt you for a &#8220;are you sure&#8221; question and doesn&#8217;t make the script wait until the hosts are powered.<\/p>\n<p>Job done.<\/p>\n<p>So the script looks like this:<\/p>\n<p><a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/script-finish.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-242\" title=\"script-finish\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/script-finish.jpg\" alt=\"\" width=\"607\" height=\"160\" srcset=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/05\/script-finish.jpg 607w, http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/05\/script-finish-300x79.jpg 300w\" sizes=\"auto, (max-width: 607px) 100vw, 607px\" \/><\/a><\/p>\n<p>That&#8217;s it for this lesson.<\/p>\n<p><a title=\"PowerCLI: Lesson#5\" href=\"http:\/\/read.virtualizeplanet.com\/?p=206\" target=\"_self\">Previous<\/a>\u00a0<a title=\"PowerCLI: Lesson #7 &lt; I\u2019m back in business\" href=\"http:\/\/read.virtualizeplanet.com\/?p=439\">Next<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This lesson I thought I&#8217;d talk about some realworld stuff.\u00a0 In my lab I had a need to use VMware DPM to reduce the heat my home servers where creating by shutting down 1 server while not needed. What I wanted though was to control at what point&#8217;s through the day DPM was enabled or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,4],"tags":[],"class_list":["post-239","post","type-post","status-publish","format-standard","hentry","category-powersehll","category-vmware"],"aioseo_notices":[],"views":10970,"_links":{"self":[{"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=239"}],"version-history":[{"count":9,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/239\/revisions"}],"predecessor-version":[{"id":588,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/239\/revisions\/588"}],"wp:attachment":[{"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=239"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}