Wildcard to search Active Directory computers

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
Locked
User avatar
epoh97
Posts: 102
Joined: Mon Jun 04, 2007 12:37 am

Wildcard to search Active Directory computers

Post by epoh97 » Thu Nov 30, 2017 8:50 am

I use this function in my profile to list all domain computers. I would like to be able to filter the list with a wildcard search for computers. Is this possible?
  1. get-domainPCs
  2.  
  3.  
  4.  
  5. $comps = Get-DomainPCs
  6.  
  7. foreach ($comp in $comps){
  8.  
  9. $comp
  10.  
  11. Get-WmiObject win32_bios -ComputerName $comp
  12.  
  13. Get-WmiObject win32_computuersystem -ComputerName $comp
  14.  
  15. }

User avatar
jvierra
Posts: 11743
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Wildcard to search Active Directory computers

Post by jvierra » Thu Nov 30, 2017 8:55 am

You forgot to post your code.

User avatar
epoh97
Posts: 102
Joined: Mon Jun 04, 2007 12:37 am

Re: Wildcard to search Active Directory computers

Post by epoh97 » Thu Nov 30, 2017 1:12 pm

That's odd. I can see it in my previous post. I posted it as PoSH code. Here it is as text...

get-domainPCs

$comps = Get-DomainPCs
foreach ($comp in $comps){
$comp
Get-WmiObject win32_bios -ComputerName $comp
Get-WmiObject win32_computuersystem -ComputerName $comp
}

User avatar
jvierra
Posts: 11743
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Wildcard to search Active Directory computers

Post by jvierra » Thu Nov 30, 2017 1:19 pm

Ahh. I see. It was created as a collapsed block. It shows when expanded.. This is likely because you pasted formatted code.

The code you posted does not get an computers. What is Get-DomainPCs? What does it return?

User avatar
epoh97
Posts: 102
Joined: Mon Jun 04, 2007 12:37 am

Re: Wildcard to search Active Directory computers

Post by epoh97 » Fri Dec 01, 2017 7:15 am

It queries AD and lists all computer names in the domain

User avatar
jvierra
Posts: 11743
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Wildcard to search Active Directory computers

Post by jvierra » Fri Dec 01, 2017 10:43 am

What is it yo are considering a "wildcard" search?

To filter any collection use the "Where-Object" CmdLet.

help where-object -online

User avatar
epoh97
Posts: 102
Joined: Mon Jun 04, 2007 12:37 am

Re: Wildcard to search Active Directory computers

Post by epoh97 » Fri Dec 01, 2017 11:16 am

On the computer names, in the list of returned items.

Results:
COMP1
COMP2
PC1
PC3

Wildcard example:
Get-DomainPCs COMP*
COMP1
COMP2

User avatar
jvierra
Posts: 11743
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Wildcard to search Active Directory computers

Post by jvierra » Fri Dec 01, 2017 11:37 am

Since we don't know what your GEt-DomainPCs does there is no way to know if that will work. Can you ask the original author for help? Does the CmdLet have help?

User avatar
epoh97
Posts: 102
Joined: Mon Jun 04, 2007 12:37 am

Re: Wildcard to search Active Directory computers

Post by epoh97 » Mon Dec 04, 2017 11:43 am

function Get-DomainPCs
{
$domainComps =([adsisearcher]'objectCategory=computer').FindAll()|%{$_.Properties['name']}
$domainComps = $domainComps | Sort-Object
$domainComps
}

=============
No help, just that code

User avatar
jvierra
Posts: 11743
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Wildcard to search Active Directory computers

Post by jvierra » Mon Dec 04, 2017 11:57 am

The function cannot use wild cards as it is built to return all computer objects by object type and not by name. Filter the list after the function.

Locked