[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 931
  • Last Modified:

Powershell to search an OU

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
WellingtonIS
Asked:
WellingtonIS
  • 5
  • 5
1 Solution
 
SubsunCommented:
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
 
WellingtonISAuthor Commented:
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
 
SubsunCommented:
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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
WellingtonISAuthor Commented:
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
 
SubsunCommented:
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
 
WellingtonISAuthor Commented:
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
 
SubsunCommented:
Try..
Get-ADComputer -Filter {OperatingSystem -like "*XP*" -and (name -like "WRM*" -or name -like "WEL*")}

Open in new window

0
 
WellingtonISAuthor Commented:
thanks.  do you know of a book  or a website that I can look at to understand how to use powershell?
0
 
SubsunCommented:
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
 
WellingtonISAuthor Commented:
thanks.  I'll look into it.
0

Featured Post

Independent Software Vendors: 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!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now