{"id":156,"date":"2017-09-28T10:33:15","date_gmt":"2017-09-28T09:33:15","guid":{"rendered":"http:\/\/brgeek.com.br\/wordpress\/?p=156"},"modified":"2017-09-28T10:53:08","modified_gmt":"2017-09-28T09:53:08","slug":"quick-function-to-determine-if-a-computer-is-a-vm-or-physical-box","status":"publish","type":"post","link":"http:\/\/brgeek.com.br\/wordpress\/2017\/09\/28\/quick-function-to-determine-if-a-computer-is-a-vm-or-physical-box\/","title":{"rendered":"Quick function to determine if a computer is a VM or physical box"},"content":{"rendered":"<p>Hi Folks, this is a script to determine if the machine is a Virtual Machine or a Physical server, very useful in large Datacenters where sometimes you have to determine large lists of servers.<\/p>\n<pre><code class=\"language-powershell\" data-line=\"\">\n\n&lt;#\n.Synopsis\n   A quick function to determine if a computer is VM or physical box.\n.DESCRIPTION\n   This function is designed to quickly determine if a local or remote\n   computer is a physical machine or a virtual machine.\n.NOTES\n   Created by: Michael\n   Modified: 4\/20\/2017 03:28:53 PM  \n\n     To Do:\n    * Find the Model information for other hypervisor VM&#039;s (i.e KVM).\n.EXAMPLE\n   Get-MachineType\n   Query if the local machine is a physical or virtual machine.\n.EXAMPLE\n   Get-MachineType -ComputerName SERVER01 \n   Query if SERVER01 is a physical or virtual machine.\n.EXAMPLE\n   Get-MachineType -ComputerName (Get-Content c:\\temp\\computerlist.txt)\n   Query if a list of computers are physical or virtual machines.\n\n#&gt;\nFunction Get-MachineType\n{\n    [CmdletBinding()]\n    [OutputType([PSCustomObject])]\n    Param\n    (\n        # ComputerName\n        [Parameter(Mandatory=$false,\n                   ValueFromPipeline=$true,\n                   ValueFromPipelineByPropertyName=$true,\n                   Position=0)]\n        [string[]]$ComputerName=$env:COMPUTERNAME,\n        $Credential = [System.Management.Automation.PSCredential]::Empty\n    )\n\n    Begin\n    {\n    }\n    Process\n    {\n        foreach ($Computer in $ComputerName) {\n            Write-Verbose &quot;Checking $Computer&quot;\n            try {\n                # Check to see if $Computer resolves DNS lookup successfuly.\n                $null = [System.Net.DNS]::GetHostEntry($Computer)\n                \n                $ComputerSystemInfo = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer -ErrorAction Stop -Credential $Credential\n                \n                switch ($ComputerSystemInfo.Model) {\n                    \n                    # Check for Hyper-V Machine Type\n                    &quot;Virtual Machine&quot; {\n                        $MachineType=&quot;VM&quot;\n                        }\n\n                    # Check for VMware Machine Type\n                    &quot;VMware Virtual Platform&quot; {\n                        $MachineType=&quot;VM&quot;\n                        }\n\n                    # Check for Oracle VM Machine Type\n                    &quot;VirtualBox&quot; {\n                        $MachineType=&quot;VM&quot;\n                        }\n\n                    # Check for Xen\n                    &quot;HVM domU&quot; {\n                        $MachineType=&quot;VM&quot;\n                        }\n\n                    # Check for KVM\n                    # I need the values for the Model for which to check.\n\n                    # Otherwise it is a physical Box\n                    default {\n                        $MachineType=&quot;Physical&quot;\n                        }\n                    }\n                \n                # Building MachineTypeInfo Object\n                $MachineTypeInfo = New-Object -TypeName PSObject -Property ([ordered]@{\n                    ComputerName=$ComputerSystemInfo.PSComputername\n                    Type=$MachineType\n                    Manufacturer=$ComputerSystemInfo.Manufacturer\n                    Model=$ComputerSystemInfo.Model\n                    })\n                $MachineTypeInfo\n                }\n            catch [Exception] {\n                Write-Output &quot;$Computer`: $($_.Exception.Message)&quot;\n                }\n            }\n    }\n    End\n    {\n\n    }\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hi Folks, this is a script to determine if the machine is a Virtual Machine or a Physical server, very useful in large Datacenters where sometimes you have to determine large lists of servers. &lt;# .Synopsis A quick function to determine if a computer is VM or physical box. .DESCRIPTION [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":148,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[8,12,6,11,9,10],"class_list":["post-156","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-michael-goulart","tag-microsoft","tag-powershell","tag-script","tag-virtual-or-physical","tag-vm-or-physical"],"jetpack_featured_media_url":"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2017\/09\/powershell-tips-and-tricks_w_640.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/comments?post=156"}],"version-history":[{"count":4,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/156\/revisions"}],"predecessor-version":[{"id":160,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/156\/revisions\/160"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media?parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/categories?post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/tags?post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}