Solved

GetMailboxStatistics task scheduler

Posted on 2014-10-21
12
225 Views
Last Modified: 2014-10-21
I would like to get this added to a task schedule but my command is wrong I guess as it just says it is running but doesn't actually do anything. I would like to get this report emailed to me rather than dumped to the hard drive if it is possible?

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command ". 'E:\Exchange Binaries\Bin\RemoteExchange.ps1'; Connect-ExchangeServer –auto; "Get-Mailbox | Select-Object Displayname,Database,@{Name='TotalItemSize'; Expression={[String]::join(";",((Get-MailboxStatistics -identity $_.identity).TotalItemSize))}},@{Name='ItemCount'; Expression={[String]::join(";",((Get-MailboxStatistics -identity $_.identity).ItemCount))}},IssueWarningQuota, ProhibitSendQuota | export-csv -path "c:\mailboxsizes.csv"
0
Comment
Question by:stlhost
[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
  • 6
  • 4
  • 2
12 Comments
 
LVL 34

Expert Comment

by:Seth Simmons
ID: 40394997
1) are the exchange extensions being loaded?
2) is the task running with highest privileges?
3) does it work when you run it manually?
4) you can pipe the output to Send-MailMessage (if you have powershell 4 installed)

Send-MailMessage
http://technet.microsoft.com/en-us/library/hh849925.aspx
0
 
LVL 2

Author Comment

by:stlhost
ID: 40395016
1) yes
2) No, forgot about that.
3) Well now that you ask, no. All I get is ,,,,,, If I run the command without C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command ". 'E:\Exchange Binaries\Bin\RemoteExchange.ps1'; Connect-ExchangeServer –auto; it does work.
4) [PS] C:\Windows\system32>$PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1
0
 
LVL 34

Expert Comment

by:Seth Simmons
ID: 40395035
what version of windows and exchange?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 69

Expert Comment

by:Qlemo
ID: 40395037
That commands are too many to keep them properly entered on a single line. And because of that, there are unmatched and errornous quotes, and other stuff.

First off all, you provide -noexit - of course PS will not exit then after executing the command, keeping the scheduled task running forever without doing anything. That needs to get removed. And for the remainder I strongly encourage to use a PS1 file consisting of:
. 'E:\Exchange Binaries\Bin\RemoteExchange.ps1'
Connect-ExchangeServer –auto
Get-Mailbox | % {
  $stats = Get-MailboxStatistics -identity $_.identity
  $_ | Select-Object Displayname, Database,
    @{Name='TotalItemSize'; Expression={[String]::join(";", $stats.TotalItemSize)}},
    @{Name='ItemCount'; Expression={[String]::join(";",$stats.ItemCount)}},
    IssueWarningQuota, ProhibitSendQuota
} | set-variable Result
Send-MailMessage -SmtpServer mx.There.com -From Me@Here.com -To You@There.com -Subject 'Mail Stats Report' -BodyAsHTML -Body (($Result | convertTo-HTML) -join "`n")

Open in new window

BTW, it is not true you can just pipe content to Send-MailMessage - pipe input is treated as attachment file names, not body text. That didn't change (officially) for PS4.
0
 
LVL 2

Author Comment

by:stlhost
ID: 40395046
Windows 2008 R2


AdminDisplayVersion                     ExchangeVersion
-------------------                     ---------------
Version 14.1 (Build 218.15)             0.1 (8.0.535.0)
0
 
LVL 2

Author Comment

by:stlhost
ID: 40395076
Qlemo

Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40395096
Probably a restriction with the Remote Exchange shell. Let's remove the optimizations, and just run your corrected script:
. 'E:\Exchange Binaries\Bin\RemoteExchange.ps1'
Connect-ExchangeServer –auto
Get-Mailbox | Select-Object Displayname, Database,
    @{Name='TotalItemSize'; Expression={[String]::join(";", (Get-MailboxStatistics -identity $_.identity).TotalItemSize)}},
    @{Name='ItemCount'; Expression={[String]::join(";",(Get-MailboxStatistics -identity $_.identity).ItemCount)}},
    IssueWarningQuota, ProhibitSendQuota
} | set-variable Result
Send-MailMessage -SmtpServer mx.There.com -From Me@Here.com -To You@There.com -Subject 'Mail Stats Report' -BodyAsHTML -Body (($Result | convertTo-HTML) -join "`n")

