Basic PowerCLI Scripting For VMware Vsphere
Basic PowerCLI Scripting For VMware Vsphere
VMware vSphere
Basic PowerCLI scripting
Let’s take a look at a few basic PowerCLI commandlets to see how easy
it is to start pulling information from our vSphere environment with
PowerCLI. Keep in mind, the below is in no way meant to be an all-
encompassing tutorial, but rather an introduction to PowerCLI and how
to get started running commandlets and learning to automate your
environment.
The first commandlet we need to run is the one to actually connect to
our vSphere environment. You can either connect directly to an ESXi
host or to vCenter Server.
To connect, run the following command in a PowerCLI enabled
PowerShell session:
connect-viserver –server 〈yourserver〉
When you run the command, you will be prompted for your login
credentials. Type in your username and password for either your ESXi
server or vCenter Server.
As you can see, this displays only the VMs that are in the “PoweredOff”
PowerState. This can be extremely useful. We can even pipe the result
of the above command into the Start-VM commandlet. So essentially
we can get all the powered off VMs and then power those VMs on.
Get-VM | where-object {$_.PowerState –eq “PoweredOff”} | Start-
VM
What about shutting down specific VMs? We can do that as well. We can
call the VM by name and then use the Stop-VMGuest commandlet
which initiates a guest operating system shutdown.
Get-VM 〈yourvm〉 | Stop-VMguest
If you do not want to receive the confirmation of the action, we can add
the –confirm:false parameter:
Let’s say we want to see how many VMs we have running that have
more than 1 CPU assigned. Again, we can use the where-
object commandlet to select out the NumCpu object.
Get-VM | where-object {$_.NumCpu –gt 1 }
PowerCLI Loops
Loops allow you to perform the same operation multiple times against
either selected objects or for a number of times looped through. For
instance, with a foreach loop, we can loop a certain action multiple
times. It is setup like the pseudo code below:
foreach ($var in $vars){
Do something…
}
Resources
There are a lot of great resources out on the web including personal
blog sites, official documentation, code samples, etc. What I have found
is that if you can think of something you want to do, most likely there is
someone who has already written a line of PowerCLI code that will do
what you want. The good thing too is normally you can find something
very close to what you want to do, and with very little modification after
learning the basics, you can modify it to suite your needs. By doing this
also, you are learning PowerCLI and how it works as well as how to
modify the code to perform the task you need to accomplish.
A great place to start with documentation and other resources is
the official VMware PowerCLI release and documentation site. Here, you
can download the binaries as well as the official User’s Guide.
There are also tons of books with scripts and script examples that can
be dissected and modified. Keep in mind the community
of VMware administrators out there who, for the most part, are glad to
share the knowledge they have learned from someone else on their
way to becoming proficient with PowerCLI. Reach out to those ones in
your community for help with specifics or just general principles.
Thoughts
PowerCLI is very powerful to say the least. We have only scratched the
surface in the above examples that were shown. Any tasks that you
repetitively do, or that are cumbersome to do in the web client are great
candidates for scripting with PowerCLI. In fact, that is usually how most
get introduced to PowerCLI – they have a task they need to perform
automatically or an action that is based on other actions in vSphere, or
again repetitive actions that are well suited for scripting. The best way
to learn PowerCLI is to download it, install it, and get started using it in a
lab environment such as a home lab. The real power of a VMware
vSphere environment is only unlocked when you see the potential of
using automation to take control of vSphere. So, get started scripting
with PowerCLI and you will never look back!