?
Solved

Need help with Get-Contact powershell command

Posted on 2010-08-19
14
Medium Priority
?
1,823 Views
Last Modified: 2012-05-10
We have about 1200 Contact objects in our AD. I need an Exchange Powershell command (Probably the Get-Contact) to search through all these Contacts and find out which ones are members of a particular Distribution List. I am sure it's something like Get-Contact | where-object...etc.etc. Does anyone have this command available. Like if I want to find out all Contacts that are members of the Distribution Group "All Employees". What would the command be.
Of course, if I have to approach it the other way, like search all DL's where membership contains a certain Contact or group of Contacts, that would work also but it would be more cumbersome because I would have to pass it a list of Contacts. LIke search all DL's and return a list that has any of these Contacts. Easy one for a powershell Guru. We are running Exchange 2007 SP1.
0
Comment
Question by:osiexchange
[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
  • 7
  • 6
14 Comments
 
LVL 32

Expert Comment

by:endital1097
ID: 33476335
the following can be saved as Get-ContactMemberOf.ps1 then run

.\get-contactmemberof.ps1 someone@contoso.com
function Get-GroupMembership($email)
{	# Verify arguments
	if(!$email)
	{	Write-Host "You must specify the email address of the user." -ForegroundColor Yellow
		break
	}

	# Set the LDAP URL to the container DN specified on the command line
	$LdapURL = "LDAP://dc=contoso,dc=com"

	# Initialize a DirectorySearcher object
	$searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]$LdapURL)

	# Set the attributes that you want to be returned from AD
	$searcher.SearchScope = "Subtree"

	$filter = "(mail=" + $email + ")"
	$searcher.Filter = $filter

	# Run the LDAP Search request against AD
	$contacts = $searcher.FindAll()

	foreach ($contact in $contacts)
	{	[string]$name = $contact.Properties.displayname
		$objContact = $contact.GetDirectoryEntry()
		if($objContact.memberof.count -ne 0)
		{	foreach($m in $objContact.memberof)
			{	$m
			}
		}
	}
}

Get-GroupMembership $args[0]

Open in new window

0
 

Author Comment

by:osiexchange
ID: 33476468
Got this error: Also, is it possible to pass a list of addresses instead of one at a time.

Exception calling "FindAll" with "0" argument(s): "A referral was returned from the server.
"
At C:\Program Files\Microsoft\Exchange Server\Scripts\get-membershipof.ps1:21 char:31
+     $contacts = $searcher.FindAll <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

You cannot call a method on a null-valued expression.
At C:\Program Files\Microsoft\Exchange Server\Scripts\get-membershipof.ps1:25 char:43
+         $objContact = $contact.GetDirectoryEntry <<<< ()
    + CategoryInfo          : InvalidOperation: (GetDirectoryEntry:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
0
 
LVL 3

Expert Comment

by:DesertCroc
ID: 33476483
Closest I can guess...I don't have EMS available right now...
get-recipientt | where{$_.addresslistmembership -like "blah*"}
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 32

Expert Comment

by:endital1097
ID: 33476625
make sure your LdapUrl is correct

you could take run
"contact1","contact2" | get-contactmemberof.ps1 $_
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33476655
sorry it would be
"contact1","contact2" | ForEach-Object { $_; .\memberof.ps1 $_ }

of course you could use a text file or csv
0
 

Author Comment

by:osiexchange
ID: 33476766
Thanks. The LDAP was incorrect. The command works well but I had problems with the input. When I specify just one Contact like "Contact1" | get-contactmembershipof.ps1, it just comes back and tells me to specify an email address. I included the $_ but no difference. I am a noob at scripting hence the questions. Also, is there any way to specific an input file like a list of Contact names in notepad or Excel?
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33477065
Need to adjust the ldap filter to meet your needs
lastname, firstnane, etc
0
 

Author Comment

by:osiexchange
ID: 33477411
Sorry but I don't understand your last post. What are you saying?
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33477584
you need to update the following line
$filter = "(mail=" + $email + ")"

if you are using lastname for your contact it would be:
$filter = "(sn=" + $email + ")"

if you are using displayName it would be:
$filter = "(displayName=" + $email + ")"

0
 

Author Comment

by:osiexchange
ID: 33478292
Thanks for all your help on this.
This is what I am typing:
"Mickey Mouse" | ForEach-Object { $_; .\getmembershipof.ps1 $_ }

This is the error it returns. ( I played with the name of the script but verified it is correct)

The term '.\getmembershipof.ps1' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the pat
h is correct and try again.
At line:1 char:60
+ "Mickey Mouse" | ForEach-Object { $_; .\getmembershipof.ps1 <<<<  $_ }
    + CategoryInfo          : ObjectNotFound: (.\getmembershipof.ps1:String) [], CommandNotFoundEx
   ception
    + FullyQualifiedErrorId : CommandNotFoundException

Is my syntax correct? As I mentioned, I am at noob at this. Also, you did say you can use an input file. What is the command for that?
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33478361
make sure your shell is in the directory where the ps1 file resides
you can always change it to
"Mickey Mouse" | ForEach-Object { $_; c:\scripts\getmembershipof.ps1 $_ }

like the disney ref

0
 

Author Comment

by:osiexchange
ID: 33478773
Perfect!! That worked. Just one last question. You mentioned you can use an input file. I have both Excel and Notepad versions of a list of users. Without typing all the names in, how would I use the file as an input to this command?
0
 
LVL 32

Accepted Solution

by:
endital1097 earned 2000 total points
ID: 33478794
yes, with the following

get-content c:\contact.txt | ForEach-Object { $_; c:\scripts\getmembershipof.ps1 $_ }
0
 

Author Closing Comment

by:osiexchange
ID: 33484260
Thanks. Worked just fine.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
A couple of months ago we ran into an issue that necessitated re-creating our Edge Subscriptions. However, when we attempted to execute the command: New-EdgeSubscription -filename C:\NewEdgeSub_01.xml we received an error indicating that the LDAP se…
how to add IIS SMTP to handle application/Scanner relays into office 365.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

764 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