Link to home
Start Free TrialLog in
Avatar of R W
R WFlag for United States of America

asked on

GetMailboxStatistics task scheduler

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"
Avatar of Seth Simmons
Seth Simmons
Flag of United States of America image

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
Avatar of R W

ASKER

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
what version of windows and exchange?
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.
Avatar of R W

ASKER

Windows 2008 R2


AdminDisplayVersion                     ExchangeVersion
-------------------                     ---------------
Version 14.1 (Build 218.15)             0.1 (8.0.535.0)
Avatar of R W

ASKER

Qlemo

Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.
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

Avatar of R W

ASKER

Unexpected token '}' in expression or statement.
At C:\mailboxstats.ps1:7 char:2
+ } <<<<  | set-variable Result
    + CategoryInfo          : ParserError: (}:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of R W

ASKER

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

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.
Avatar of R W

ASKER

I figured as much. Thanks so much