Solved

Add searchbase to PS script

Posted on 2014-04-12
4
371 Views
Last Modified: 2014-04-18
Hi all ..

Can someone help me modify this so my searchbase is an OU?
and it outpts the results to a csv file ?


Import-Module ActiveDirectory
$contents = gc Users.txt
foreach ($obj in $contents)
{
      if ((Get-ADUser $obj -properties memberof).memberof.count -ge 1)
      {
            $obj
      }
}
0
Comment
Question by:MilesLogan
4 Comments
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39996771
Try this (not tested)

Import-Module ActiveDirectory
$contents = gc Users.txt
foreach ($obj in $contents)
{
      if ((Get-ADUser $obj -Filter * -SearchBase "OU=department,DC=domain,DC=COM" -properties memberof).memberof.count -ge 1)
      {
            $obj
      }
}
0
 
LVL 40

Accepted Solution

by:
footech earned 250 total points
ID: 39996931
I'm really kind of surprised by this question (at least part of it).  It seems you're already aware of the -searchbase parameter so what is it that you're not understanding?  Next, what information do you want to output (i.e. what properties)?

To pipe to Export-CSV it's usually better to use ForEach-Object cmdlet since that supports the pipeline.  However, if you really want to use the foreach statement, surround the entire statement with array notation - @() - like the following pseudo-code.
@(foreach ($a in $b)
{  # do something
}) | Export-CSV file.csv

Open in new window


For what you appear to be doing, using Where-Object to filter the results of a query is better (and simpler) than using an If statement.
Import-Module ActiveDirectory
gc Users.txt | ForEach { Get-ADUser $_ -properties memberof -searchbase "ou=someOU,DC=domain,dc=com" } | Where { $_.memberof.count -ge 1 } | Export-CSV file.csv -notype

Open in new window

0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 39997195
With "output the results", do you want to have the name only (as is shown in your question - $obj just contains a string building the user name), or the AD user object?

If the latter, note that the MemberOf property (and other properties containing more than one object) won't output to a CSV. You can have string representation as seen on console, or e.g. the count of groups the user is a member of.
Also, it doesn't make much sense to have internal properties like the GUID in the CSV.

I agree with footech about the pipeline stuff. It can get simpler even, as we don't need the foreach:
Import-Module ActiveDirectory
gc Users.txt |
  Get-ADUser -properties MemberOf -searchBase 'ou=someOU,DC=domain,dc=com' |
  Where { $_.memberof.count } |
  select Name, DistinguishedName, UserPrincipalName, @{n='GroupCount'; e={$_.MemberOf.Count}} |
  Export-CSV file.csv -notype

Open in new window

0
 
LVL 2

Author Closing Comment

by:MilesLogan
ID: 40009871
Thank you both !
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

820 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