Powershell Assistance to Include Get-Mailbox and Get-Mailboxstatics
Hello,
I need some assistance on writing a PS command to get some information out of Exchange. I need the information to be exported to CSV so I can manage the data in Excel.
Data Needed:
DisplayName,PrimarySMTPAddress,Office,DatabaseName,LastLogonTime,TotalItemSize,ItemCount
I can get all of the information in seperate Get-Mailbox and Get-Mailboxstatics separately but I would rather avoid having to merge to Excel files together.
This is the command below that is giving me trouble.
[PS] C:\Windows\system32>Get-mailbox | Get-MailboxStatistics | select DisplayName,PrimarySMTPAddress,Office,{$_.TotalItemSize.Value.ToKB)},{$_.TotalItemSize.Value.ToMB()},ItemCount,DatabaseName,LastLogonTime | Export-Csv c:\mb5.csv -NoTypeInformation
Can someone let me know what I am doing wrong? ThanksJason
June 7th, 2012 4:49pm
Hello Rich,
Thanks for your post and my apologies on this question but how do I use this command exactly?Jason
Free Windows Admin Tool Kit Click here and download it now
June 7th, 2012 9:59pm
On Fri, 8 Jun 2012 01:59:55 +0000, Jason B. Williams wrote:
>Thanks for your post and my apologies on this question but how do I use this command exactly?
Save it to a file with a .ps1 extension. change to the directory that
contains the file and enter ./<filename>.ps1
---
Rich Matheisen
MCSE+I, Exchange MVP
--- Rich Matheisen MCSE+I, Exchange MVP
June 7th, 2012 10:14pm
Thanks Rich,
I am still getting a ton of errors on this script.
[PS] C:\>./mb.ps1
The term 'DisplayName' is not recognized as a cmdlet, function, operable prog
m, or script file. Verify the term and try again.
At C:\MB.ps1:5 char:12
+ DisplayName, <<<< PrimarySMTPAddress,Office,'TotalItemSize(KB)','TotalItemS
e(MB)',ItemCount,DatabaseName,LastLogonTime
Property 'DisplayName' cannot be found on this object; make sure it exists an
is settable.
At C:\MB.ps1:6 char:4
+ $x.D <<<< isplayName = $_.Displayname
Property 'PrimarySMTPAddress' cannot be found on this object; make sure it ex
ts and is settable.
At C:\MB.ps1:7 char:4
+ $x.P <<<< rimarySMTPAddress = $_.PrimarySMTPAddress
Property 'Office' cannot be found on this object; make sure it exists and is
ttable.
At C:\MB.ps1:8 char:4
+ $x.O <<<< ffice = $_.Office
Property '(TotalItemSize(KB)' cannot be found on this object; make sure it ex
ts and is settable.
At C:\MB.ps1:9 char:4
+ $x.' <<<< (TotalItemSize(KB)' = $s.TotalItemSize.Value.ToKB()
Property '(TotalItemSize(MB)' cannot be found on this object; make sure it ex
ts and is settable.
At C:\MB.ps1:10 char:4
+ $x.' <<<< (TotalItemSize(MB)' = $s.TotalItemSize.Value.ToMB()
Property 'ItemCount' cannot be found on this object; make sure it exists and
settable.
At C:\MB.ps1:11 char:4
+ $x.I <<<< temCount = $s.Itemcount
Property 'DatabaseName' cannot be found on this object; make sure it exists a
is settable.
At C:\MB.ps1:12 char:4
+ $x.D <<<< atabaseName = $_.Database
Property 'LastLogonTime' cannot be found on this object; make sure it exists
d is settable.
At C:\MB.ps1:13 char:4
+ $x.L <<<< astLogonTime = $_.LastLogonTime
The term 'DisplayName' is not recognized as a cmdlet, function, operable prog
m, or script file. Verify the term and try again.
At C:\MB.ps1:5 char:12
+ DisplayName, <<<< PrimarySMTPAddress,Office,'TotalItemSize(KB)','TotalItemS
e(MB)',ItemCount,DatabaseName,LastLogonTime
Property 'DisplayName' cannot be found on this object; make sure it exists an
is settable.
At C:\MB.ps1:6 char:4
+ $x.D <<<< isplayName = $_.Displayname
Property 'PrimarySMTPAddress' cannot be found on this object; make sure it ex
ts and is settable.
At C:\MB.ps1:7 char:4
+ $x.P <<<< rimarySMTPAddress = $_.PrimarySMTPAddress
Property 'Office' cannot be found on this object; make sure it exists and is
ttable.
At C:\MB.ps1:8 char:4
+ $x.O <<<< ffice = $_.Office
Property '(TotalItemSize(KB)' cannot be found on this object; make sure it ex
ts and is settable.
At C:\MB.ps1:9 char:4
+ $x.' <<<< (TotalItemSize(KB)' = $s.TotalItemSize.Value.ToKB()
Property '(TotalItemSize(MB)' cannot be found on this object; make sure it ex
ts and is settable.
At C:\MB.ps1:10 char:4
+ $x.' <<<< (TotalItemSize(MB)' = $s.TotalItemSize.Value.ToMB()
Property 'ItemCount' cannot be found on this object; make sure it exists and
settable.
At C:\MB.ps1:11 char:4
+ $x.I <<<< temCount = $s.Itemcount
Property 'DatabaseName' cannot be found on this object; make sure it exists a
is settable.Jason
Free Windows Admin Tool Kit Click here and download it now
June 7th, 2012 10:26pm
Also, I am using Exchange 2007.Jason
June 7th, 2012 10:26pm
On Fri, 8 Jun 2012 02:26:22 +0000, Jason B. Williams wrote:
>
>
>Thanks Rich,
>
>I am still getting a ton of errors on this script.
>
>
>
>[PS] C:\>./mb.ps1 The term 'DisplayName' is not recognized as a cmdlet, function, operable prog m, or script file. Verify the term and try again. At C:\MB.ps1:5 char:12 + DisplayName, <<<< PrimarySMTPAddress,Office,'TotalItemSize(KB)','TotalItemS e(MB)',ItemCount,DatabaseName,LastLogonTime
Property 'DisplayName' cannot be found on this object; make sure it exists an is settable. At C:\MB.ps1:6 char:4 + $x.D <<<< isplayName = $_.Displayname Property 'PrimarySMTPAddress' cannot be found on this object; make sure it ex ts and is settable. At C:\MB.ps1:7
char:4 + $x.P <<<< rimarySMTPAddress = $_.PrimarySMTPAddress Property 'Office' cannot be found on this object; make sure it exists and is ttable. At C:\MB.ps1:8 char:4 + $x.O <<<< ffice = $_.Office Property '(TotalItemSize(KB)' cannot be found on this object;
make sure it ex ts and is settable. At C:\MB.ps1:9 char:4 + $x.' <<<< (TotalItemSize(KB)' = $s.TotalItemSize.Value.ToKB() Property '(TotalItemSize(MB)' cannot be found on this
>object; make sure it ex ts and is settable. At C:\MB.ps1:10 char:4 + $x.' <<<< (TotalItemSize(MB)' = $s.TotalItemSize.Value.ToMB() Property 'ItemCount' cannot be found on this object; make sure it exists and settable. At C:\MB.ps1:11 char:4 + $x.I <<<<
temCount = $s.Itemcount Property 'DatabaseName' cannot be found on this object; make sure it exists a is settable. At C:\MB.ps1:12 char:4 + $x.D <<<< atabaseName = $_.Database Property 'LastLogonTime' cannot be found on this object; make sure it exists d is
settable. At C:\MB.ps1:13 char:4 + $x.L <<<< astLogonTime = $_.LastLogonTime The term 'DisplayName' is not recognized as a cmdlet, function, operable prog m, or script file. Verify the term and try again. At C:\MB.ps1:5 char:12 + DisplayName, <<<< PrimarySMTPAddress,Office,'TotalItemSize(KB)','TotalItemS
e(MB)',ItemCount,DatabaseName,LastLogonTime Property 'DisplayName' cannot be found on this object; make sure it exists an is settable. At C:\MB.ps1:6 char:4 + $x.D <<<<
isplayName
>= $_.Displayname Property 'PrimarySMTPAddress' cannot be found on this object; make sure it ex ts and is settable. At C:\MB.ps1:7 char:4 + $x.P <<<< rimarySMTPAddress = $_.PrimarySMTPAddress Property 'Office' cannot be found on this object; make sure
it exists and is ttable. At C:\MB.ps1:8 char:4 + $x.O <<<< ffice = $_.Office Property '(TotalItemSize(KB)' cannot be found on this object; make sure it ex ts and is settable. At C:\MB.ps1:9 char:4 + $x.' <<<< (TotalItemSize(KB)' = $s.TotalItemSize.Value.ToKB()
Property '(TotalItemSize(MB)' cannot be found on this object; make sure it ex ts and is settable. At C:\MB.ps1:10 char:4 + $x.' <<<< (TotalItemSize(MB)' = $s.TotalItemSize.Value.ToMB() Property 'ItemCount' cannot be found on this object; make sure it exists
and settable. At C:\MB.ps1:11 char:4 + $x.I <<<< temCount = $s.Itemcount Property 'DatabaseName' cannot be found on this object; make sure it exists a is settable.
There were a couple of mistakes in the original (corrected below), but
nothing that would cause all those errors.
I expect you copied the script from a web page? If you did, I'll bet
the double and single quotes were turned into "smart quotes". If that
happened, just replace them with "straight quotes".
Also, the line that begins with 'x=""|select' was line-wrapped. I
added a back-tick after the 'x=""|select' so the shell sees the next
line as a continuation of that line and not as an independant line.
$o = @()
Get-mailbox -resultsize unlimited | foreach {
$s = Get-MailboxStatistics -identity $_.distinguishedname
$x=""|select `
DisplayName,PrimarySMTPAddress,Office,'TotalItemSize(KB)','TotalItemSize(MB)',ItemCount,DatabaseName,LastLogonTime
$x.DisplayName = $_.Displayname
$x.PrimarySMTPAddress = $_.PrimarySMTPAddress
$x.Office = $_.Office
$x.'TotalItemSize(KB)' = $s.TotalItemSize.Value.ToKB()
$x.'TotalItemSize(MB)' = $s.TotalItemSize.Value.ToMB()
$x.ItemCount = $s.Itemcount
$x.DatabaseName = $_.Database
$x.LastLogonTime = $s.LastLogonTime
$o += $x
}
$o | export-csv c:\mb5.csv -NoTypeInformation
---
Rich Matheisen
MCSE+I, Exchange MVP
--- Rich Matheisen MCSE+I, Exchange MVP
Free Windows Admin Tool Kit Click here and download it now
June 7th, 2012 11:17pm
I am having some issues with the script. I think I am very close but not sure what is missing here. Should I copy this code and put into Notepad? That should strip out all of the HTML right?
[PS] C:\>./mb.ps1
The string starting:
At C:\MB.ps1:10 char:22
+ $x.'TotalItemSize(MB) <<<< ' = $s.TotalItemSize.Vaule.ToMB()
is missing the terminator: '.
At C:\MB.ps1:16 char:47
+ $o | export-csv c:\mb10.csv -NoTypeInformation <<<<
+ CategoryInfo : ParserError: ( = $s.TotalItem...TypeInformation:
String) [], ParseException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
[PS] C:\>./mb.ps1
The string starting:
At C:\MB.ps1:9 char:22
+ $x.'TotalItemSize(KB) <<<< ' = $s.TotalItemSize.Value.ToKB()
is missing the terminator: '.
At C:\MB.ps1:15 char:47
+ $o | export-csv c:\mb10.csv -NoTypeInformation <<<<
+ CategoryInfo : ParserError: ( = $s.TotalItem...TypeInformation:
String) [], ParseException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfStringJason
June 8th, 2012 10:13am
I found this PS1 file Mike Crowley's blog. This script is great but all I need is to bring in LastLogonTime and the Office field. When I manually edit this script the LastLogonTime and Office fields are still blank.
http://blog.jasonsherry.net/2012/04/13/get-mailboxinfo/
# Based on script from: http://www.powergui.org/thread.jspa?threadID=7514
# Modified to work with Exchange 2010 by Jason Sherry http://info.izzy.org
# Created 11/10/2010, Last Updated 4/24/2012
# Gets the size of mailboxes and certain attributes from the AD that can be used to plan for mailbox moves
# For more info see: http://info.izzy.org/Wiki/GetMailBoxInfo.aspx
# For Exchange 2003 support see: http://info.izzy.org/Technical/Scripting/Documents/Forms/DispForm.aspx?ID=60
$MB = Get-Mailbox -resultSize unlimited
$MB | foreach{
$AccountEnabled = "Enabled"
$user = Get-User $_ | select DisplayName, FirstName, LastName, company, department, title, samAccountName, UserAccountControl, City, StateOrProvince, CountryOrRegion
$mbx = $_ | select ServerName, samAccountName, Name, Alias, PrimarySmtpAddress, DistinguishedName
write-host "Processing: " $user.DisplayName "("$user.samAccountName")"
$ADSPath = "LDAP://" + $mbx.DistinguishedName
$ADUser = [ADSI]$ADSPath
$Description = [String]$ADUser.Description # Required to convert the returned value to a string
$mbx | add-member -type noteProperty -name DisplayName -value $User.DisplayName # Not part of the mailbox properties, so using Get-User property and adding it to $mbx variable
$mbx | add-member -type noteProperty -name FirstName -value $User.FirstName
$mbx | add-member -type noteProperty -name LastName -value $User.LastName
$mbx | add-member -type noteProperty -name Company -value $User.company
$mbx | add-member -type noteProperty -name Department -value $User.department
$mbx | add-member -type noteProperty -name Title -value $User.title
$mbx | add-member -type noteProperty -name City -value $User.City
$mbx | add-member -type noteProperty -name StateOrProvince -value $User.StateOrProvince
$mbx | add-member -type noteProperty -name CountryOrRegion -value $User.CountryOrRegion
$mbx | add-member -type noteProperty -name Description -value $Description # Not avaliable from Exchange cmdlets, so using ADSI
If ($User.UserAccountControl -contains "AccountDisabled"){
$AccountEnabled = "Disabled"
}
$mbx | add-member -type noteProperty -name UserAccountControl -value $AccountEnabled
Get-MailboxStatistics $_ | ForEach{
$MBSize = $_.TotalItemSize.Value.ToKB()
$MBItemCount = $_.ItemCount
$MBDB = $_.DatabaseName
}
$mbx | add-member -type noteProperty -name TotalItemSizeinKB -value $MBSize # Get attributes from Get-MailboxStatistics and add them to $mbx variable
$mbx | add-member -type noteProperty -name ItemCount -value $MBItemCount
$mbx | add-member -type noteProperty -name DatabaseName -value $MBDB
write-host "DisplayName: "$mbx.DisplayName "`tMailbox Size: "$mbx.TotalItemSizeinKB "`tMailbox Size: "$mbx.ItemCount
write-host
# Write-host $mbx.ServerName,"N/A", $mbx.DatabaseName, $mbx.Name, $mbx.FirstName, $mbx.LastName, $mbx.DisplayName, $mbx.Alias, $mbx.PrimarySmtpAddress, $mbx.samAccountName, $mbx.UserAccountControl, $mbx.TotalItemSizeinKB, $mbx.Description, $mbx.Department, $mbx.Title, $mbx.City, $mbx.StateOrProvince, $mbx.CountryOrRegion, $mbx.DistinguishedName
$mbx | Select ServerName,"N/A", DatabaseName, Name, FirstName, LastName, DisplayName, Alias, PrimarySmtpAddress, samAccountName, UserAccountControl, TotalItemSizeinKB, ItemCount, Company, Description, Department, Title, City, StateOrProvince, CountryOrRegion, DistinguishedName
} | export-csv -NoTypeInformation .\MailboxData.csv -Encoding unicode
$MB = $Null
$user = $Null
$ADUser = $Null
$MBX = $NullJason
Free Windows Admin Tool Kit Click here and download it now
June 8th, 2012 10:49am
On Fri, 8 Jun 2012 14:13:12 +0000, Jason B. Williams wrote:
>I am having some issues with the script. I think I am very close but not sure what is missing here. Should I copy this code and put into Notepad? That should strip out all of the HTML right?
There's no HTML in the script. It's all just plain text.
Given the fact that the word "Vaule" is misspelled in what you posted
I'm guessing you're rekeying it. In that case, yes, use Notepad (or
any editor that works with plain text). Be careful of matching opening
an closing single quotes. I think you missed one. Posting the whole
script instead of just the errors would help, too.
>
>
>
>[PS] C:\>./mb.ps1 The string starting: At C:\MB.ps1:10 char:22 + $x.'TotalItemSize(MB) <<<< ' = $s.TotalItemSize.Vaule.ToMB() is missing the terminator: '. At C:\MB.ps1:16 char:47 + $o | export-csv c:\mb10.csv -NoTypeInformation <<<< + CategoryInfo :
ParserError: ( = $s.TotalItem...TypeInformation: String) [], ParseException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
>
>[PS] C:\>./mb.ps1 The string starting: At C:\MB.ps1:9 char:22 + $x.'TotalItemSize(KB) <<<< ' = $s.TotalItemSize.Value.ToKB() is missing the terminator: '. At C:\MB.ps1:15 char:47 + $o | export-csv c:\mb10.csv -NoTypeInformation <<<< + CategoryInfo : ParserError:
( = $s.TotalItem...TypeInformation: String) [], ParseException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
>
>
>Jason
---
Rich Matheisen
MCSE+I, Exchange MVP
--- Rich Matheisen MCSE+I, Exchange MVP
June 8th, 2012 10:06pm
Hi Jason,
Any updates?
Did you correct the wrong word "vaule" in your copied script as Rich said? After that, it should work.
$mbx | add-member -type noteProperty -name LastLogonTime -vaule $MBLastLogonTime
I also tested Rich's script, and it works as well.Frank Wang
TechNet Community Support
Free Windows Admin Tool Kit Click here and download it now
June 10th, 2012 11:49pm