Solved

Need Help With Powershell Script

Posted on 2008-10-08
11
769 Views
Last Modified: 2013-12-24
Hello. I am looking for some assistance creating a Powershell Script to do the following.

1. List all users of a specific group in active directory and write to an XLS

2. Then I want to use the users in the XLS and get the email  address's dumped to an XLS.

This is what I have found to start with, however it doesn't return any thing within PS.

$root=([ADSI]"").distinguishedName
$Group = [ADSI]("LDAP://CN=Domain Admins, CN=Users,"+ $root)
$Group.member




Any Powershell Guru's here ?

Thanks
0
Comment
Question by:MelittaChickadee
[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
  • 5
  • 4
  • 2
11 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 22676874

I would approach it from the other way. Searching AD for the members allows us to return any property we might need in a single search.

HTH

Chris

$Root = [ADSI]""
 
# Construct the search filter
$Filter = "(memberOf=CN=Domain Admins,CN=Users," + $root.distinguishedName + ")"
 
# Create a searcher
$ADSearch = New-Object System.DirectoryServices.DirectorySearcher($Root, $Filter)
 
# Add properties we're interested in the to result set
$PropertyNo = $ADSearch.PropertiesToLoad.Add("sAMAccountName")
$PropertyNo = $ADSearch.PropertiesToLoad.Add("mail")
 
# Sort the results into an exportable object
$Results = $ADSearch.FindAll() | Select-Object `
  @{n="sAMAccountName";e={$_.Properties.samaccountname}}, `
  @{n="mail";e={$_.Properties.mail}}
 
# Write the output to a CSV
$Results | Export-CSV -Path "outputfile.csv"

Open in new window

0
 
LVL 18

Accepted Solution

by:
BSonPosh earned 125 total points
ID: 22678544
If you are going to be doing a lot of Active Directory stuff I would recommend using the free Quest cmdlets located here. www.quest.com/powershell

Then you can do something like
Get-QADGroupMember "Domain Admins" | select name,email | export-csv bleh.csv

Open in new window

0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 22678660

Welcome back BSonPosh :) I thought we'd lost you for a while.

Chris
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 18

Expert Comment

by:BSonPosh
ID: 22678726
I am here all the time :)

I just have been focusing on the Powershell questions.

You on the other hand have been quite busy :)
0
 

Author Comment

by:MelittaChickadee
ID: 22698085
Hello Chris and BSonPosh,

I appreciate the help. When I run the script as is, I receive the folllowing:
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At line:1 char:22
+ $Results | Export-CSV  <<<< -Path "outputfile.csv"

I would imagine I need to put some variables into this ?

Again, appreciate your help.
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 22698294
That error implies that $results is NULL. Did you try my Quest example?
0
 

Author Comment

by:MelittaChickadee
ID: 22705695
Hello BSonPosh,

Yes your recommendation to use Quest cmdlets work great!  I will give you the Accepted Solution. On a side note, how tough is it to do the following in PS using the Quest cmdlets ?

1. Search AD For any user with *whatever.com  email address, export to csv.
2. Then do a bulk edit to change with *.whomever.com email address.

-or-

1. Search AD for any user with *whatever.com email address then simply do a bulk edit to give them a new primary SMTP address of *.whomever.com ?

THoughts ? Again..appreciate your help!

Thanks,
Brian
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 22705878
It would not be that complicated at all. Something like this [1]

[1] means I didn't test it

btw... I am starting a weekly series on www.turbochargead.org that will cover these cmdlets


$users = Get-Qaduser -ldapfilter "mail=*whatever.com" 
foreach($user in $users)
{
   $user | set-qaduser -objectattributes @{mail="$($user.name)@whomever.com"} -whatif
}

Open in new window

0
 

Author Comment

by:MelittaChickadee
ID: 22706257
I am going to test out of prod , and I will let you know. Anyway I could get the results written to a csv?

Thanks,
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 22706292
you can almost always use export-csv.

Get-Qaduser -ldapfilter "mail=*whatever.com" | export-csv myfile.csv
0
 

Author Comment

by:MelittaChickadee
ID: 22714000
BSonPosh,

Ran the following :
Qaduser -ldapfilter "mail=*whatever.com"
foreach($user in $users)
{
   $user | set-qaduser -objectattributes @{mail="$($user.name)@whomever.com"} -whatif
}


Nothing happens...just returns me to a >>

Help ?


0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Temporary Write Permission for a Drive 15 55
Data architecture learning. 17 47
Best way to get data into a database 12 56
Powershell WPF, Redirection Issue 2 30
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

752 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