A huge shift in enterprise IT is the move toward a more Dev Ops way of handling infrastructure. Enterprise administrators are finding the need to think more as a developer as automation of today’s environments allows administrators to be agile, efficient, and timely in carrying out routine tasks as well as provisioning new infrastructure. Learning to write a bit of code is an essential skill for enterprise administrators, especially virtualization professionals to stay relevant in the industry. One of the perhaps easiest scripting languages to quickly see a tangible benefit from is Microsoft PowerShell. It has very intuitive verb-noun syntax that is very human readable. It is also readily available. It is built into all modern versions of Microsoft Windows. In addition, recently, PowerShell has been made available for Linux and MacOS as well which makes it available across the board!

One of the huge benefits as well to PowerShell is that it is highly extensible and allows for vendors to be able to write their own modules or snapins which bundle specific commandlets for their products. A case in point to this is VMware’s PowerCLI which is now a module. By adding this module to a PowerShell environment, an administrator can interact with and script against a VMware vSphere environment. Many may already be familiar with PowerShell in general so we want to take a look at moving to the next level – working with PowerShell and using this knowledge for our virtual environments.

Protect Your Data with BDRSuite

Cost-Effective Backup Solution for VMs, Servers, Endpoints, Cloud VMs & SaaS applications. Supports On-Premise, Remote, Hybrid and Cloud Backup, including Disaster Recovery, Ransomware Defense & more!

Getting Started with PowerShell

Since PowerShell is readily available being included with Windows and easily downloaded and installed for Linux and MacOS, it is a great way to get started automating especially in virtualized environments by using PowerCLI as well as the commandlets for Hyper-V interaction. What are commandlets? Commandlets are lightweight commands that are used in PowerShell. We are going to assume here that you have launched and are familiar with how PowerShell works. Let’s take it to the next level by learning how to use PowerShell ISE as our IDE for working with PowerShell.

To launch PowerShell ISE, we can simply type PowerShell_ISE in a search or run menu if we like to type, or we can navigate out to Windows PowerShell >> Windows PowerShell ISE in the Windows 10 Start menu.

Download Banner

Windows-PowerShell-ISE

Using either method we should see our PowerShell ISE launch and look similar to the below.

PowerShell-Scripting

From here we can write PowerShell scripts and execute them as well. Notice how in the PowerShell pane which is the bottom blue pane in the window above, we have the “Welcome to VMware PowerCLI”. This is because we have added our import-module for VMware PowerCLI to our PowerShell ISE profile. How do we create and use a PowerShell ISE profile?

Create PowerShell ISE Profile and call Modules on Startup

A fairly well documented way to both check for and show your PowerShell profile is to use the following set of commandlets. We can do this right from our PowerShell ISE PowerShell window.

  • To see your profile path, you can simply type $profile
  • We can test to see if the profile file is there by running the commandlet Test-Path $profile
  • If the return is False, we can use the command new-item -path $profile -type file -force | out-null to create it

We can edit the created Microsoft.PowerShellISE_profile.ps1 file to add additional modules that we want to start when we launch PowerShell ISE such as VMware PowerCLI. Since the 6.5.1 release of PowerCLI, it is module based. To add the new PowerCLI module once we have installed it, we can add the following line to our PowerShell ISE profile:

# Load Windows PowerShell cmdlets for managing vSphere

Import-Module VMware.PowerCLI

The next time you launch PowerShell ISE you will have the VMware PowerCLI module loaded.

Running Commandlets

Now that we have loaded our example VMware PowerCLI module, we can begin pulling data from our VMware environment. The first thing that we need to do however is connected to our vCenter server or standalone host. We have to make that initial connection before we can start querying information or changing configuration. To do that, it is a simple PowerCLI one liner.

  • Connect-viserver -server < your server > or you can also use the assumed server name and simply use the shorter
    • Connect-viserver < your viserver >

Once we have made a connection to our VMware environment, we can now run commandlets against the environment. The simplest and perhaps most used command among the get commands is the get-vm commandlet. This commandlet allows us to query our VMware environment to return all the VMs in a particular vCenter or standalone server environment.

PowerShell-Scripting

The beauty of PowerShell is that we can begin to massage the data any way we want to by querying based on certain properties of the objects that are returned to us. For instance if we query and return all of the properties of the VMs by using the get-vm | fl commandlet, we can now start to see properties that we can use for querying purposes such as PowerState, NumCPU, MemoryGB, and VMHost just to name a few.

PowerShell-Scripting

For instance, if we only want to see the VMs that are in the PoweredOn state, we can run a simple one liner to pull that information like so:

  • Get-vm | where-object {$_.PowerState -eq “PoweredOn”}

Or maybe we only want to see the VMs that reside on a certain host:

  • Get-vm | where-object {$_.VMHost -like “192.168.85.10” }

We can also manipulate how the data is displayed to us. If we want to utilize the Grid View which opens the result set in a box where we have GUI access to filter or add criteria.

  • Get-vm | where-object {$_.VMHost -like “192.168.85.10” } | Out-GridView

The resulting window looks like the following.

PowerShell-Scripting

Variables

Variables are building blocks of being able to reuse data very efficiently in PowerShell code. The variable is represented by the $ sign in front of the word we want to use as a variable. For instance, we might use a variable to store all our VMs.

$vms = get-vm

With the above, we no longer have to type or use the commandlet get-vm as we now have it stored in the $vms variable.

Simple Looping

Looping in PowerShell allows us to accomplish a lot of work very efficiently and quickly. If we have a command that we want to execute against a lot of servers, we can use a loop to do this. The basic format of a PowerShell loop where we read in a list of VMs from a text file and then do something with that list of information would be like the following.

  • $vms = Get-content c:\myfolder\vms.txt

    Foreach ($vm in $vms) {

    Get-something | where object {$_.Something -eq “something”}

    }

The above code will get the list of VMs that we have in the text file and then that list will be used as a variable to do a repetitive action against that certain list. We can start to see the real value and benefit from using PowerShell in that we can automate mundane tasks and do them very quickly and efficiently.

Learning Resources

There are so many good learning resources out on the Internet that it would be hard to list them all. But certainly there are many useful official blogs such as the Microsoft PowerShell Team Blog where you can find a lot of useful information. Also, there is PowerShell.org which has many learning resources and useful information available. Never underestimate forums, blogs, and other resources such as PodCasts to hear techniques talked about and ways of thinking about accomplishing tasks in PowerShell. Also, don’t try to reinvent the wheel so to speak. Most likely if you run into a problem or a task you want to automate, someone has most likely written something similar that you can make use of. Don’t be afraid to reuse community code at least as a starting point.

Thoughts

PowerShell is a relatively easy and tremendously powerful scripting language that can be used to automate many tasks in today’s IT environments. It is easy to get started using PowerShell as it is readily available and there are many resources for learning as well as sample code that can be consumed. So, wait no longer and start getting your feet wet with PowerShell automation and you will never regret that you did.

Follow our Twitter and Facebook feeds for new releases, updates, insightful posts and more.

Rate this post