PowerCLI: Lesson #7 < I'm back in business
Ok I have to make my apologies about this section, it’s 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.
So now I have a copy I think it’s 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.
In this Lesson we’ll look at spaces, wildcards, escape characters and how they can be used in PowerCLI.
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 WinXP Test 1 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 WinXP Test 1 is one piece of text and will fail or will only use the first part of the text WinXP. To get around this just place the text in quotes, so the VM name would be inputted like so:
You can also use single quotes too ‘ but what if you need to input the quotes/single quote symbol as a legal character which is part of the VM name like: WinXP’1 well the answer is simple just surround the VM name with the other quote character. So in our case it would look like this:
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 ‘v’. To do that is very simple we just enter the asterisks symbol * after specifying the character we want to search on. Here’s an example where we list out only VMs that begin with the letter v:
And we are not just restricted to searching against 1 character. This next example shows we can query with 2 characters.
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 ‘l’ like so:
We can also use the [] wildcards to:
specify a range of characters Get-VM – name [m-z]Lab* which would list out any VMs that started with any letter from M to Z and ends with Lab
or
specify multiple characters Get-VM –name [uv]Lab* which would list out any VMs that started with letters U or V and ends with Lab
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.
Each new line is started with >> prompt but when you have finished just hit enter one more time on a empty line and the command will execute.
I hope you found this useful. And I promise to bring you more of these lessons in the very near future.




Leave a Reply
You must be logged in to post a comment.