Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

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"
0
stlhost
Asked:
stlhost
  • 6
  • 4
  • 2
1 Solution
 
Seth SimmonsSr. Systems AdministratorCommented:
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
 
stlhostAuthor Commented:
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
 
Seth SimmonsSr. Systems AdministratorCommented:
what version of windows and exchange?
0
Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

 
QlemoC++ DeveloperCommented:
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
 
stlhostAuthor Commented:
Windows 2008 R2


AdminDisplayVersion                     ExchangeVersion
-------------------                     ---------------
Version 14.1 (Build 218.15)             0.1 (8.0.535.0)
0
 
stlhostAuthor Commented:
Qlemo

Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.
0
 
QlemoC++ DeveloperCommented:
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
 
stlhostAuthor Commented:
Unexpected token '}' in expression or statement.
At C:\mailboxstats.ps1:7 char:2
+ } <<<<  | set-variable Result
    + CategoryInfo          : ParserError: (}:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken
0
 
QlemoC++ DeveloperCommented:
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
 
stlhostAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
stlhostAuthor Commented:
I figured as much. Thanks so much
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now