Wildcard to search Active Directory computers

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
epoh97
Posts: 102
Last visit: Wed Apr 18, 2018 6:16 am

Wildcard to search Active Directory computers

Post by epoh97 »

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?
Code: [Select all] [Expand/Collapse] [Download] (Get-DomainPCs.ps1)
  1. get-domainPCs
  2.  
  3. $comps = Get-DomainPCs
  4. foreach ($comp in $comps){
  5. $comp
  6. Get-WmiObject win32_bios -ComputerName $comp
  7. Get-WmiObject win32_computuersystem -ComputerName $comp
  8. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Wildcard to search Active Directory computers

Post by jvierra »

You forgot to post your code.
User avatar
epoh97
Posts: 102
Last visit: Wed Apr 18, 2018 6:16 am

Re: Wildcard to search Active Directory computers

Post by epoh97 »

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
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Wildcard to search Active Directory computers

Post by jvierra »

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
Last visit: Wed Apr 18, 2018 6:16 am

Re: Wildcard to search Active Directory computers

Post by epoh97 »

It queries AD and lists all computer names in the domain
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Wildcard to search Active Directory computers

Post by jvierra »

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
Last visit: Wed Apr 18, 2018 6:16 am

Re: Wildcard to search Active Directory computers

Post by epoh97 »

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

Results:
COMP1
COMP2
PC1
PC3

Wildcard example:
Get-DomainPCs COMP*
COMP1
COMP2
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Wildcard to search Active Directory computers

Post by jvierra »

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
Last visit: Wed Apr 18, 2018 6:16 am

Re: Wildcard to search Active Directory computers

Post by epoh97 »

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

=============
No help, just that code
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Wildcard to search Active Directory computers

Post by jvierra »

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.
This topic is 6 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked