Powercli Basics Part 1

We all have to start somewhere so what better place than the basics. I kept jumping into complex scripts without working out the basics. For years I have written scripts in shell or perl but I have never spent much time in PowerShell. This is a few basic items to get your started with PowerCLI for Vmware.

What can you do with PowerCLI?

Just about anything you can do in the command line and a lot more. It allows full access to Vmware in every respect. It can be used to automate tasks, produce reports or resolve problems.

Why do we want to automate tasks and produce reports we can do that manually?

This has been suggested more than once. If you have to do it more than once it should be automated why? It reduces to human error risk factor and produces repeatable results everytime. It also helps you think about the process before you do it reducing missed steps. Automation is the key to more time as an administrator and less problems.

First thing first:
Your first step into the world of PowerCLI is installing it. You can download it from vmware’s site for free and you should update it everytime a new version of ESX comes out. Each updated version gets more and more functionality and commands. Make sure you run the installer as Administrator. The next step is to change the execution policy. Microsoft felt that it was a good idea to have all Powershell code signed off by a third party before allowing it to run. This eliminates the chance that someone’s virus could be run. It also makes it impossible for your to run your code so you need to change it.

Changing execution policy
1. Log into Powershell by right clicking on it and selecting run as Administrator
2. Run the following command:

set-executionpolicy remotesigned

3. Anwer yes to the question

Now you need to connect to your first vmware node with PowerCLI.   Right click on powercli on your desktop and select run as administrator.  Once it loads up you need to connect to either vsphere or an esx host with the following command:

connect-viserver IP_address

It has a series of questions which can be answered on the command line or via the prompt (like your login information) and your connected to your host / vcenter.

You can now issue your first powercli command to locate some information about your host or hosts:

get-vmhost

Great now I know how to pull information about my host but what if I want to see all the information it pulls from my host or from any command.  That can be done with the fl command.  Powershell uses a piping structure that will be familiar to all Linux / Unix shell coders.

get-vmhost | fl

How about displaying only selected fields.  For example lets display the used CPU and used memory on each host:

get-vm | select Name, CpuUsageMhz, MemoryUsageGB

Now we have the state of a beautiful reporting system.  Look for more in the next part.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.