VM Inventory PowerCLI script for DR Purposes

I know that there have been a number of blog posts in the past about creating inventories of VMs within an environment, showing various details  about the environment… I have even used some of them myself in the past.

The script that I have pulled together and enhanced (from various sources) was designed to provide information that can be used in the event of a disaster and the virtual machines needed to be registered back into the environment from the datastores.  There is an additional script that I discovered which allows you to register all VMs on a particular datastore into the environment, and this works quite well (I’ll post this information separately) but sometimes there may be a requirement to just register a few of the VMs and therefore this information could be very useful and reduce the time to recover the VMs.

Unfortunately, I’m not a Powershell or PowerCLI expert and therefore there may be a more efficient way to perform this task, especially as this task can take around 3 hrs to complete on an environment consisting of around 700 VMs, and therefore in my environment, I only run this script once a week.  There are two files that are created at the end of the script, a CSV file which will allow you to manipulate the information and a formatted html page.  The html page looks similar to below (all references to particular server names have been removed):

This script runs on PowerCLI 6.5 Update 1.  Items in bold and italic should be changed for your environment.

Here’s the script:

get-module -ListAvailable VM* | Import-Module

connect-viserver vcenterservername

$Date = get-date -f dd-MM-yyyy

$Header = @"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html><head><title>VMware Inventory Report from vcenterservername</title>
<style type="text/css">
body {
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
 #report { width: 835px; }
 border-collapse: collapse;
 border: none;
 font: 10pt Verdana, Geneva, Arial, Helvetica, sans-serif;
 color: black;
 margin-bottom: 10px;
 table td{
 font-size: 12px;
 padding-left: 0px;
 padding-right: 20px;
 text-align: left;
 table th {
 font-size: 12px;
 font-weight: bold;
 padding-left: 0px;
 padding-right: 20px;
 text-align: left;
h2{ clear: both; font-size: 130%; }
 clear: both;
 font-size: 115%;
 margin-left: 20px;
 margin-top: 30px;
p{ margin-left: 20px; font-size: 12px; }
table.list{ float: left; }
 table.list td:nth-child(1){
 font-weight: bold;
 border-right: 1px grey solid;
 text-align: right;
table.list td:nth-child(2){ padding-left: 7px; }
table tr:nth-child(even) td:nth-child(even){ background: #A7D6EC; }
table tr:nth-child(odd) td:nth-child(odd){ background: #A7D1E5; }
table tr:nth-child(even) td:nth-child(odd){ background: #7FC6E7; }
table tr:nth-child(odd) td:nth-child(even){ background: #C2E6F6; }
div.column { width: 320px; float: left; }
div.first{ padding-right: 20px; border-right: 1px grey solid; }
div.second{ margin-left: 30px; }
table{ margin-left: 20px; }
<H1>VMware Inventory Report from vcenterserver</H1>

$Pre = "This report lists all of the VMs as of $Date"
$Post = "Created on: $Date by yourname"

$inventory = Get-VM | Add-Member -MemberType ScriptProperty -Name 'VMXPath' -Value {$this.extensiondata.config.files.vmpathname} -Passthru -Force | Select Name, @{N="Cluster";E={Get-Cluster -VM $_}}, `
@{N="ESX Host";E={Get-VMHost -VM $_}}, `
@{N="Datastore";E={Get-Datastore -VM $_}},PowerState,NumCPU,MemoryMB,Folder,VMXPath,Notes | Sort -Property Name
$inventory | Export-Csv -NoTypeInformation filelocation\VMInventory.csv
$inventory | ConvertTo-Html -title "VMware Inventory Report from vcenterservername" -Head $Header -PreContent $Pre -PostContent $Post | Out-file filelocation\VMInventory.html

Disconnect-viserver -confirm:$false

Leave a Reply

Your email address will not be published. Required fields are marked *