tdubb123
Expert
Expert

get-adcomputer

is it possible to get a list of non virtual servers in the domain with get-adcomputer?

0 Kudos
13 Replies
SyApps
Contributor
Contributor

If you are logged into a computer that's on the network and running vcli then just doing a net view would work. You can't manage them and run commands for them through the vcli in the same respect as you could a VM, so simply listed machines on the network can be done through the command prompt as you normally could.

Maybe we'd be able to help you better if you replied stating exactly what you are trying to accomplish.

Good Luck!

Always a big thanks to the community in advance! Dan Lee
0 Kudos
LucD
Leadership
Leadership

Afaik none of the AD object properties will tell you if a machine is virtual or not (unless you added some properties yourself to the schema).

I sometimes use a combination of Get-ADComputer and Get-WMIObject to do this filtering.

Something like this

Get-ADComputer -Filter {OperatingSystem -like "*server*"} | `

where {(Get-WmiObject -Class Win32_BIOS).SerialNumber -notlike "VMware*"} | `

Select Name

If you want the virtual servers, just replace -notlike with -like.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

i tried that but it seems to bring up a list of both virtual and physical.

0 Kudos
RvdNieuwendijk
Leadership
Leadership

You can make a list of VM's with PowerCLI and compare the computers that Get-ADComputer returns with this list, to select only the computers that are not in the list. The next script does this. It assumes that the computernames in Active Directory are the same as the VM names.

$VmList = @()
Get-VM | `
  ForEach-Object {$VmList += $_.Name}
Get-ADComputer -Filter {OperatingSystem -like "*server*"} | `
  Where-Object {$VmList -notcontains $_.Name}

I made another version of this script that uses the servername of the guest instead of the VM name. This will give better results if your VM names are different than the guest names. It uses the Guest.HostName property of a VM. This property will be empty if the VMware Tools are not installed. That's why I added a filter to select only the VM's that have this property filled, before I add them to the list of VM's.

$VmList = @()
Get-VM | `
  Where-Object {$_.Guest.HostName} | `
  ForEach-Object {$VmList += $_.Guest.HostName.Split(".")[0]}
Get-ADComputer -Filter {OperatingSystem -like "*server*"} | `
  Where-Object {$VmList -notcontains $_.Name}

Regards, Robert

Message was edited by: RvdNieuwendijk. Added the filter for servers and added the second script

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
LucD
Leadership
Leadership

Perhaps the filtering is not correct.

Does this show you all machines in the domain and is the OperatingSystem property distinguisable from PCs by the presence of the term "server" in that property ?

Get-ADComputer -Filter * -Properties OperatingSystem | Select Name,OperatingSystem


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

yes this show all machines including desktops. and it does show the Operating system of desktops and servers.

Get-ADComputer -Filter {OperatingSystem -Like "*Server*"} -Properties OperatingSystem | where {(Get-Wmi
Object -Class WIN32_BIOS).SerialNumber -notlike "VMware*"} | Select Name,OperatingSystem
and

Get-ADComputer -Filter {OperatingSystem -Like "*Server*"} -Properties OperatingSystem | Select Name,Ope

ratingSystem

seems to give the same output. maybe something wrong with the wmi filter?

0 Kudos
LucD
Leadership
Leadership

Not sure what goes wrong here.

Can you try the folllowing with a couple of servers

Get-WmiObject -Class WIN32_BIOS -ComputerName ServerName

and check if you see the SerialNumber property starting with "VMware" only when the <ServerName> machine is a virual box ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

yes it does. it gives VMware  in a virtual box.

for the wmi filter, the machine has to be online to work right?

0 Kudos
tdubb123
Expert
Expert

I tried this and it seems to work.

> Get-ADComputer -Filter {OperatingSystem -Like "*Server*"} -Properties OperatingSystem | where {(Get-Wmi

Object -Class WIN32_BIOS -ComputerName $_.Name).SerialNumber -NotLike "VMware*"} | Select Name,OperatingSystem

however a lot of these errors:

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

At line:1 char:110

+ Get-ADComputer -Filter {OperatingSystem -Like "*Server*"} -Properties OperatingSystem | where {(Get-WmiObject <<<<  -

Class WIN32_BIOS -ComputerName $_.Name).SerialNumber -NotLike "VMware*"} | Select Name,OperatingSystem

    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException

    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

how do I modify the query to ignore the offline servers?

0 Kudos
LucD
Leadership
Leadership

My mistake, I assumed that the ComputerName parameter would take pipeline input, and it doesn't.

To avoid offline hosts, you can do something like this

Get-ADComputer -Filter {OperatingSystem -Like "*Server*"} -Properties OperatingSystem | `

where {(Get-WmiObject -Class WIN32_PingStatus -filter "address = '$_.Name'").StatusCode -eq 0} | `

where {(Get-WmiObject -Class WIN32_BIOS -ComputerName $_.Name).SerialNumber -NotLike "VMware*"} | `

Select Name,OperatingSystem

The first Where-clause will only pass hosts that reply to a ping and hence are online.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

i am getting a blank list. with the second wmi filter

0 Kudos
LucD
Leadership
Leadership

Did you try the WIN32_PingStatus on it's own to validate you get the StatusCode 0 for online hosts ?

Just to make sure, you did escape the variable in the filter with single quotes ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert

yes I added the single quotes in there.

PS C:\> Get-WmiObject -class WIN32_PingStatus -Filter ("address='$_'") | select statuscode
                                                                                                             statuscode
                                                                                                             ----------
                                                                                                                      0
PS C:\> Get-WmiObject -class WIN32_PingStatus -Filter ("address='$_'") -computername xxx | select statuscode
                                                                                                             statuscode
                                                                                                             ----------
                                                                                                                      0
PS C:\> Get-WmiObject -class WIN32_PingStatus -Filter ("address='$_'") -computername yyy | select statuscode
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:14
+ Get-WmiObject <<<<  -class WIN32_PingStatus -Filter ("address='$_'") -computername padc2 | select statuscode
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
0 Kudos