Solved

Add searchbase to PS script

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

706 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

15 Experts available now in Live!

Get 1:1 Help Now