{"id":814,"date":"2017-02-07T07:18:11","date_gmt":"2017-02-07T07:18:11","guid":{"rendered":"http:\/\/virtualworlduk.co.uk\/?p=814"},"modified":"2017-02-07T07:18:11","modified_gmt":"2017-02-07T07:18:11","slug":"vm-inventory-powercli-script-for-dr-purposes","status":"publish","type":"post","link":"https:\/\/www.virtualworlduk.co.uk\/wordp\/vm-inventory-powercli-script-for-dr-purposes\/","title":{"rendered":"VM Inventory PowerCLI script for DR Purposes"},"content":{"rendered":"<p>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 \u00a0about the environment&#8230; I have even used some of them myself in the past.<\/p>\n<p>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. \u00a0There 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&#8217;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.<\/p>\n<p>Unfortunately, I&#8217;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. \u00a0There 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. \u00a0The html page looks similar to below (all references to particular server names have been removed):<\/p>\n<p><a href=\"http:\/\/virtualworlduk.co.uk\/wp-content\/uploads\/2017\/02\/VMInventory.jpg\"><img decoding=\"async\" class=\"aligncenter wp-image-815 size-medium\" src=\"http:\/\/virtualworlduk.co.uk\/wp-content\/uploads\/2017\/02\/VMInventory-300x78.jpg\" width=\"300\" height=\"78\" \/><\/a><\/p>\n<p>This script runs on PowerCLI 6.5 Update 1. \u00a0Items in bold and italic should be changed for your environment.<\/p>\n<p>Here&#8217;s the script:<\/p>\n<pre>get-module -ListAvailable VM* | Import-Module\n\nconnect-viserver <strong><em>vcenterservername<\/em><\/strong>\n\n$Date = get-date -f dd-MM-yyyy\n\n$Header = @\"\n&lt;!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Frameset\/\/EN\" \"http:\/\/www.w3.org\/TR\/html4\/frameset.dtd\"&gt;\n&lt;html&gt;&lt;head&gt;&lt;title&gt;VMware Inventory Report from <strong><em>vcenterservername<\/em><\/strong>&lt;\/title&gt;\n&lt;style type=\"text\/css\"&gt;\n&lt;!--\nbody {\nfont-family: Verdana, Geneva, Arial, Helvetica, sans-serif;\n}\n \n #report { width: 835px; }\n \n table{\n border-collapse: collapse;\n border: none;\n font: 10pt Verdana, Geneva, Arial, Helvetica, sans-serif;\n color: black;\n margin-bottom: 10px;\n}\n \n table td{\n font-size: 12px;\n padding-left: 0px;\n padding-right: 20px;\n text-align: left;\n}\n \n table th {\n font-size: 12px;\n font-weight: bold;\n padding-left: 0px;\n padding-right: 20px;\n text-align: left;\n}\n \nh2{ clear: both; font-size: 130%; }\n \nh3{\n clear: both;\n font-size: 115%;\n margin-left: 20px;\n margin-top: 30px;\n}\n \np{ margin-left: 20px; font-size: 12px; }\n \ntable.list{ float: left; }\n \n table.list td:nth-child(1){\n font-weight: bold;\n border-right: 1px grey solid;\n text-align: right;\n}\n \ntable.list td:nth-child(2){ padding-left: 7px; }\ntable tr:nth-child(even) td:nth-child(even){ background: #A7D6EC; }\ntable tr:nth-child(odd) td:nth-child(odd){ background: #A7D1E5; }\ntable tr:nth-child(even) td:nth-child(odd){ background: #7FC6E7; }\ntable tr:nth-child(odd) td:nth-child(even){ background: #C2E6F6; }\ndiv.column { width: 320px; float: left; }\ndiv.first{ padding-right: 20px; border-right: 1px grey solid; }\ndiv.second{ margin-left: 30px; }\ntable{ margin-left: 20px; }\n--&gt;\n&lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n \n&lt;H1&gt;VMware Inventory Report from <strong><em>vcenterserver<\/em><\/strong>&lt;\/H1&gt;\n\"@\n\n\n$Pre = \"This report lists all of the VMs as of $Date\"\n$Post = \"Created on: $Date by <strong><em>yourname<\/em><\/strong>\"\n\n\n$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@{N=\"ESX Host\";E={Get-VMHost -VM $_}}, `\n@{N=\"Datastore\";E={Get-Datastore -VM $_}},PowerState,NumCPU,MemoryMB,Folder,VMXPath,Notes | Sort -Property Name\n$inventory | Export-Csv -NoTypeInformation <em><strong>filelocation\\<\/strong><\/em>VMInventory.csv\n$inventory | ConvertTo-Html -title \"VMware Inventory Report from <em><strong>vcenterservername<\/strong><\/em>\" -Head $Header -PreContent $Pre -PostContent $Post | Out-file <em><strong>filelocation\\<\/strong><\/em>VMInventory.html\n\nDisconnect-viserver -confirm:$false<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 \u00a0about the environment&#8230; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":735,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[9],"tags":[],"class_list":["post-814","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vmware"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/posts\/814","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/comments?post=814"}],"version-history":[{"count":0,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/posts\/814\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/media?parent=814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/categories?post=814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/tags?post=814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}