Discussion:
Powershell script help
(too old to reply)
Tboz
2008-07-30 21:21:08 UTC
Permalink
I'm new to this and have pieced together a script, but it is not working.
Can you help me figure out why I can't get this to email to me? I can get
the file to output but get a bunch of null errors

$FromAddress = ***@xxx.org
$ToAddress = ***@xxx.org
$MessageSubject = "Mailbox Size Report"
$MessageBody = "Attached is the current list of mailbox sizes."
$SendingServer = "xxx.org"

###Now get the stats and store in a text file
Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft
DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount > mailboxes.txt

###Create the mail message and add the statistics text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress,
$ToAddress,
$MessageSubject, $MessageBody
$Attachment = New-Object Net.Mail.Attachment("./mailboxes.txt")
$SMTPMessage.Attachments.Add($Attachment)

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



_______ERRORS_______________
New-Object : Exception calling ".ctor" with "4" argument(s): "The parameter
'from' cannot be an emp
ty string.
Parameter name: from"
At C:\emailstats.ps1:11 char:26
+ $SMTPMessage = New-Object <<<< System.Net.Mail.MailMessage $FromAddress,
$ToAddress,
You cannot call a method on a null-valued expression.
At C:\emailstats.ps1:14 char:29
+ $SMTPMessage.Attachments.Add( <<<< $Attachment)
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: message"
At C:\emailstats.ps1:18 char:17
+ $SMTPClient.Send( <<<< $SMTPMessage)
Jon
2008-07-30 21:40:25 UTC
Permalink
The absence of quotes might be causing the problem ....

$FromAddress = '***@xxx.org'
$ToAddress = '***@xxx.org'
--
Jon
Post by Tboz
I'm new to this and have pieced together a script, but it is not working.
Can you help me figure out why I can't get this to email to me? I can get
the file to output but get a bunch of null errors
$MessageSubject = "Mailbox Size Report"
$MessageBody = "Attached is the current list of mailbox sizes."
$SendingServer = "xxx.org"
###Now get the stats and store in a text file
Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft
Post by Tboz
mailboxes.txt
###Create the mail message and add the statistics text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress,
$ToAddress,
$MessageSubject, $MessageBody
$Attachment = New-Object Net.Mail.Attachment("./mailboxes.txt")
$SMTPMessage.Attachments.Add($Attachment)
###Send the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)
_______ERRORS_______________
New-Object : Exception calling ".ctor" with "4" argument(s): "The parameter
'from' cannot be an emp
ty string.
Parameter name: from"
At C:\emailstats.ps1:11 char:26
+ $SMTPMessage = New-Object <<<< System.Net.Mail.MailMessage
$FromAddress,
$ToAddress,
You cannot call a method on a null-valued expression.
At C:\emailstats.ps1:14 char:29
+ $SMTPMessage.Attachments.Add( <<<< $Attachment)
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: message"
At C:\emailstats.ps1:18 char:17
+ $SMTPClient.Send( <<<< $SMTPMessage)
Tboz
2008-07-31 13:56:01 UTC
Permalink
I now get this error when it starts to run:

The term '***@xxx.org' is not recognized as a cmdlet, function,
operable progra
m, or script file. Verify the term and try again.
At C:\emailstats.ps1:1 char:47
+ $FromAddress = ***@xxx.org <<<<
The term '***@xxx.org' is not recognized as a cmdlet, function, operable
program, or sc
ript file. Verify the term and try again.
At C:\emailstats.ps1:2 char:37
Post by Jon
The absence of quotes might be causing the problem ....
--
Jon
Post by Tboz
I'm new to this and have pieced together a script, but it is not working.
Can you help me figure out why I can't get this to email to me? I can get
the file to output but get a bunch of null errors
$MessageSubject = "Mailbox Size Report"
$MessageBody = "Attached is the current list of mailbox sizes."
$SendingServer = "xxx.org"
###Now get the stats and store in a text file
Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft
Post by Tboz
mailboxes.txt
###Create the mail message and add the statistics text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress,
$ToAddress,
$MessageSubject, $MessageBody
$Attachment = New-Object Net.Mail.Attachment("./mailboxes.txt")
$SMTPMessage.Attachments.Add($Attachment)
###Send the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)
_______ERRORS_______________
New-Object : Exception calling ".ctor" with "4" argument(s): "The parameter
'from' cannot be an emp
ty string.
Parameter name: from"
At C:\emailstats.ps1:11 char:26
+ $SMTPMessage = New-Object <<<< System.Net.Mail.MailMessage $FromAddress,
$ToAddress,
You cannot call a method on a null-valued expression.
At C:\emailstats.ps1:14 char:29
+ $SMTPMessage.Attachments.Add( <<<< $Attachment)
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: message"
At C:\emailstats.ps1:18 char:17
+ $SMTPClient.Send( <<<< $SMTPMessage)
Justin Rich
2008-08-01 13:58:37 UTC
Permalink
should be " not ' i believe, its a normal string your passing to the
method... and speaking of methods (in this case, constructors), when you
call them, you need (args)

for example you did
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer

it should be
$SMTPClient = New-Object System.Net.Mail.SMTPClient($SendingServer)

EXAMPLE:

$smtp = New-Object System.Net.Mail.SmtpClient
$mailto = New-Object System.Net.Mail.MailAddress("***@here.com")
$mailfrom = New-Object System.Net.Mail.MailAddress("***@there.com")
$msg = New-Object System.Net.Mail.MailMessage($mailfrom, $mailto)

$smtp.host = "smtp.server.com"
$msg.subject = "im a test!"
$msg.body = "testing testing... 1 2 3"
$smtp.send($msg)
Post by Tboz
operable progra
m, or script file. Verify the term and try again.
At C:\emailstats.ps1:1 char:47
program, or sc
ript file. Verify the term and try again.
At C:\emailstats.ps1:2 char:37
Post by Jon
The absence of quotes might be causing the problem ....
--
Jon
Post by Tboz
I'm new to this and have pieced together a script, but it is not working.
Can you help me figure out why I can't get this to email to me? I can get
the file to output but get a bunch of null errors
$MessageSubject = "Mailbox Size Report"
$MessageBody = "Attached is the current list of mailbox sizes."
$SendingServer = "xxx.org"
###Now get the stats and store in a text file
Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft
Post by Tboz
mailboxes.txt
###Create the mail message and add the statistics text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress,
$ToAddress,
$MessageSubject, $MessageBody
$Attachment = New-Object Net.Mail.Attachment("./mailboxes.txt")
$SMTPMessage.Attachments.Add($Attachment)
###Send the message
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.Send($SMTPMessage)
_______ERRORS_______________
New-Object : Exception calling ".ctor" with "4" argument(s): "The parameter
'from' cannot be an emp
ty string.
Parameter name: from"
At C:\emailstats.ps1:11 char:26
+ $SMTPMessage = New-Object <<<< System.Net.Mail.MailMessage $FromAddress,
$ToAddress,
You cannot call a method on a null-valued expression.
At C:\emailstats.ps1:14 char:29
+ $SMTPMessage.Attachments.Add( <<<< $Attachment)
Exception calling "Send" with "1" argument(s): "Value cannot be null.
Parameter name: message"
At C:\emailstats.ps1:18 char:17
+ $SMTPClient.Send( <<<< $SMTPMessage)
Loading...