Ps1 - bat
Hello, I am using the below shell command and need to schedule it for the 1st of each month: Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft DisplayName, lastlogontime, @{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount > c:\SCRIPTS\mailboxes.txt At the moment I use the above shell command that I have scheduled with the windows Scheduled Tasks + using the below script (blat.exe) that I have scheduled after my ps1 has been generated. @echo OFF blat.exe C:\Scripts\mailboxes.txt -server "twwp01.company.intra" -to graig@ company .com -s "[COMPANY] Mailbox Statistics - %date% " -mailfrom admin@ company .com echo goto :eof My main problem is that I would like to generate a nice HTML file instead of the txt file. Because it is not looking good --> DisplayName LastLogonTime TotalItemSize(KB) ItemCount ----------- ------------- ----------------- --------- USER1 21/12/2009 22:19:28 6585261 16956 USER2 22/12/2009 11:06:28 5056816 11233 USER3 31/07/2009 05:16:11 4613964 11513 USER4 22/12/2009 09:24:46 3613409 15767 Also if you have any idea how I could gather the ps1 and the *.bat would be great :-D
December 22nd, 2009 2:55pm

Hi, Open a txt copy the command and save it .bat (don't forget to replace mailbox.ps1 by ur script) Powershell -command "& {C:\SCRIPTS\mailboxes.ps1}"
Free Windows Admin Tool Kit Click here and download it now
December 22nd, 2009 3:08pm

Here you go... To Convert the output to HTML... and to send it through mail... #Start function SendMail($Recipient) { $SmtpClient = new-object system.net.mail.smtpClient $MailMessage = New-Object system.net.mail.mailmessage $SmtpClient.Host = "twwp01.company.intra " $mailmessage.from = "admin@ company .com " $mailmessage.To.add("$Recipient ") $mailmessage.Subject = "[COMPANY] Mailbox Statistics - $date " $mailmessage.IsBodyHTML = $true $mailmessage.Body = $body $smtpclient.Send($mailmessage) } $date = $(get-date).GetDateTimeFormats()[6] $Statistics = Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ConvertTo-HTML -property DisplayName, lastlogontime, @{l="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount -title "Mailbox Statistics" $Recipient = "graig@ company .com " $body = $Statistics SendMail($Recipient) #End To Schedule it for every month user Task Scheduler. Thanks. Vishal Ramnani MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
December 22nd, 2009 3:29pm

To schedule for every month do as follow... Save above script PS1 file. Open Notepad and Type below command. C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". ' <path of the PS1 file>' " Note the command which is preceded with ". Save this as .BAT file which is ready to be included in Scheduled Task. Thanks. Vishal Ramnani MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
Free Windows Admin Tool Kit Click here and download it now
December 22nd, 2009 3:38pm

If you want some colors... in ur html file $a = "<style>" $a = $a + "BODY{background-color:#FFFFFF;}" $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}" $a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#E3CEF6}" $a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#E3CEF6}" $a = $a + "</style>" Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ConvertTo-HTML -head $a -property DisplayName, lastlogontime, @{l="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount | Out-File c:\report.htm
December 22nd, 2009 3:48pm

Agreed with Lana! this will furnish your mail with tables and colors. Add that part ($a) from Lana's post as a block and add a parameter in ... ConvertTo-Html -head $a ... rest will remain same. Thanks. Vishal Ramnani MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
Free Windows Admin Tool Kit Click here and download it now
December 22nd, 2009 4:05pm

Hello Vishal,A big thanks to Vishal and Lana !!!!For some reason I got the below error:The term 'graig@company.com' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.At C:\Scripts\Extract.ps1:19 char:29+ $Recipient = graig@company.com <<<<Exception calling "Add" with "1" argument(s): "The parameter 'addresses' cannot be an empty string.Parameter name: addresses"At C:\Scripts\Extract.ps1:8 char:22+ $mailmessage.To.add( <<<< "$Recipient")Exception calling "Send" with "1" argument(s): "A recipient must be specified."At C:\Scripts\Extract.ps1:12 char:19+ $smtpclient.Send( <<<< $mailmessage)Any idea??
December 22nd, 2009 4:54pm

It's expecting a string, so you need to quote it -$Recipient = graig@company.com
Free Windows Admin Tool Kit Click here and download it now
December 22nd, 2009 5:03pm

Now it woks --> graig@company.com should read "graig@"company.comSo I added "" and it's working.$Statistics = Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ConvertTo-HTML -property DisplayName, lastlogontime, @{l="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount -title "Mailbox Statistics"$Recipient = graig@ company .com $body = $StatisticsSendMail($Recipient)
December 22nd, 2009 5:04pm

Try this... $a = "<style>" $a = $a + "BODY{background-color:#FFFFFF;}" $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}" $a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#E3CEF6}" $a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#E3CEF6}" $a = $a + "</style>" Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ConvertTo-HTML -head $a -property DisplayName, lastlogontime, @{l="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount | Out-File c:\report.html Function sendmail($body) { $SmtpClient = new-object system.net.mail.smtpClient $MailMessage = New-Object system.net.mail.mailmessage $SmtpClient.Host = "twwp01.company.intra " $mailmessage.from = "admin@ company .com " $mailmessage.To.add("graig@ company .com ") $mailmessage.Subject = “Mailboxes statistics” $MailMessage.IsBodyHtml = $false $mailmessage.Attachments.Add("c:\report.html") $smtpclient.Send($mailmessage) }
Free Windows Admin Tool Kit Click here and download it now
December 22nd, 2009 5:04pm

