Need a little help with a powershell script

Discussion in 'Scripting & Programming' started by nugget, Jan 14, 2009.

  1. nugget
    Honorary Member

    nugget Junior toady

    7,796
    71
    224
    I'm trying to set up a powershell script that will email me the statistics of mailbox sizes on our exchange 2007 server. Basically it works so far. It pulls the stats for the mailboxes and puts them into a text file okay. Then it should send the file to me as an attachment but you can see the result below (in red).



    $FromAddress = "[email protected]"
    $ToAddress = "name
    @company.com"
    $MessageSubject = "Mailbox Size Report"
    $MessageBody = "Attached is the current list of mailbox sizes."
    $SendingServer = "fqdn of mail server"



    get-mailboxstatistics | Sort-Object TotalItemSize -Descending | format-table DisplayName,@{Label="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.ToMB()}},ItemCount | format-table DisplayName,TotalItemSize,ItemCount > C:\CustomScripts\mailboxes.txt


    $SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress,
    $MessageSubject, $MessageBody
    $Attachment = New-Object Net.Mail.Attachment("./mailboxes.txt")
    $SMTPMessage.Attachments.Add($Attachment)



    $SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
    $SMTPClient.Send($SMTPMessage)


    [PS] C:\Documents and Settings\me>C:\CustomScripts\MailBoxStatistics.PS1
    Exception calling "Send" with "1" argument(s): "Mailbox unavailable. The server response was: 5.7.1 Message rejected as spam by Content Filtering."
    At C:\CustomScripts\MailBoxStatistics.PS1:21 char:17
    + $SMTPClient.Send( <<<< $SMTPMessage)



    Anyone with any ideas of what I'm doing wrong?
     
    Certifications: A+ | Network+ | Security+ | MCP (270,271,272,290,620) | MCDST | MCTS:Vista
    WIP: MCSA, 70-622,680,685
  2. dwhyte85

    dwhyte85 Nibble Poster

    54
    1
    39
    If that is directly how you've got your script it's probably being caught as spam from:

    $SendingServer = "fqdn of mail server"

    Even with the correct fqdn, eg my.mailserver.com it's likely it'll be caught as spam because it can tell it has been spoofed from the headers, we had masses of problems at my company with MailMarshal being overly pedantic with our Intranet systems using a similar function, the fix was to allow a specific address, eg [email protected] to be a safe address and bypass the rules.
     
    Certifications: Bsc. Comp Sci, MCP, MCTS, MCSA, CCENT, MBCS
    WIP: ICND 2, CEH and converting MCSA to MCITP: Enterprise Administrator
  3. nugget
    Honorary Member

    nugget Junior toady

    7,796
    71
    224
    Thanks for the help.

    I ended up putting the phrase Attached is the current list of mailbox sizes. into the content filter to allow messages with these phrases.

    Works a treat. Now to schedule it to run every week.:biggrin
     
    Certifications: A+ | Network+ | Security+ | MCP (270,271,272,290,620) | MCDST | MCTS:Vista
    WIP: MCSA, 70-622,680,685
  4. dwhyte85

    dwhyte85 Nibble Poster

    54
    1
    39
    If you were to fully authenticate with SMTP/Exchange it'd let you do it without that hassle :P
     
    Certifications: Bsc. Comp Sci, MCP, MCTS, MCSA, CCENT, MBCS
    WIP: ICND 2, CEH and converting MCSA to MCITP: Enterprise Administrator
  5. PAT

    PAT Banned

    158
    1
    27
    try this code it worked for me.



    $file = "c:\name of Attachment file goes here "

    $smtpServer = "severname"
    $msg = new-object Net.Mail.MailMessage
    $att = new-object Net.Mail.Attachment($file)
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)

    $msg.From = "from email address"
    $msg.To.Add("to email address")
    $msg.Subject = " "
    $msg.Body = " "
    $msg.Attachments.Add($att)

    $smtp.Send($msg)

    $att.Dispose()
     

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.