{"id":206,"date":"2010-05-12T21:10:24","date_gmt":"2010-05-12T21:10:24","guid":{"rendered":"http:\/\/read.virtualizeplanet.com\/?p=206"},"modified":"2011-12-02T21:35:37","modified_gmt":"2011-12-02T21:35:37","slug":"powercli-lesson-5","status":"publish","type":"post","link":"http:\/\/read.virtualizeplanet.com\/?p=206","title":{"rendered":"PowerCLI: Lesson #5"},"content":{"rendered":"<p>Lesson # 5<\/p>\n<p>Ok if you look back at Lesson #3 we talked about a command to list out all the PowerCLI Get- cmdlets, which was <strong><em>&gt;gcm \u2013Module vmware* -verb get <\/em><\/strong>well let us come back to that now. Its traditional for PowerShell cmdlets to be constructed like so:<\/p>\n<p><strong><em>Verb-MeaningfulWord<\/em><\/strong> for example <strong><em>Get-VM<\/em><\/strong><\/p>\n<p>Well Get- isn\u2019t the only verb used in PowerShell, you\u2019ll also find:<\/p>\n<ul>\n<li><strong><em>Set-<\/em><\/strong><\/li>\n<li><strong><em>New-<\/em><\/strong><\/li>\n<li><strong><em>Add<\/em><\/strong>&#8211; \u00a0(there are only 3 of these in PowerCLI)<\/li>\n<\/ul>\n<p>We\u2019ll try and spend this lesson looking at some more PowerCLI cmdlets\u00a0 before spending more time on PowerShell fundamentals.<\/p>\n<p>So we know how to list all the PowerCLI cmdlets but how do we find out how to use them? Well we have different options.<\/p>\n<ol>\n<li>Read the help that comes with the command<\/li>\n<li>Refer to the Guru\u2019s :\n<ol>\n<li>Hal Rottenberg \u2013 <a href=\"http:\/\/halr9000.com\/\">http:\/\/halr9000.com<\/a><\/li>\n<li>Alan Renouf \u2013 <a href=\"http:\/\/www.virtu-al.ent\/\">http:\/\/www.virtu-al.ent<\/a><\/li>\n<li>Luc Dekens \u2013 <a href=\"http:\/\/www.lucd.info\/\">http:\/\/www.lucd.info<\/a><\/li>\n<\/ol>\n<\/li>\n<li>Or just plain old Google it.<\/li>\n<\/ol>\n<p>So I listed the PowerCLI <strong><em>Get-<\/em><\/strong> cmdlets and found a command <strong><em>Get-VMHost <\/em><\/strong><em> <\/em> and wanted to learn how to use it. So I probed its help by placing \u201c-?\u201d after the cmdlet like so:<\/p>\n<p><a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/Get-VMhost-help.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-210\" title=\"Get-VMhost-help\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/Get-VMhost-help.jpg\" alt=\"\" width=\"640\" height=\"528\" srcset=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/05\/Get-VMhost-help.jpg 640w, http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/05\/Get-VMhost-help-300x247.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>This is great to give you an insight in to the parameters to make the cmdlet work but I needed an example. If you\u00a0look at the bottom of the help output you see it provides you with a way of showing some examples:<\/p>\n<p><a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/Get-VMhost-eg.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-208\" title=\"Get-VMhost-eg\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/Get-VMhost-eg.jpg\" alt=\"\" width=\"638\" height=\"283\" srcset=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/05\/Get-VMhost-eg.jpg 638w, http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/05\/Get-VMhost-eg-300x133.jpg 300w\" sizes=\"auto, (max-width: 638px) 100vw, 638px\" \/><\/a><\/p>\n<p>From this information I was able to see that the <strong><em>Get-VMHost<\/em><\/strong> cmdlet is used for finding out which host a VM is running on within a specific datacenter. So I tried it for myself:<\/p>\n<p><a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/Get-VMhost-full.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-209\" title=\"Get-VMhost-full\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/Get-VMhost-full.jpg\" alt=\"\" width=\"580\" height=\"27\" \/><\/a><\/p>\n<p>Breakdown:<\/p>\n<p><a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/Get-VMhost-breakdwon.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-207\" title=\"Get-VMhost-breakdwon\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/05\/Get-VMhost-breakdwon.jpg\" alt=\"\" width=\"605\" height=\"185\" \/><\/a><\/p>\n<p>Explained:<\/p>\n<p>a)\u00a0\u00a0\u00a0\u00a0\u00a0 We run the <strong><em>Get-VMhost<\/em><\/strong> cmdlet<\/p>\n<p>b)\u00a0\u00a0\u00a0\u00a0\u00a0 Use the <strong><em>\u2013Location<\/em><\/strong> switch to pass in the name of the datacenter.<\/p>\n<p>c)\u00a0\u00a0\u00a0\u00a0\u00a0 We use the <strong><em>Get-Datacenter<\/em><\/strong> cmdlet to pass the object of the datacenter into the location parameter.<\/p>\n<p>d)\u00a0\u00a0\u00a0\u00a0\u00a0 Use the <strong><em>\u2013VM <\/em><\/strong>switch to pass in the name of the VM.<\/p>\n<p>e)\u00a0\u00a0\u00a0\u00a0\u00a0 We use the <strong><em>Get-VM<\/em><\/strong> cmdlet to pass the object of the VM into the VM parameter.<\/p>\n<p>And the result is the host system information that the VM \u201cVeeamRicky\u201d is running in.<\/p>\n<p>So you can see it didn\u2019t take much to substitute my lab information into the example to make the whole string of commands to work.<\/p>\n<p>Now following on from that, what if we don\u2019t know the Datacenter name! Well we can of course run the <strong><em>Get-Datacenter<\/em><\/strong> cmdlet which will enumerate the datacenter\u2019s associated with you vCenter. Now in my Lab I\u00a0only have one but if you have a few what you could do is store the enumeration in a variable.<\/p>\n<p><strong><em>&gt;$mydatacenters = Get-Datacenter<\/em><\/strong><\/p>\n<p>So if we looked into the variable we see server entries potentially that are indexed for example:<\/p>\n<p>[0] VeeamUKVsphereDatacentre1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Datacenter-datacenter-21<\/p>\n<p>[1] VeeamUKVsphereDatacentre2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Datacenter-datacenter-22<\/p>\n<p>[2] VeeamUKVsphereDatacentre3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Datacenter-datacenter-23<\/p>\n<p>Because this an enumeration (collection) then your variable is holding multiple entries. This format of a variable is known as an Array. A single entry can be referenced by call the variable with an index like so:<\/p>\n<p><strong><em>&gt;mydatacenter[1]<\/em><\/strong><\/p>\n<p><strong><em>&gt; Name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Id<\/em><\/strong><\/p>\n<p><strong><em>&#8212;-\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8212;<\/em><\/strong><\/p>\n<p><strong><em>VeeamUKVsphereDatacentre2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Datacenter-datacenter-22<\/em><\/strong><\/p>\n<p>So in our <strong><em>Get-VMHost<\/em><\/strong> cmdlet we can substitute the Datacenter query with a our variable like so:<\/p>\n<p><strong><em>&gt;Get-VMHost -location $mydatacenter[1] -VM (Get-VM -Name &#8220;VeeamRicky&#8221;)<\/em><\/strong><\/p>\n<p>And if you wanted to you could do the same with VM list retrieved with the <strong><em>Get-VM<\/em><\/strong> cmdlet<\/p>\n<p>OK let\u2019s break off there. Look out for Lesson6.<\/p>\n<p><a title=\"PowerCLI: Lesson#4\" href=\"http:\/\/read.virtualizeplanet.com\/?p=187\" target=\"_self\">Previous<\/a> &lt;&gt; <a title=\"PowerCLI: Lesson#6\" href=\"http:\/\/read.virtualizeplanet.com\/?p=239\" target=\"_self\">Next<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lesson # 5 Ok if you look back at Lesson #3 we talked about a command to list out all the PowerCLI Get- cmdlets, which was &gt;gcm \u2013Module vmware* -verb get well let us come back to that now. Its traditional for PowerShell cmdlets to be constructed like so: Verb-MeaningfulWord for example Get-VM Well Get- [&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-206","post","type-post","status-publish","format-standard","hentry","category-powersehll","category-vmware"],"aioseo_notices":[],"views":20884,"_links":{"self":[{"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/206","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=206"}],"version-history":[{"count":10,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/206\/revisions"}],"predecessor-version":[{"id":212,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/206\/revisions\/212"}],"wp:attachment":[{"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}