I want to help everyone get more out of PowerCLI. So I am going to work through a short series on getting started with it, and doing a number of different things with it. This is more about being a consumer, rather than a developer. But it will get you going. I did this article a bunch of years ago in 2013. But this article is about how to do things in 2018. The install is a little different and so is the usage. PowerCLI is installed via the PowerShell Gallery, and is used from within PowerShell but scripts and commands are like they were in the past.
Why PowerCLI? Because it is easier to do some things, in an automated way, and that means less errors. As compared to doing a bunch of edits. Plus, there is a lot of help and examples out there. So for us, in this series, I am going to start with getting PowerCLI working, than setting NTP and Syslog parameters using a script. More likely to be done with no errors this way. But we will do some other things along the way that I think will be useful.
We are going to start with using Notepad++ as our editor so get those bits here. More and more people are using the ISE but starting with Notepad++ is good.
Now where to install. Generally there is two places. The first is your admin desktop. It is in fact a View desktop for me. But, there will be scripts that are executed on a schedule, and that is generally not good for your desktop. So the second place is where you will not normally log in and can use as a script execution location. In my lab it is on one of my utility servers. We will need this for one of the very special scripts we are going to get going in this series.
You need to make sure you have the latest PowerShell on your desktop. And you need to make sure you do not have the older PowerShell installed. So if you see something like below you need to remove it by uninstalling it.
So start PowerShell, and in fact I do it with a Run as Admin.
You will use the command below to install PowerCLI.
Install-Module -Name VMware.PowerCLI
If you wish to confirm that it is installed, use the command below.
Get-Module VMware* -ListAvailable
So we can find lots of VMware. We are good to go.
We have been doing this after the Run as Admin so that is good as any users can now use PowerCLI. Now you will enter the following command:
This will allow scripts you created or find and modify to be executed. But downloaded scripts will need to be signed. This does not mean copy and paste type downloaded scripts need to be signed. Once this is done it should like something like below.
We now have a working PowerCLI world. But there are some tweaks to make it better.
I use c:\scripts to keep all my scripts in. Both on my desktop and on the DC where I will do the scheduled script executions. We will create a PowerShell profile to make sure we always start in this folder. From within PowerCLI you should type the following:
New-Item -path $profile -type file -force
Now you can use the command below to actually open your profile and look at it.
Right now we only want to add one thing to it.
Now save the file and exit PowerShell. When you come back in you should be in that folder. I found out how to do this sort of thing from this here. If you want to make even more changes to your profile than check this out.
We need a nice tool to work with the scripts. There are a few around, but for now we are going to use Notepad++. I like it, and it has pretty colors so it works good and is helpful. Again I install with defaults.
Good things to know
You can work interactively in PowerShell. Meaning start it up, and type commands. You can also create scripts in Notepad++ and save them to the c:\scripts folder and execute them. I generally try to not work interactively but use scripts. If you work interactively remember you can use the TAB key to complete which is very helpful and can help avoid errors too.
We will start with working interactively. Once you are in PowerShell use the command below to connect to your vC.
Connect-VIServer -server vC_server_name_FQDN
You will see a bunch of stuff go by. Something like below. \
The thing to note is at the bottom and that is it shows that my account is now connected. Now lets find out what VMs this vC can see.
PowerShell is not case sensitive. I just like typing things like that. Now you should see something like below.
This is working interactively. If you have a script, you use .\script.ps1 to execute it.
We want to use scripts. Safe and more flexible. Plus, they can be smarter than we are! We will have an example of that later.
So this is the end of this Getting Started. Next one will be about using some scripts to accomplish real things.
- 1/14/18 – first release of article.