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; }
 
 table{
 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%; }
 
h3{
 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; }
-->
</style>
</head>
<body>
 
<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

About the Author

Dinger

I have been in IT for the past 15 years and using virtualisation technologies for around the past 8 years. I started, as quite a lot of people do, working with PCs after playing with such iconic systems like the ZX81, ZX Spectrum and then progressing through 386s, 486s, Pentiums etc. After being headhunted at sixth form to work for a small company based around Hertfordshire, UK. I began working with small businesses and gaining a lot of hardware experience. Three years later, after helping to increase the size of the business, I needed to gain exposure to a larger environment to progress my own career. I joined a large manufacturing company around Electronic Test and Measurement which progressed my skills onto more PC work, hardware work and then onto Server Operating Systems. I progressed again onto a consultancy company based in Reading, UK. Initially working as an engineer performing hardware / software installations for larger companies contracted out to the consultancy company, I moved up into a Consultant position continuing my travel across the UK assisting and providing solutions to companies. I finally moved on again to my current position, working back in Hertfordshire, UK. Again working for a large manufacturing company, this time with over 50,000 users worldwide. I am responsible for the datacenter hardware, the storage environment, the vmware environment and also implementing their new Citrix XenApp farm. My days are busy but also productive, its a friendly environment and in my four years of being with the company, I have seen many changes in technology and infrastructure in use within the company. About the site I started this site as I had been thinking of having more of a presence on the web for a while. On a daily basis, I perform tasks and use tools that others may not use or may not think to do and therefore I thought that I would share some of these experiences and tips with others to help with their day to day work. Currently, my main focus of work is around VMware and Veeam Backup & Replication but hopefully as my tasks progress, I’ll be able to share useful bits of information about other areas of IT as well.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.