Link to home
Start Free TrialLog in
Avatar of krish5music
krish5musicFlag for United States of America

asked on

Automate the mailbox command in the specific format.

Hello Experts,

I have the below script in place, which will provide the information about mailboxes which were created for last 24 hours. I would like to automate this , instead of running the
below command everyday. I would like to get the output in the attached format. I appreciate any help .

Get-Mailbox -Server "server name" | Where {$_.WhenCreated -gt (Get-Date).AddDays(-1)} | Select-Object Name, PrimarySmtpAddress

Thanks
Krish
format.JPG
Avatar of oBdA
oBdA

Save this as MailboxReport.ps1 or Whatever.ps1
It will create the report as html table and send it as email.
For automatic execution, schedule it to run with a user with the required permissions to query the mailboxes.
Program to run:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Arguments:
-ExecutionPolicy Bypass -File "C:\Path\to\MailboxReport.ps1"
$htmlHead = @'
<style>
	table	{border-width:1px; border-style:solid; border-color:black;}
	th		{border-width:1px; border-style:solid; border-color:black; padding:1px;}
	td		{border-width:1px; border-style:solid; border-color:black; padding:1px;}
</style>
'@

$results = Get-Mailbox -Server "server name" |
	Where-Object {$_.WhenCreated -gt (Get-Date).AddDays(-1)} |
	Select-Object Name, PrimarySmtpAddress

If ($results) {
	$html = (ConvertTo-Html -Head $htmlHead) -join '<br />'
} Else {
	$html = "No new mailboxes found."
}

$mailArgs = @{
	To = 'someone@domain.com'
	From = 'someone.else@domain.com'
	Subject = 'New mailboxes'
	Body = $html
	SmtpServer = 'smtp.acme.com'
	BodyAsHtml = $true
}
Send-MailMessage @mailArgs

Open in new window

Avatar of krish5music

ASKER

Hi OBda,

Thanks a lot for your inputs.

I will test the same and let you know.

Regards,
Krish
Hello OdBA,

Im sorry for the delayed response. I have attached the screenshot of the output,  i got when testing the above script. It works fine, however, they are two changes which i want to include in the script. Please let me know your suggestion for the same.
1. Can we able to add the Table in the output? Currently it displays the content without table format.
2.  Can you suggest , if incase i want to remove certain headings(Example, Index Category, Event ID), what kind of changes i have to do in the code?

Thanks
Krish.
auto-alert.jpg
Sorry, the variable from which the html should have been created went AWOL ...
And you can select the properties to show in the table with the "Select-Object ..." in line 11, currently it's "Name" and "PrimarySmtpAddress"
$htmlHead = @'
<style>
	table	{border-width:1px; border-style:solid; border-color:black;}
	th		{border-width:1px; border-style:solid; border-color:black; padding:1px;}
	td		{border-width:1px; border-style:solid; border-color:black; padding:1px;}
</style>
'@

$results = Get-Mailbox -Server "server name" |
	Where-Object {$_.WhenCreated -gt (Get-Date).AddDays(-1)} |
	Select-Object Name, PrimarySmtpAddress

If ($results) {
	$html = ($results | ConvertTo-Html -Head $htmlHead) -join '<br />'
} Else {
	$html = "No new mailboxes found."
}

$mailArgs = @{
	To = 'someone@domain.com'
	From = 'someone.else@domain.com'
	Subject = 'New mailboxes'
	Body = $html
	SmtpServer = 'smtp.acme.com'
	BodyAsHtml = $true
}
Send-MailMessage @mailArgs

Open in new window

Hi OdBA,

I have tested the above script. Now the result keeps telling that "No new Mailbox" eventhough there is new mailbox created.

I have used the same arguments as mentioned above.

Kindly advise.

Thanks
Krish.
"New" is currently restricted to the past 24 hours - are you sure that it should have returned results?
What happens when you run just this? And/or if you go back further in AddDays()?
$results = Get-Mailbox -Server "server name" |
	Where-Object {$_.WhenCreated -gt (Get-Date).AddDays(-1)} |
	Select-Object Name, PrimarySmtpAddress

Open in new window

When i ran the above command it provides the exact result. However when i execute the script, it keep says "No New Mailbox".

Thanks
Krish
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

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
Hi OdBA,

I got the below error msg when i ran the above script. The account that is used for this script has full permission.

Error during Get-Mailbox: The term 'Get-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Thanks
Krish
Well, then the Exchange module isn't available; you need to add the commands to import the module/session, depending whether you're using on-premise or online.
It worked. I changed the execution command as below. One last question, Is it possible to align the text as a center in the table? Please find the attached screenshot.

-version 2.0 -NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; C:\temp\mbxnotify.ps1"

Thanks
Krish.
table.JPG
Sorry, can't reproduce that rendering; which mail client are you using to display the message?
You can add that at the end of the script:
$html | Set-Content C:\Temp\mbxnotify.html

Open in new window

You'll then have the original html created, and can open it in the browser of your choice.
Thank you so much for your help oBdA. I Appreciate your patience and spending your valuable time for providing the solution.