{"id":157,"date":"2010-04-21T22:37:46","date_gmt":"2010-04-21T22:37:46","guid":{"rendered":"http:\/\/read.virtualizeplanet.com\/?p=157"},"modified":"2011-12-02T21:35:49","modified_gmt":"2011-12-02T21:35:49","slug":"powercli-lesson2","status":"publish","type":"post","link":"https:\/\/read.virtualizeplanet.com\/?p=157","title":{"rendered":"PowerCLI: Lesson #2"},"content":{"rendered":"<p>As I go through this serious of lessons on PowerCLI\u00a0 I\u2019m going to try to expose the basics of PowerShell in the same lessons. 2 for the price 1. I\u2019m going to initially deliver them in very small nuggets scaling larger as we advance into more detailed stuff. So in this lesson I\u2019m going to look at a some core fundamentals of PowerShell whilst applying the theory to PowerCLI.<\/p>\n<p>First let\u2019s look at a process known in PowerShell as the \u201cpipeline\u201d.<\/p>\n<p>Most cmdlets (commands) provide some form of output when you run them, for example when we ran the <strong><em>Get-VM<\/em><\/strong> the output was a list of VMs and some basic information. With the \u201cpipeline\u201d function we can take this output and process it through a follow-up cmdlet. \u00a0This is done by placing the | character after the command then entering the follow-up command. \u00a0We can use another cmdlet <strong><em>Where-Object<\/em><\/strong> to filter out information\u00a0 from the properties of the previous cmdlet.<\/p>\n<p>Now we mentioned in lesson 1 that cmdlets have properties and methods and one of those properties found with the <strong><em>Get-VM<\/em><\/strong> cmdlet is PowerState\u00a0 which you can guess it is used to report the power state of a VM (powered off, on etc) . So to list all VMs that are powered off we would run the <strong><em>Get-VM<\/em><\/strong> cmdlet and pipe it into the <strong><em>Where-Object<\/em><\/strong> cmdlet and would look like this:<\/p>\n<p>\u00a0<a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/04\/GVM-WO.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-159\" title=\"GVM-WO\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/04\/GVM-WO.jpg\" alt=\"\" width=\"453\" height=\"19\" srcset=\"https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/04\/GVM-WO.jpg 453w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/04\/GVM-WO-300x12.jpg 300w\" sizes=\"auto, (max-width: 453px) 100vw, 453px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/04\/GVM-WO-Explained.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-160\" title=\"GVM-WO-Explained\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/04\/GVM-WO-Explained.jpg\" alt=\"\" width=\"602\" height=\"157\" srcset=\"https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/04\/GVM-WO-Explained.jpg 602w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/04\/GVM-WO-Explained-300x78.jpg 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/a><\/p>\n<p>\u00a0So when we execute that command we get a list of just the powered Off VMs:<\/p>\n<p>\u00a0<a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/04\/GVM-WO-list.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-161\" title=\"GVM-WO-list\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/04\/GVM-WO-list.jpg\" alt=\"\" width=\"450\" height=\"112\" srcset=\"https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/04\/GVM-WO-list.jpg 450w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/04\/GVM-WO-list-300x74.jpg 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/a><\/p>\n<p>Change the command to filter \u201cPoweredOn\u201d then we just get the powered On VMs:<\/p>\n<p>\u00a0<a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/04\/GVM-WO-list-ON.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-162\" title=\"GVM-WO-list-ON\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/04\/GVM-WO-list-ON.jpg\" alt=\"\" width=\"448\" height=\"138\" srcset=\"https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/04\/GVM-WO-list-ON.jpg 448w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/04\/GVM-WO-list-ON-300x92.jpg 300w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><\/a><\/p>\n<p>So how do we find out what properties of the <strong><em>Get-VM<\/em><\/strong> cmdlet we can filter on. Well like any cmdlet we find out what properties and methods (actions) are available by passing (piping) the cmdlet into a another cmdlet to find the results. If you pipe any cmdlet into the <strong><em>Get-Member<\/em><\/strong> cmdlet the output will be a list of properties and methods. You can also use a predefined alias (alias use to abbreviate cmdlets) for <strong><em>Get-Member <\/em><\/strong>which executed by entering <strong><em>gm <\/em><\/strong>. So to get the properties and methods of the <strong><em>Get-VM<\/em><\/strong> cmdlet you could use:<\/p>\n<p>\u00a0<a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/04\/GVM-GM.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-158\" title=\"GVM-GM\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/04\/GVM-GM.jpg\" alt=\"\" width=\"513\" height=\"432\" srcset=\"https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/04\/GVM-GM.jpg 513w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/04\/GVM-GM-300x252.jpg 300w\" sizes=\"auto, (max-width: 513px) 100vw, 513px\" \/><\/a><\/p>\n<p>So let\u2019s take this idea one step further, let\u2019s use a cmdlet <strong><em>Start-VM<\/em><\/strong> and I bet you can guess what that does&#8230; Let\u2019s use it to start all the powered off VMs by piping what we know into it like this:<\/p>\n<p>\u00a0<a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/04\/GVM-WO-SVM.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-163\" title=\"GVM-WO-SVM\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2010\/04\/GVM-WO-SVM.jpg\" alt=\"\" width=\"544\" height=\"21\" srcset=\"https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/04\/GVM-WO-SVM.jpg 544w, https:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2010\/04\/GVM-WO-SVM-300x11.jpg 300w\" sizes=\"auto, (max-width: 544px) 100vw, 544px\" \/><\/a><\/p>\n<p>Which will start all the VMs that have the Power State of Powered Off.<\/p>\n<p>Ok last thing I want to do here is just explain something about piping. The result of the <strong><em>Get-VM<\/em><\/strong> although look like a load of text is actually creating a list of objects (VMs in this case) . So the <strong><em>Start-VM <\/em><\/strong>cmdlet isn\u2019t actually trying to start a VM called \u201cSCOM PoweredOff\u00a0 1\u00a0 256\u201d it just takes the Name property value as the reference for the VM which in this case is \u201cSCOM\u201d and starts a VM with the same name.<\/p>\n<p>In a later lesson I\u2019ll try an explain Objects as PowerShell is an object orientated scripting language so it\u2019s very important but for now look out for lesson#3.<\/p>\n<p><a title=\"PowerCLI: Lesson #1\" href=\"http:\/\/read.virtualizeplanet.com\/?p=138\">Previous<\/a> &#8211;<a title=\"PowerCLI: Lesson #3\" href=\"http:\/\/read.virtualizeplanet.com\/?p=170\"> Next<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I go through this serious of lessons on PowerCLI\u00a0 I\u2019m going to try to expose the basics of PowerShell in the same lessons. 2 for the price 1. I\u2019m going to initially deliver them in very small nuggets scaling larger as we advance into more detailed stuff. So in this lesson I\u2019m going to [&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-157","post","type-post","status-publish","format-standard","hentry","category-powersehll","category-vmware"],"aioseo_notices":[],"views":49242,"_links":{"self":[{"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=157"}],"version-history":[{"count":7,"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":165,"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/157\/revisions\/165"}],"wp:attachment":[{"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}