{"id":63,"date":"2010-05-03T22:18:48","date_gmt":"2010-05-04T02:18:48","guid":{"rendered":"http:\/\/enterpriseadmins.org\/blog\/?p=63"},"modified":"2010-05-03T22:18:48","modified_gmt":"2010-05-04T02:18:48","slug":"a-quick-way-to-find-virtual-machines-with-incorrect-names","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/a-quick-way-to-find-virtual-machines-with-incorrect-names\/","title":{"rendered":"A quick way to find virtual machines with incorrect names"},"content":{"rendered":"<p>A few days back I saw a twitter post from @maishsk which linked to this article:<\/p>\n<p><a href=\"http:\/\/technodrone.blogspot.com\/2010\/04\/how-to-speed-up-your-powercli-queries.html\">http:\/\/technodrone.blogspot.com\/2010\/04\/how-to-speed-up-your-powercli-queries.html<\/a><\/p>\n<p>The article got me thinking about the difference between Get-VM and Get-View and I wanted to apply it to a script I&#8217;ve been using for a little while. \u00a0This is a pretty simple script that finds my virtual machines that have incorrect names &#8212; the host name inside the VM doesn&#8217;t match the name of the VM in the vCenter console.<\/p>\n<p>Using the Get-VM cmdlet takes 148.7862461 seconds to find all of my virtual machines (excluding the templates):<\/p>\n<pre><code class=\"language-powershell\">$getvms = (Measure-Command {get-vm  | where { $_.Guest.HostName -ne $NULL -AND $_.Guest.HostName -notmatch $_.Name } | select-object name, {$_.Guest.HostName}}).TotalSeconds<\/code><\/pre>\n<p>Using the Get-View VirtualMachines cmdlet with a filter to exclude templates only takes 11.6853342\u00a0seconds:<\/p>\n<pre><code class=\"language-powershell\">$getviewf = (Measure-Command {get-view -viewtype virtualmachine -Filter @{\"Config.Template\"=\"false\"}  | where { $_.Guest.HostName -ne $NULL -AND $_.Guest.HostName -notmatch $_.Name } | select-object name, {$_.Guest.HostName}}).TotalSeconds<\/code><\/pre>\n<p>While either script does a better job than trying to compare these settings manually, this Get-View method shows a very significant savings! \u00a0Thanks for the suggestion @maishsk!<\/p>\n<p>Just in case you only want the query to show VM&#8217;s with incorrect names, here you go:<\/p>\n<pre><code class=\"language-powershell\">get-view -viewtype virtualmachine -Filter @{\"Config.Template\"=\"false\"} | where { $_.Guest.HostName -ne $NULL -AND $_.Guest.HostName -notmatch $_.Name } | select-object name, {$_.Guest.HostName}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A few days back I saw a twitter post from @maishsk which linked to this article: http:\/\/technodrone.blogspot.com\/2010\/04\/how-to-speed-up-your-powercli-queries.html The article got me thinking about the difference between Get-VM and Get-View and I wanted to apply it to a script I&#8217;ve been &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/a-quick-way-to-find-virtual-machines-with-incorrect-names\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-scripting","category-virtualization"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":5,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":68,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/63\/revisions\/68"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}