Open in new window

0
 
LVL 2

Author Comment

by:stlhost
ID: 40395264
Unexpected token '}' in expression or statement.
At C:\mailboxstats.ps1:7 char:2
+ } <<<<  | set-variable Result
    + CategoryInfo          : ParserError: (}:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40395303
Sorry for that, left something over:
. 'E:\Exchange Binaries\Bin\RemoteExchange.ps1'
Connect-ExchangeServer –auto
Get-Mailbox | Select-Object Displayname, Database,
    @{Name='TotalItemSize'; Expression={[String]::join(";", (Get-MailboxStatistics -identity $_.identity).TotalItemSize)}},
    @{Name='ItemCount'; Expression={[String]::join(";",(Get-MailboxStatistics -identity $_.identity).ItemCount)}},
    IssueWarningQuota, ProhibitSendQuota |
  set-variable Result
Send-MailMessage -SmtpServer mx.There.com -From Me@Here.com -To You@There.com -Subject 'Mail Stats Report' -BodyAsHTML -Body (($Result | convertTo-HTML) -join "`n")

Open in new window

0
 
LVL 2

Author Comment

by:stlhost
ID: 40395334
Update-TypeData : The following error occurred while loading the extended type data file:
Microsoft.PowerShell, E:\Exchange Binaries\bin\exchange.types.ps1xml : File skipped because it was already present from
 "Microsoft.PowerShell".
At E:\Exchange Binaries\Bin\RemoteExchange.ps1:94 char:17
+     Update-TypeData <<<<  -PrependPath $typeFilePath
    + CategoryInfo          : InvalidOperation: (:) [Update-TypeData], RuntimeException
    + FullyQualifiedErrorId : TypesXmlUpdateException,Microsoft.PowerShell.Commands.UpdateTypeDataCommand

Update-TypeData : The following error occurred while loading the extended type data file:
Microsoft.PowerShell, E:\Exchange Binaries\bin\Exchange.partial.Types.ps1xml : File skipped because it was already pres
ent from "Microsoft.PowerShell".
Microsoft.PowerShell, E:\Exchange Binaries\bin\exchange.types.ps1xml : File skipped because it was already present from
 "Microsoft.PowerShell".
At E:\Exchange Binaries\Bin\RemoteExchange.ps1:104 char:16
+ Update-TypeData <<<<  -PrependPath $partialTypeFile
    + CategoryInfo          : InvalidOperation: (:) [Update-TypeData], RuntimeException
    + FullyQualifiedErrorId : TypesXmlUpdateException,Microsoft.PowerShell.Commands.UpdateTypeDataCommand


         Welcome to the Exchange Management Shell!

Full list of cmdlets: Get-Command
Only Exchange cmdlets: Get-ExCommand
Cmdlets that match a specific string: Help *<string>*
Get general help: Help
Get help for a cmdlet: Help <cmdlet name> or <cmdlet name> -?
Show quick reference guide: QuickRef
Exchange team blog: Get-ExBlog
Show full output for a command: <command> | Format-List

Tip of the day #67:

Want to customize your Exchange Management Shell profile? Run the following command to determine the location of your Mi
crosoft.PowerShell_profile.ps1 file:

 $Profile

You may have to create the PSConfiguration folder and Microsoft.PowerShell_profile.ps1 file. After you've done that, you
 can add your favorite functions and aliases, which will be loaded every time that the Exchange Management Shell is open
ed.

VERBOSE: Connecting to CTEXCH02.xxx.local
VERBOSE: Connected to CTEXCH02.xxx.local.
[PS] C:\>

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40395357
That is because you run the script or code more than once in the same PS session. It is trying to do the necessary imports and connect to Exchange again, but the imports may only happen once. You should be able to just ignore that, the script should continue. In your scheduled task this will not happen, as PS is started newly each time.
0
 
LVL 2

Author Comment

by:stlhost
ID: 40395391
I figured as much. Thanks so much
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In-place Upgrading Dirsync to Azure AD Connect
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

756 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