Search code examples
powershellvirtual-machinewmipowershell-2.0get-wmiobject

Test for VM remotely without permissions


I have a PowerShell script that queries all of the computers on my domain via:

$ADlist = (([adsi]"WinNT://$((Get-WMIObject Win32_ComputerSystem).Domain)").Children) |
          Where ({$_.schemaclassname -eq 'computer'}) |
          Where ({$_.path -ne ''}) |
          Select Path | ft -Hide | Out-String

Works great. The goal is to loop through these computers to retrieve hardware and software info via WMI objects. Mission accomplished.

Except...I started throwing this on certain machines:

Access Denied Error

I am set up as a domain controller at my company, and the DNS hostname resolved and was responsive in my error catching tests. A little investigation into these problem PCs revealed that these were VMs and VM hosts.

Is there a way I can detect if a host is a VM remotely (of course) without whatever extra permissions I require to run GWMI commands? Actually, I just need to ignore them, as I'm only interested in physical machines. Only way I've found is to query the model field from Win32_ComputerSystem, but that doesn't help as I don't have permissions to run that command on these computers.

Researched has turned up empty, or rely on accessing the WM host physically, which are halfway around the world.

If absolutely necessary, I can be granted elevated permissions to access these machines, but the idea was to make this script accessible to my entire team. Requiring special permissions to run the script means that only I can run it.


Solution

  • Ended up enabling RSAT and using AD to do it. Much cleaner.

            import-module ActiveDirectory
            $ADlist = get-adcomputer -filter '*' -searchbase "OU=Workstations,DC=[redacted],DC=com" -properties 'Name' `
            | Where { ($_.DistinguishedName -notlike "*OU=Appliances,*") } | FT Name -hide | Out-String
            $computers = ($ADlist -split '[\r\n]') | ? {$_}