?
Solved

Powershell to search an OU

Posted on 2014-03-27
10
Medium Priority
?
894 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…

770 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