Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add searchbase to PS script

Posted on 2014-04-12
4
Medium Priority
?
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 41

Accepted Solution

by:
footech earned 1000 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 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…

670 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