Solved

Add searchbase to PS script

Posted on 2014-04-12
4
365 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 18

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 39

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 68

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
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…

910 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

21 Experts available now in Live!

Get 1:1 Help Now