Updated the script in the original post. Thanks.Vishal RamnaniMCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
December 22nd, 2009 5:07pm

Sorry for being a pain in the neck.. but when I try to combine your script Vishal and the Lana's HTML code like this -->#Startfunction SendMail($Recipient){ $SmtpClient = new-object system.net.mail.smtpClient $MailMessage = New-Object system.net.mail.mailmessage $SmtpClient.Host = "xxxxx.company.intra" $mailmessage.from = "admin@company.com" $mailmessage.To.add("$Recipient") $mailmessage.Subject = "[COMPANY] Mailbox Statistics - $date" $mailmessage.IsBodyHTML = $true $mailmessage.Body = $body $smtpclient.Send($mailmessage)} $date = $(get-date).GetDateTimeFormats()[6] $a = "<style>"$a = $a + "BODY{background-color:#FFFFFF;}"$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#E3CEF6}"$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#E3CEF6}"$a = $a + "</style>" Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ConvertTo-HTML -head $a -property DisplayName, lastlogontime, @{l="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount | Out-File c:\scripts\mailboxes.htm $Recipient = "graig@company.com"$body = $StatisticsSendMail($Recipient) #EndI get the email but don't get the attachment. Could you please help me out with that??Thanks
Free Windows Admin Tool Kit Click here and download it now
December 22nd, 2009 5:22pm

if you want to run above code then there should be some modification done. e.g. adding that file as attachment with the mail. In above script there is NO data in $Statistics variable, you are exporting the output to a file instead and assigning this empty variable to $body so it will off-course not work. Try the script i posted originally. that should work fine now. (i corrected the typos). Thanks. Vishal Ramnani MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
December 22nd, 2009 5:30pm

