Get-aduser in an exhange powershell scripy

Hi I have a scipt pasted below that pulls out mailbox statistics and this works grate. But it laks the OU so I am trying to use get-aduser to get this info but it fails. If  search for $mb.alis for examle jhon.doe manually in AD users and computers it works fine.

so I am missing something. Can someone give me some guidance? 


$mailboxes=get-mailbox -results Unlimited
$results=ForEach($mb in $mailboxes){
	$stats=get-mailboxstatistics $mb
	$props=@{
		alias=$mb.alias
		DisplayName=$mb.displayname
		PrimarySmtpAddress=$mb.PrimarySmtpAddress
		StorageLimitStatus=$stats.StorageLimitStatus
		TotalItemSize=$stats.totalitemsize
		DatabaseName=$stats.databasename
		ProhibitSendQuota=$mb.ProhibitSendQuota
		ProhibitsendReceiveQuota=$mb.ProhibitsendReceiveQuota
		IssueWarningQuota=$mb.IssueWarningQuota
		#
		# Here my problems begins, I am doing something wrong
		#
		DistinguishedName=(Get-ADUser -identity $mb.alias | select DistinguishedName)
		# DistinguishedName=(Get-ADUser -filter {SamAccountName -eq $mb.alias} | select DistinguishedName)
		SamAccountName=Get-ADUser -identity $mb.alias | select SamAccountName
		 
		#userinfo=Get-ADUser $alias -Properties DistinguishedName,SamAccountName |  select DistinguishedName,SamAccountName
	
	}
	New-Object PsObject -Property $props

}
$results | Sort-Object TotalItemSize -descending | ft -auto
#$results | Sort-Object TotalItemSize -descending | export-csv G:\rappoter\Exchange_mailboxes.csv

Get-Aduser : Cannot find an object with identity: 'NN.NN' under: 'DC=DOAMIN,DC=local'.
At C:\scripts\exchange2.ps1:17 char:32
+         DistinguishedName=(Get-Aduser <<<<  -identity $mb.alias | locallect DistinguishedName)
    + CategoryInfo          : ObjectNotFound: (NN.NN:Aduser) [Get-Aduser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : Cannot find an object with identity: 'NN.NN' under: 'DC=DOAMIN,DC=local'.,Microsoft.ActiveDirectory.Management.Commands.GetAduser

July 17th, 2015 9:15am

Your script worked for me.  Is the alias the same format for all users?  You could try $($mb.alias) Just wonder if the format of your alias vs identity is allowed.

Is the error only on this one particular user?

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 9:26am

Many mailboxes are not associated with an AD account.  YOU have to fiter for mailboxes that actually are in AD.

Try this:

$mb=Get-Mailbox joe.smith
$user=Get-AdUser $mb.alias

Here is how to get only valid accounts mailboxes:

$mailboxes=Get-AdUser -filter *|%{Get-Mailbox $_.samaccountname -ea 0}

July 17th, 2015 9:30am

Your script worked for me.  Is the alias the same format for all users?  You could try $($mb.alias) Just wonder if the format of your alias vs identity is allowed.

Is the error only on this one particular user?

Free Windows Admin Tool Kit Click here and download it now
July 17th, 2015 1:10pm

Your script worked for me.  Is the alias the same format for all users?  You could try $($mb.alias) Just wonder if the format of your alias vs identity is allowed.

Is the error only on this one particular user?

July 17th, 2015 1:10pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics