Get started Bring yourself up to speed with our introductory content.

Master the basics of scripting by creating snapshots

Thanks to its predictability, scripting is quickly gaining traction; admins would be wise to get on board the bandwagon by familiarizing themselves with some basic scriptable tasks.

Scripting is the way of the future, because it provides quick, repeatable and predicable outcomes. The rise of...

scripting means the days of using graphical user interfaces are limited, even with the new Web client. One example of an easily scriptable task is creating snapshots on several machines. By comparison, creating snapshots via the Web client involves more work than it should. Creating scripts may sound a bit intimidating if you have never done it before, but in reality, constructing a simple script is a relatively painless process. In this tip, we'll show you how to build a rudimentary script to manage snapshots, from the basics all the way through scripting snapshot reports.

The building blocks of scripting

To begin, you need to make sure PowerShell and PowerCLI are installed and working correctly. Both products can be downloaded and installed for free. PowerCLI is especially important, as it extends the capability of PowerShell by adding VMware-centric commands to manage a VMware environment.

There are three basic steps for creating snapshots: connecting to the vCenter in question, locating the machine you intend to make a snapshot of and, finally, creating and naming your snapshot.

You can test the process manually using PowerCLI before putting it all together.

To log in to the vCenter Server, open the PowerCLI console and enter the following command:

                connect-viserver vcenter

When prompted, log in using your Active Directory credentials. Once you have successfully logged in, you can use the PowerCLI cmdlets, such as get-vm or set-vm.

vCenter Server login window.
Figure A. vCenter Server login window.

If you see a lot of virtual red ink on the screen, the command failed. If your command should fail, I recommend double checking your login credentials. Note that PowerCLI is case-sensitive; yellow writing means caution or warning.

To find a specific VM, you can use the command shown below:

                get-vm vcommander

This will report back the name, power status, CPUs and memory of the VM by default. While this is certainly helpful, how do we make vCenter Server do more than just display details?

Displaying VM details.
Figure B. Displaying VM details in vCenter Server.

This is where pipelines come in. In PowerCLI, you can pull together several commands that work left to right, each taking the output of the previous command as its input.

To create a snapshot, we can chain together the get-vm and pass it to the set-snapshot cmdlet, as shown below. In essence, it passes the VM name to the New-Snapshot cmdlet.

                get-vm vcommander | New-Snapshot -Name "Pre upgrade snapshot"

After this is complete, check to make the snapshot is there, with the following command:

                get-vm vcommander | get-snapshot

Creating a snapshot.
Figure C. Creating snapshots with PowerCLI.

Creating snapshots in bulk

Now, all this is very useful by itself, but it really comes into its own when someone asks for a whole range of machines to be snapshotted. We can extend our little script to repeat the set-snapshot command for each VM specified.

We can do this by reading in the names of the VMs to snapshot using the get-content cmdlet, and then looping through and performing the command on the VM in question. Get-content reads in a line at a time and assigns it to our $VM variable. It then uses foreach to loop through the entire file -- for each line, enter the command encased in brackets. Note that you must fill the vmlist.txt file and run it using PowerCLI. This kind of tool is useful for quarterly upgrades, as it eliminates the need to search, right-click, create a snapshot and fill in the details.

     Connect-viserver vc-server

     foreach ($VM in Get-Content c:\tools\vmlist.txt) {

           get-VM $VM | new-snapshot -Name "Pre upgrade snapshot to new OS revision"

     }

For the sake of simplicity, enter the code shown above in Notepad and give it a useful name. For example, I called mine "snappy.ps1." Be sure to save your snapshot as a PS1 extension and put it in the c:\tools folder for ease of use.

Removing snapshots is just as easy. Copy and paste the basic script we created above into another window. All you need to do is to change the set-snapshot line in the command to Remove-Snapshot.

                foreach ($VM in Get-Content c:\tools\vmlist.txt) {

           get-VM $VM | remove-snapshot -Name "Pre upgrade snapshot to new OS revision"

     }

Now, we have a useful script to manage snapshots. Even the most basic scripts can be useful and don't require expert-level PowerShell experience to use.

Basic looping can help you achieve a great deal, including exporting machine details. If you want to see all the possible information for a VM, you can override the default items from get-VM by piping it into a select command, like the one shown below, and exporting it to a file:

                get-vm "vm name" | select * | export-csv -NoTypeInformation output.csv

You can use this script to build a weekly inventory of your VM servers with all the items and export them to comma-separated values file.

Finally, if you're unsure if what to do, the get-help cmdlet offers a list of commands, usage scenarios and examples.

Next Steps

Choosing the right snapshot method

Build your PowerShell scripting knowledge

Which CLI is superior?

This was last published in April 2016

Dig Deeper on Scripting administrative tasks

Join the conversation

2 comments

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

Other than creating snapshots, what are some other easy scriptable tasks?
Cancel
Interesting article, makes m wish I had some vcenter nearby to try this on.


Cancel

-ADS BY GOOGLE

SearchServerVirtualization

SearchVirtualDesktop

SearchDataCenter

SearchCloudComputing

Close