Test-IsVirtual.ps1


<#PSScriptInfo
 
.VERSION 1.0.0.0
 
.GUID 9933e350-9219-4e0d-b3e3-19380b71cf0c
 
.AUTHOR Jeffrey Snover
 
.COMPANYNAME Microsoft
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Tests whether an machine is virtual or not
    Note - this is based upon a code fragment from a piece of malware discovered by John Lambert @JohnLATwC
    It looked pretty useful so I decided to clean it up and make it available to the rest of the world.
#>
 
Param(
 [Parameter(Position=0)]
 [Alias("CN")]
 [CimSession[]]$CimSession="localhost",

 [Parameter()]
 [Switch]$AsBoolean
 )
$Results = @()
foreach ($CS in $CimSession)
{
    $BIOS     = Get-CimInstance -CimSession $CS -ErrorAction Stop Win32_BIOS  
    $System   = Get-CimInstance -CimSession $CS -ErrorAction Stop Win32_ComputerSystem 
    $Item = New-Object PSObject -property  @{
        ComputerName = $CS.ComputerName
        DNSHostName  = $System.DNSHostName
        IsVirtual    = $False
        BIOSVersion  = $BIOS.Version
        SerialNumber = $BIOS.serialnumber
        Manufacturer = $System.Manufacturer
        Model        = $System.model
        VirtualType  = $null
    }

    if ($BIOS.SerialNumber -like "*VMware*") 
    {
        $Item.IsVirtual   = $true
        $Item.VirtualType = "VMWare"
    }
    else 
    {
        switch -wildcard ($BIOS.Version) 
        {
            'VIRTUAL' {
                $Item.IsVirtual   = $true
                $Item.VirtualType = "Hyper-V"
            }
            'A M I' {
                $Item.IsVirtual   = $true
                $Item.VirtualType = "Virtual PC"
            }
            '*Xen*' {
                $Item.IsVirtual   = $true
                $Item.VirtualType = "Xen"
            }
        }
    }
    if (!($Item.IsVirtual))
    {
        if ($System.Manufacturer -like "*Microsoft*")
        {
            $Item.IsVirtual   = $true
            $Item.VirtualType = "Hyper-V"
        }
        elseif ($System.Manufacturer -like "*VMWare*")
        {
            $Item.IsVirtual   = $true
            $Item.VirtualType = "VMWare"
        }
        elseif ($System.Model -like "*Virtual*") {
            $Item.IsVirtual   = $true
            $Item.VirtualType = "Unknown"
        }
    }
    if ($AsBoolean)
    {
        $Results += $Item.IsVirtual
    }
    else
    {
        $Results += $Item
    }
}
return $Results