{"id":821,"date":"2017-02-07T15:00:25","date_gmt":"2017-02-07T15:00:25","guid":{"rendered":"http:\/\/virtualworlduk.co.uk\/?p=821"},"modified":"2017-02-07T15:00:25","modified_gmt":"2017-02-07T15:00:25","slug":"register-all-vms-from-a-datastore","status":"publish","type":"post","link":"https:\/\/www.virtualworlduk.co.uk\/wordp\/register-all-vms-from-a-datastore\/","title":{"rendered":"Register All VMs from a Datastore"},"content":{"rendered":"<p>Quick post this one, I discovered this script\u00a0when I was looking to register VMs as part of a temporary DR solution, unfortunately I cannot remember where I found the script and therefore I apologise to the original creator.<\/p>\n<p>The idea behind the script is to search through the datastore or datastores that you select, discover the location of the .vmx files on the datastores and then registering the virtual machines associated with the .vmx files into the virtual environment.<\/p>\n<p>Here&#8217;s the script, items in bold and italic should be changed:<\/p>\n<pre>connect-viserver <strong><em>vcenterservername<\/em><\/strong>\n\n$Cluster = \"<em><strong>clustername<\/strong><\/em>\"\n$Datastores = \"<em><strong>datastorename<\/strong><\/em>\"\n$VMFolder = \"<em><strong>destinationfolderforvms<\/strong><\/em>\"\n$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1\n \nforeach($Datastore in Get-Datastore $Datastores) {\n # Set up Search for .VMX Files in Datastore\n $ds = Get-Datastore -Name $Datastore | %{Get-View $_.Id}\n $SearchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec\n $SearchSpec.matchpattern = \"*.vmx\"\n $dsBrowser = Get-View $ds.browser\n $DatastorePath = \"[\" + $ds.Summary.Name + \"]\"\n \n # Find all .VMX file paths in Datastore, filtering out ones with .snapshot (Useful for NetApp NFS)\n $SearchResult = $dsBrowser.SearchDatastoreSubFolders($DatastorePath, $SearchSpec) | where {$_.FolderPath -notmatch \".snapshot\"} | %{$_.FolderPath + ($_.File | select Path).Path}\n \n #Register all .vmx Files as VMs on the datastore\n foreach($VMXFile in $SearchResult) {\n New-VM -VMFilePath $VMXFile -VMHost $ESXHost -Location $VMFolder -RunAsync\n }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Quick post this one, I discovered this script\u00a0when I was looking to register VMs as part of a temporary DR solution, unfortunately I cannot remember where I found the script and therefore I apologise to the original creator. The idea behind the script is to search through the datastore or datastores that you select, discover the location of the .vmx [&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-821","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\/821","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=821"}],"version-history":[{"count":0,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/posts\/821\/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=821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/categories?post=821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.virtualworlduk.co.uk\/wordp\/wp-json\/wp\/v2\/tags?post=821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}