Category Archives: VMware

Easy VM Backup with Veeam Zip

If you run a small VMware or Hyper-V setup, perhaps for a lab or personal training, you probably haven’t thought to much about backing up your virtual machines. But it is actually pretty easy and doesn’t cost anything. Let me give you a quick intro to the free backup tool from Veeam. The first thing you need to do is download it. The install is pretty straightforward so I’ll skip that part.

When you fire it up for the first time you’ll see a tree for VMware and Hyper-V. This product works for both! Right-click on whichever virtualization infrastructure you are using and add a server. It is a quick and easy wizard that should only take a minute to complete. The app should automatically enumerate all the virtual machines.

veeam-backup-1

To create a backup right-click on a virtual machine and select Veeam zip, or you can use the menu icon. You will be ask to provide a path for the backup.

veeam-backup-2

The program will create a single zip file of all associated files for the virtual machine. It takes a few minutes for the backup to get up to speed.

veeam-backup-3

Eventually my backup reached 24MB/second. When the backup finishes you can view a status report by going to History and then right-clicking on the backup. You can view stats.

veeam-backup-4

Or a summary report as an HTML file in your browser

veeam-backup-5

All of the files will be stored in a vbk file. This is a zip file format. You can extract files locally, or leave it alone until you need to restore something.

There are some limitations, but remember this is a free tool not a free trial. There’s no PowerShell support and it doesn’t support Hyper-V on Windows 8. Those are biggies for me. Of course Veeam has a full-blown backup product that you should try out, especially if you like the idea of managing backups via PowerShell.

I’ll cover some additional features in future posts.

Copy and Mount a CD with PowerCLI

The other day I realized I needed to rebuild my SQL Server 2012 installation which I’m running on a virtual machine running on an ESX box. Given that I have PowerCLI and I like to do things from the command prompt when I can, I decided to mount the SQL Server 2012 ISO on the VM using PowerShell. This actually requires a few steps that I thought I would share.

First, you naturally need to have PowerCLI installed. You will need to import the necessary snapins and connect to the ESX host.


Add-PSSnapin VMware.VimAutomation.core
Connect-viserver -Server ESX

Once connected, I can begin the process. Next, I need to copy the ISO file the datastore so that I can mount it from the VM. While the vmstore: PSDrive is fun to work with, you can’t copy from the filesystem. Copying between providers is simply not allowed. Instead I’ll use the Copy-DatastoreItem cmdlet. This will allow me to copy a local file to the VMware datastore.

The tricky part here is to get the right format for the datastore destination. This is where I want to copy to:


PS vmstore:\ha-datacenter\datastore3\ISO> dir

Datastore path: [datastore3] ISO

LastWriteTime Type Length Name
------------- ---- ------ ----
8/18/2009 9:09 AM IsoImageFile 2996799488 en_windows_server...

The “trick” is to grab the value for Datastore path from the directory listing. Thus, I can run this to copy the ISO file to the datastore.


$iso="C:\users\jeff\Downloads\en_sql_server_2012_standard_edition_with_sp1_x86_x64_dvd_1228143.iso"
$dest="vmstore:\ha-datacenter\datastore3\ISO"
Copy-DatastoreItem -Item $iso -Destination $dest -passthru

You get a nice progress bar and in a few minutes the file is copied. Now I can mount it in the VM’s CDDrive using Set-CDDrive. The cmdlet will need a CDDrive object from a VM and the path to the ISO file. I’ll have to construct a path using the VMware datastore format. It looks a little funny because it is not a typical Windows path.


$isopath = "[datastore3] ISO\en_sql_server_2012_standard_edition_with_sp1_x86_x64_dvd_1228143.iso"

For situations like this, I find it easiest to use the corresponding Get cmdlet, and pipe the resulting object to the Set cmdlet.


get-cddrive -VM "globomantics db" | set-cddrive -IsoPath $isopath -Connected $true

The only other parameter I specified was to connect the CDDrive to the VM. This command takes a moment to run and then in the VM I can “see” the DVD and use it normally. Awesome. When I’m finished I can dismount the CD much the same way.


get-cddrive -VM "globomantics db" | set-cddrive -NoMedia

This may seem like a lot of typing, but if it is something I need to do a lot I could build a simple script or function. And it is still faster (for me) than navigating the GUI.

Pipeline Power

Last week I came across a blog post that had a decent example using PowerShell and PowerCLI to get the disk location for all virtual machines. The posted code works and does display the information you might be after.


$myVMs = get-vm

foreach($vm in $myVMs){
$myDisks = @($vm | get-harddisk)
foreach ($disk in $myDisks) {
write-host $vm.Name, ( $disk|select -ExpandProperty Filename)
}
}

But I saw an teaching opportunity. Because the code works I can’t say it is “wrong”, but it really doesn’t adopt the PowerShell paradigm. The first issue is that using Write-Host only writes content to the console. There is no way with this command to do anything else with the results such as sorting, grouping or sending to a text file.

The other issue is the need to use ForEach. This is what we had to do in VBScript but in PowerShell we can take advantage of the pipeline.


get-vm | Select Name,@{Name="Disk";Expression= {$_ | get-harddisk | Select -ExpandProperty Filename }}

But now I can do something with this such as sorting by disk:


PS S:\> get-vm | Select Name,@{Name="Disk";Expression= {$_ | get-harddisk | Select -ExpandProperty Filename }} | sort Disk,Name

Name Disk
---- ----
Cluster Alpha {[datastore1] Cluster Alpha/Cluster ...
Globomantics Mail [datastore1] globomantics mail/Win2K...
MyCompany Exchange 2007 {[datastore1] MyCompany Exchange 200...
MyCompany XP {[datastore1] MyCompany XP/MyCompany...
MyCompany Windows 2008 [datastore1] MyCompany2008/Windows S...
MyCompanyDC 2K3R2 {[datastore1] MyCompanyDC 2K3R2/MyCo...
R2 Server Core -DEL [datastore1] Research Core DC/R2 Ser...
Cluster Bravo {[datastore2] Cluster Bravo/Cluster ...
MyCompany Vista {[datastore2] MyCompany Vista/Vista ...
...

Or if there are multiple disks, it is much easier to work with them. Write-Host can’t.


PS S:\> $vminfo=get-vm | Select Name,@{Name="Disk";Expression= {$_ | get-harddisk | Select -ExpandProperty Filename }}
PS S:\> $vminfo[1].disk
[datastore2] MyCompany Vista/Vista Baseline.vmdk
[datastore2] MyCompany Vista/MyCompany Vista.vmdk
PS S:\> $vminfo | Export-Clixml c:\work\vminfo.xml

The tricky part here I realize is pulling up a value from a nested object, in this case the Filename and adding it to the VM object. I totally get that this is not something a beginner would necessarily discover on their own, which is why I write stuff like this. But the big difference is that I know have an object written to the pipeline that I can do something with and I didn’t have to resort to keep track of what goes where in some foreach loops.

The other advantage, although not universal, is performance. Running the ForEach code against my 23 VMs took almost 6 seconds. My PowerShell one line took a tad over 3 seconds.

I don’t want you to think you can never use Write-Host or ForEach. Sometimes they make sense and may even perform better. But always ask yourself if you are thinking the PowerShell way and pushing objects through the pipeline or are you writing something that could be mistaken for VBScript.

By the way, I have posted most of this on the blog as a comment that is awaiting moderation. But I figured I would share it with my readers as well.