Basic PowerCLI Scripting for VMware vSphere

Previously, we looked at PowerCLI – what it is, how to install it, and integrating into Windows Powershell ISE. Now, let’s take a look at basic scripting including connecting, PowerCLI commandlets, as well as looping.

NAKIVO for VMware vSphere Backup

NAKIVO for VMware vSphere Backup

Complete data protection for VMware vSphere VMs and instant recovery options. Secure backup targets onsite, offsite and in the cloud. Anti-ransomware features.

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.

Basic PowerCLI scripting

One of the most basic commands we can run when we connect to either ESXi or vCenter is seeing all the VMs in the environment. We use the get-vm commandlet to show information about the guest VMs.

  • Get-VM - The get-vm commandlet lists VMs running on the connected host

get-vm commandlet

To get more verbose information from the get-vm commandlet, we can show the full information with the command:

  • Get-VM | fl – Displays more verbose information such as the Guest Operating System, Resource pools, VM Hardware version level, folder it is located in, and many other details.

Get VM Full Information

Now, let’s return specific useful information from the Get-VM commandlet. Let’s find the VMs that are in the PoweredOff state. We can get information from the Get-VM commandlet and then pipe that into where-object to select out the specific information we want to query for – in this case PowerState. As you can see, there are worlds of possibilities in getting various VM information and piping this information into another PowerShell commandlet.

  • Get-VM | where-object {$_.PowerState –eq “PoweredOff”}

Get-VM PowerState

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

Start-VM commandlet

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

Stop-VMguest commandlet

If you do not want to receive the confirmation of the action, we can add the –confirm:false parameter:

Stop-VMguest –confirm:false

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 }

where-object commandlet

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…
}

If we want to get specific about which objects we are going to perform the action on, we can, for example, loop through a specific list of VMs from a text file. To populate the text file, we can run our Get-VM commandlet selecting out the properties we want, then pipe that to a text file. We can then use PowerShell to read the text file and place the VMs into an array on which we can loop through and perform an action. Below we are adding a new network adapter to VMs in the list.

Get-vm | where-object {$_.MemoryGB –eq 4 } | select -ExpandProperty Name | out-file c:\VMs.txt

$vms = get-content c:\VMs.txt

Foreach-object ($vm in $vms) {
new-networkadapter -vm $vms -NetworkName "〈Port group name〉" -Type "VMXNET3" –startconnected
}

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!

1 Year of Free Data Protection: NAKIVO Backup & Replication

1 Year of Free Data Protection: NAKIVO Backup & Replication

Deploy in 2 minutes and protect virtual, cloud, physical and SaaS data. Backup, replication, instant recovery options.

People also read