Solved

Powershell to search an OU

Posted on 2014-03-27
10
873 Views
Last Modified: 2014-03-28
I'm trying to find a way to search an OU in powershell
OU=General_Use_PCs,OU=PC,OU=Wel_Computers,OU=domainname,OU=xxx, DC=xxx,DC=xxx,DC=Biz

Is there a way to make it search for Get-ADComputer -Filter {OperatingSystem -like "*XP*"} ?
0
Comment
Question by:WellingtonIS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 39959267
You can try with SearchBase parameter..
Get-ADComputer -Filter {OperatingSystem -like "*XP*"} -SearchBase "OU=General_Use_PCs,OU=PC,OU=Wel_Computers,OU=domainname,OU=xxx,DC=xxx,DC=xxx,DC=Biz"

Open in new window

0
 

Author Comment

by:WellingtonIS
ID: 39959411
PS N:\> Get-ADComputer -Filter {OperatingSystem -like "*XP*"} -SearchBase "OU=Ge
neral_Use_PCs,OU=PC,OU=Wel_Computers,OU=x,OU=Acute Facilities,DC=x,D
C=x,DC=Biz"
Get-ADComputer : Directory object not found
At line:1 char:15
+ Get-ADComputer <<<<  -Filter {OperatingSystem -like "*XP*"} -SearchBase "OU=G
eneral_Use_PCs,OU=PC,OU=Wel_Computers,OU=x,OU=Acute Facilities,DC=x
,DC=x,DC=Biz"
    + CategoryInfo          : ObjectNotFound: (:) [Get-ADComputer], ADIdentity
   NotFoundException
    + FullyQualifiedErrorId : Directory object not found,Microsoft.ActiveDirec
   tory.Management.Commands.GetADComputer

I'm getting this error.  I didn't get this before I tried to search the OU...

even better.. Is there a way to search  operatingsystem -like "*xp"  but to search PC's with WRM...?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39961177
From the error it looks like the distinguishedname of OU is incorrect in your code.. Error says the object not found in directory..

even better.. Is there a way to search  operatingsystem -like "*xp"  but to search PC's with WRM...?
Simple method to check the winrm service status on the computers. But you need to have a computer list or you can query the computers from AD and run the command against each of them using a foreach loop..

Get-Service Winrm -ComputerName 'ServerName'

Here is a sample code to collect the computer information from AD and check the WinRM service status..
Get-ADComputer -Filter {OperatingSystem -like "*XP*"} -SearchBase "OU=General_Use_PCs,OU=PC,OU=Wel_Computers,OU=domainname,OU=xxx,DC=xxx,DC=xxx,DC=Biz" -properties OperatingSystem | % {
$Server = $_
	Try {
	$Service = Get-Service Winrm -ComputerName $Server.Name -ErrorAction Stop
		New-Object PSobject -Property @{
		Server = $Server.Name
		OperatingSystem = $Server.OperatingSystem
		WinRM = $Service.Status
		}
	}
	Catch{
		New-Object PSobject -Property @{
		Server = $Server.Name
		OperatingSystem = $Server.OperatingSystem
		WinRM = "Error to connect"
		}
	}
}

Open in new window

0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:WellingtonIS
ID: 39961390
WRM is just a prefix in front of my PC names - I have wel and wrm as prefixes  I though If I could somehow search these prefixes I wouldn't need to use DN
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39961402
Lol... I thought you are talking about windows remote management...

Yes you can search using name..

Get-ADComputer -Filter {OperatingSystem -like "*XP*" -and name -like "WRM*"}

Open in new window


You may also e xport the list to csv file using..

Get-ADComputer -Filter {OperatingSystem -like "*XP*" -and name -like "WRM*"} | Export-csv C:\report.csv -nti

Open in new window

0
 

Author Comment

by:WellingtonIS
ID: 39961418
LOL!  That's what I thought based upon your answer!! :) thanks!!!  If I wish to add to the Like command "wrm" and Wel do I need to use a comma or type the command again?
So would it be -like "WRM","WEL" or WRM &...
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39961582
Try..
Get-ADComputer -Filter {OperatingSystem -like "*XP*" -and (name -like "WRM*" -or name -like "WEL*")}

Open in new window

0
 

Author Closing Comment

by:WellingtonIS
ID: 39961603
thanks.  do you know of a book  or a website that I can look at to understand how to use powershell?
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39961629
I had answered similar query yesterday.. Q_28398694

Learn Windows PowerShell in a Month of Lunches is a good book by Don Jones for beginners...
0
 

Author Comment

by:WellingtonIS
ID: 39961654
thanks.  I'll look into it.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question