You need to add $Statistics before Get-Mailbox if you want that works. $Statistics = Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ConvertTo-HTML -head $a -property DisplayName, lastlogontime, @{l="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount | Out-File c:\scripts\mailboxes.htm
Free Windows Admin Tool Kit Click here and download it now
December 22nd, 2009 5:31pm

function SendMail($Recipient){ $SmtpClient = new-object system.net.mail.smtpClient $MailMessage = New-Object system.net.mail.mailmessage $SmtpClient.Host = "xxxxx.company.intra" $mailmessage.from = "admin@company.com" $mailmessage.To.add("$Recipient") $mailmessage.Subject = "[COMPANY] Mailbox Statistics - $date" $mailmessage.IsBodyHTML = $true $mailmessage.Body = $body#add attachment $mailattachment = new-object System.Net.Mail.Attachment($attachment) $mailmessage.attachments.add($mailattachment) $smtpclient.Send($mailmessage)} $date = $(get-date).GetDateTimeFormats()[6] $a = "<style>"$a = $a + "BODY{background-color:#FFFFFF;}"$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#E3CEF6}"$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#E3CEF6}"$a = $a + "</style>" Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ConvertTo-HTML -head $a -property DisplayName, lastlogontime, @{l="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount | Out-File c:\scripts\mailboxes.htm $Recipient = "graig@company.com"$body = $Statistics$attachment = "c:\scripts\mailboxes.htm"SendMail($Recipient) #End
December 22nd, 2009 5:34pm

Thanks a lot again to you 2!!Vishal --> your first scrip is great. However, I have no borders and texts are not always align left.Lana_75 --> Thank you!! design is really good would it be possible to wide the column for the lastlogontime and TotalItemSize (KB)??mjolinor --> Script does work I just wonder whether I could also view the html attachment directly in the body of the email??
Free Windows Admin Tool Kit Click here and download it now
December 22nd, 2009 6:35pm

As far as getting the output into the body of the email, the easies way would be to change this:$body = $Statistics to $body = gc "c:\scripts\mailboxes.htm"
December 22nd, 2009 6:39pm

Thanks Mjolinor, It does work very good. But.. if I run the ps1 twice I got the below error message. [PS] C:\Documents and Settings\graig\Bureau>C:\Scripts\TotalItemSize.ps1 -->OK [PS] C:\Documents and Settings\graig\Bureau>C:\Scripts\TotalItemSize.ps1 --> NOK Out-File : The process cannot access the file 'C:\scripts\mailboxes.htm' because it is being used by another process. At C:\Scripts\TotalItemSize.ps1:27 char:200 + Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ConvertTo-HTML -head $a -property DisplayName, @{l="T otalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount | Out-File <<<< c:\scripts\mailboxes.htm After I run your script is there any chance to remove the mailboxes.htm? because I would need to regenerate the mailboxes.htm everytime I run the ps1 script. When I try manualy to remove it an error message says that the ressource is already used by another program..
Free Windows Admin Tool Kit Click here and download it now
December 23rd, 2009 11:09am

Hi Graig, What do you mean by "to wide the column for... "?
December 23rd, 2009 11:40am

Thanks Mjolinor, It does work very good. But.. if I run the ps1 twice I got the below error message. [PS] C:\Documents and Settings\graig\Bureau>C:\Scripts\TotalItemSize.ps1 -->OK [PS] C:\Documents and Settings\graig\Bureau>C:\Scripts\TotalItemSize.ps1 --> NOK Out-File : The process cannot access the file 'C:\scripts\mailboxes.htm' because it is being used by another process. At C:\Scripts\TotalItemSize.ps1:27 char:200 + Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ConvertTo-HTML -head $a -property DisplayName, @{l="T otalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount | Out-File <<<< c:\scripts\mailboxes.htm After I run your script is there any chance to remove the mailboxes.htm? because I would need to regenerate the mailboxes.htm everytime I run the ps1 script. When I try manualy to remove it an error message says that the ressource is already used by another program.. Try adding a line as below at the end of the script. $attachment = $null if (Test-Path c:\scripts\mailboxes.htm) {Remove-Item c:\scripts\mailboxes.htm} This will remove the out file after sending it as attachment. Never tested it though but i think it should work. Vishal Ramnani MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
Free Windows Admin Tool Kit Click here and download it now
December 23rd, 2009 1:12pm

I meant column width (sorry)
December 23rd, 2009 1:50pm

In fact it does work once I logg off. *So the below script from mjolinor works fine *Thanks to Lana_75 for the html file design *And V I S H A L for the below scheduler: To schedule for every month do as follow... Save above script PS1 file. Open Notepad and Type below command. C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". ' <path of the PS1 file>' " Note the command which is preceded with ". Save this as .BAT file which is ready to be included in Scheduled Task. SCRIPT: function SendMail($Recipient) { $SmtpClient = new-object system.net.mail.smtpClient $MailMessage = New-Object system.net.mail.mailmessage $SmtpClient.Host = "xxxxx.company.intra" $mailmessage.from = "admin@company.com " $mailmessage.To.add("$Recipient") $mailmessage.Subject = "[COMPANY] Mailbox Statistics - $date" $mailmessage.IsBodyHTML = $true $mailmessage.Body = $body #add attachment $mailattachment = new-object System.Net.Mail.Attachment($attachment) $mailmessage.attachments.add($mailattachment) $smtpclient.Send($mailmessage) } $date = $(get-date).GetDateTimeFormats()[6] $a = "<style>" $a = $a + "BODY{background-color:#FFFFFF;}" $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}" $a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#E3CEF6}" $a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#E3CEF6}" $a = $a + "</style>" Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ConvertTo-HTML -head $a -property DisplayName, lastlogontime, @{l="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount | Out-File c:\scripts\mailboxes.htm $Recipient = "graig@company.com " $body = $Statistics $attachment = "c:\scripts\mailboxes.htm" SendMail($Recipient) #End
Free Windows Admin Tool Kit Click here and download it now
December 23rd, 2009 5:45pm

The other posters actually answered the bulk of your questions. I came in on the tail end of the disscussion about the attachments. It would probably be appreciated if you would mark the appropriate posts that answered you other questions as being answers, or at least helpful.
December 23rd, 2009 6:20pm

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

Other recent topics Other recent topics