Solved

Powershell to search an OU

Posted on 2014-03-27
10
815 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
  • 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now