Quantcast
Channel: PowerShell
Viewing all articles
Browse latest Browse all 15370

enumerate and email with powershell

$
0
0

ok fellas, I took you guys word and I've been reading, researching, learning and testing about powershell. Within the last 20 days, here is what I've been able to come up with.

Powershell
1
get-mailbox-identity$_.name|selectname,userprincipalname,@{label="Size";expression={$size=Get-Mailboxstatistics$_.identity;$size.TotalItemSize.Value.ToMB()}},@{label="Items";expression={$items=Get-Mailboxstatistics$_.name;$item.ItemCount}}

I stored this in a script called accountsizes.ps1. It works exactly as I expected by outputting all the email accounts with the sizes, but in order for me to get only the mailboxes over 2048MB, I have to call it like this:

Text
1
PS C:\accountsizes.ps1 | where size -gt "2048" | select userprincipalname,size

And this works by return the email addresses and mailbox sizes in MBs. But one of my dilemmas is, if I put the ForEach into the script, there is no output to the screen; here's how it looks:


Powershell
1
2
3
4
5
get-mailbox-identity$_.name|selectname,userprincipalname,@{label="Size";expression={$size=Get-Mailboxstatistics$_.identity;$size.TotalItemSize.Value.ToMB()}},@{label="Items";expression={$items=Get-Mailboxstatistics$_.name;$item.ItemCount}}|foreach-object{wheresize-gt2048|selectuserprincipalname,size|write-host$_.userprincipalname}

 how do I enumerate through the results and extract each email address and send an email to that user and CC myself. From what I been reading and learning, I would have to use a ForEach loop and the send-mailmessage cmdlet. I cannot figure out how to use the ForEach and incorporate it with the script: Here is I go brain dead with the ForEach. I do not know the right way to go about doing this (so, don't ask me why I'm doing this way :)), I have no previous knowledge about scripting and coding.

Here is the complete script with my email part which also outputs nothing:

Powershell
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
get-mailbox-identity$_.name|selectname,userprincipalname,@{label="Size";expression={$size=Get-Mailboxstatistics$_.identity;$size.TotalItemSize.Value.ToMB()}},@{label="Items";expression={$items=Get-Mailboxstatistics$_.name;$item.ItemCount}}|selectuserprincipalname,size|foreach-object{if($size-gt2048){$smtpserver="domain.com"$smtpFrom="administrator@domain.com"$smtpTo=$_.userprincipalname$messageSubject="Warning Email Mailbox Too Large"$body="blah blah blah blah"send-mailmessage-from$smtpFrom-to$smtpTo-subject$messageSubject-body$body}}

Thanks in advance.


Viewing all articles
Browse latest Browse all 15370

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>