{"id":439,"date":"2011-05-21T21:55:49","date_gmt":"2011-05-21T20:55:49","guid":{"rendered":"http:\/\/read.virtualizeplanet.com\/?p=439"},"modified":"2011-12-02T21:28:51","modified_gmt":"2011-12-02T21:28:51","slug":"powercli-lesson-7-im-back-in-business","status":"publish","type":"post","link":"http:\/\/read.virtualizeplanet.com\/?p=439","title":{"rendered":"PowerCLI: Lesson #7 < I'm back in business"},"content":{"rendered":"<p>Ok I have to make my apologies about this section, it\u2019s been sometime that I have continued these PowerShell Lessons and the reason for that was I was waiting to get my hands on a new PowerCLI book.<\/p>\n<p>\u00a0<a href=\"http:\/\/www.amazon.co.uk\/VMware-VSphere-PowerCLI-Reference-Administration\/dp\/0470890797\/ref=sr_1_1?ie=UTF8&amp;qid=1306011109&amp;sr=8-1\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-440\" title=\"PowerCLI book\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2011\/05\/AlsBook.png\" alt=\"\" width=\"188\" height=\"238\" \/><\/a><\/p>\n<p>So now I have a copy I think it\u2019s time for me to once again start these lessons up again and hopefully learn a trick or two from this book. However I still plan to focus on core fundamental PowerShell code for now and try to drop in the odd PowerCLI usage.<\/p>\n<p>In this Lesson we\u2019ll look at spaces, wildcards, escape characters and how they can be used in PowerCLI.<\/p>\n<p>In PowerShell a space is seen as a delimiter between variables and parameters so a typical problem you may run into is when you are querying an object like a VM that has a space in the VM name. vSphere allows you to use the space character in the VM name for example <strong><em><span style=\"text-decoration: underline;\">WinXP Test 1<\/span><\/em><\/strong> is a legal name for a VM. However if you try to manipulate this VM buy inputting the VM name as is PowerShell will not know that <strong><em><span style=\"text-decoration: underline;\">WinXP Test 1<\/span><\/em><\/strong> is one piece of text and will fail or will only use the first part of the text <strong><em><span style=\"text-decoration: underline;\">WinXP.<\/span><\/em><\/strong><span style=\"text-decoration: underline;\"> <\/span>\u00a0To get around this just place the text in quotes, so the VM name would be inputted like so:<\/p>\n<p>\u00a0<a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2011\/05\/Get-VMQuotes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-444\" title=\"Get-VMQuotes\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2011\/05\/Get-VMQuotes.png\" alt=\"\" width=\"232\" height=\"19\" \/><\/a><\/p>\n<p>You can also use single quotes too \u2018 but what if you need to input the quotes\/single quote symbol as a legal character which is part of the VM name like: <strong><em><span style=\"text-decoration: underline;\">WinXP\u20191<\/span><\/em><\/strong> well the answer is simple just surround the VM name with the other quote character. So in our case it would look like this:<\/p>\n<p>\u00a0<a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2011\/05\/Get-VMQuote-in-Quotes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-443\" title=\"Get-VMQuote-in-Quotes\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2011\/05\/Get-VMQuote-in-Quotes.png\" alt=\"\" width=\"193\" height=\"17\" \/><\/a><\/p>\n<p>Ok so what about wildcards? If you were\/are familiar with DOS command-line or another shell environments like bash for example these shells often provide a way of performing a query on a group of object opposed to a single object that have common characters in the name of the object. So for example we may want to search the list of VMs that begin with the character \u2018v\u2019. To do that is very simple we just enter the asterisks symbol * after specifying the character we want to search on. Here\u2019s an example where we list out only VMs that begin with the letter v:<\/p>\n<p>\u00a0<a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2011\/05\/Get-VM-v.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-445\" title=\"Get-VM-v\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2011\/05\/Get-VM-v-300x113.png\" alt=\"\" width=\"300\" height=\"113\" srcset=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2011\/05\/Get-VM-v-300x113.png 300w, http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2011\/05\/Get-VM-v.png 429w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>And we are not just restricted to searching against 1 character. This next example shows we can query with 2 characters. \u00a0<\/p>\n<p>\u00a0<a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2011\/05\/Get-VM-vl.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-446\" title=\"Get-VM-vl\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2011\/05\/Get-VM-vl-300x59.png\" alt=\"\" width=\"300\" height=\"59\" srcset=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2011\/05\/Get-VM-vl-300x59.png 300w, http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2011\/05\/Get-VM-vl.png 427w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>We can use a different wildcards to specify that the query should list out only objects that have a letter at a specific position by using the ? wildcard. So in the above example we could have done the below to list out VMs where the second character is \u2018l\u2019 like so:<\/p>\n<p>\u00a0<a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2011\/05\/Get-VM-l.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-442\" title=\"Get-VM-l\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2011\/05\/Get-VM-l-300x62.png\" alt=\"\" width=\"300\" height=\"62\" srcset=\"http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2011\/05\/Get-VM-l-300x62.png 300w, http:\/\/read.virtualizeplanet.com\/wp-content\/uploads\/2011\/05\/Get-VM-l.png 422w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>We can also use the [] wildcards to:<\/p>\n<p>specify a range of characters <strong><em>Get-VM \u2013 name [m-z]Lab*<\/em><\/strong> which would list out any VMs that started with any letter from M to Z and ends with Lab<\/p>\n<p>or<\/p>\n<p>specify multiple characters <strong><em>Get-VM \u2013name [uv]Lab*<\/em><\/strong> which would list out any VMs that started with letters U or V and ends with Lab<\/p>\n<p>One last thing I want to show you is the use of the back tick symbol `. One thing PowerShell will allow you to do is escape out of the command line so the structure of the command line can be presented over multiple lines. So coders do this to make it easier to see how they form a command line. Instead of trying to do all on one line we can use the ` to bring us down a line. From here we enter more of the command line and if we want to start another new line we just hit enter.<\/p>\n<p>\u00a0<a href=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2011\/05\/Get-VM-backtick.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-441\" title=\"Get-VM-backtick\" src=\"http:\/\/virtualizeplanet.com\/wordpress\/wp-content\/uploads\/2011\/05\/Get-VM-backtick.png\" alt=\"\" width=\"99\" height=\"46\" \/><\/a><\/p>\n<p>Each new line is started with &gt;&gt; prompt but when you have finished just hit enter one more time on a empty line and the command will execute.<\/p>\n<p>I hope you found this useful. And I promise to bring you more of these lessons in the very near future.<\/p>\n<p><a title=\"PowerCLI: Lesson #6\" href=\"http:\/\/read.virtualizeplanet.com\/?p=239\">Previous<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ok I have to make my apologies about this section, it\u2019s been sometime that I have continued these PowerShell Lessons and the reason for that was I was waiting to get my hands on a new PowerCLI book. \u00a0 So now I have a copy I think it\u2019s time for me to once again start [&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-439","post","type-post","status-publish","format-standard","hentry","category-powersehll","category-vmware"],"aioseo_notices":[],"views":9491,"_links":{"self":[{"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/439","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=439"}],"version-history":[{"count":3,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/439\/revisions"}],"predecessor-version":[{"id":545,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=\/wp\/v2\/posts\/439\/revisions\/545"}],"wp:attachment":[{"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=439"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/read.virtualizeplanet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}