Solved

Add searchbase to PS script

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
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…

808 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