Solved

Powershell to search an OU

Posted on 2014-03-27
10
829 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 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

8 Experts available now in Live!

Get 1:1 Help Now