Mark Minasi's Tech Forum
Register Calendar Latest Topics Chat
 
 
 


Reply
  Author   Comment  
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 30
Reply with quote  #1 
I have a script that searches AD for specific attributes.


$listHR = Get-ADUser -Filter {department -eq $'HR' -and enabled -eq $true} -searchbase $ou | select -ExpandProperty sAMAccountName | Sort 

$listLegal = Get-ADUser -Filter {department -eq $Legal -and enabled -eq $true} -searchbase $ou | select -ExpandProperty sAMAccountName | Sort 


I want to be able to combine the results for the 2 variables - $listHR and $listLegal

I tried using Get-Content $listHR, $listLegal | Set-Content $listAll

but I get an error stating "Get-Content : Cannot convert 'System.Object[]' to the type 'System.String' 
required by parameter 'Path'
0
Pieter

Avatar / Picture

Senior Member
Registered:
Posts: 130
Reply with quote  #2 
$List=get-aduser -Properties * -SearchBase $ou  -LDAPFilter "(&(objectCategory=User)(|(Department=Legal)(Department=HR)))"

I always prefer LDAPfilter, just because I'm used to. No particular reason.

The filter says : Department must be "Legal" OR "HR", AND it must be a user.   The | character is an OR, the & is an AND constructor.




__________________
Pieter Demeulemeester
0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 30
Reply with quote  #3 
Thanks Pieter!  Your explanation is very helpful!   I am kind of partial to -Filter. [smile]  I was able to get it to work using:


$list = Get-ADUser -Filter {department -eq HR -and enabled -eq $true -or department -eq Legal -and enabled -eq $true} -searchbase $ou | select -ExpandProperty sAMAccountName | Sort 



0
meloao

New Friend (or an Old Friend who Built a New Account)
Registered:
Posts: 30
Reply with quote  #4 
Can the where command accept multiple parameters?  For example, I want to include additional test conditions for the physicalDeliveryOfficeName attribute.

It would get enabled user accounts if department is HR or Legal and if physicalDeliveryOfficeName is $location1 or $location. 

When I use:

$list = Get-ADUser -Filter {department -eq $HR -and enabled -eq $true} -searchbase $ou -Properties sAMAccountName, department |  select  sAMAccountName, physicalDeliveryOfficeName | where physicalDeliveryOfficeName -like $location1 -or  $location2  |  select -ExpandProperty sAMAccountName | Sort 

I am getting the following error:

Where-Object : A parameter cannot be found that matches parameter name 'or'.

I also tried:

where physicalDeliveryOfficeName -eq $location1 -or physicalDeliveryOfficeName -eq $location2 

0
JasonH

Avatar / Picture

Still Checking the Forum Out
Registered:
Posts: 1
Reply with quote  #5 
Try:
Code:

where {($_.physicalDeliveryOfficeName -like $location1) -or ($_.physicalDeliveryOfficeName -like $location2)}
0
jsclmedave

Administrator
Registered:
Posts: 418
Reply with quote  #6 
Quote:
Originally Posted by Pieter
$List=get-aduser -Properties * -SearchBase $ou  -LDAPFilter "(&(objectCategory=User)(|(Department=Legal)(Department=HR)))"

I always prefer LDAPfilter, just because I'm used to. No particular reason.

The filter says : Department must be "Legal" OR "HR", AND it must be a user.   The | character is an OR, the & is an AND constructor.





The & is an AND and the | is an OR btw...   Just figured that out this week...  : )

__________________
Tim Bolton @jsclmedave
Email: [string](0..20|%{[char][int](32+('527377347976847978324785847679797514357977').substring(($_*2),2))}) -replace ' '  

New to the forum? Please Read this
0
Previous Topic | Next Topic
Print
Reply

Quick Navigation: