Hi All,
With this 'BIG' script I'm writing - I'd like to be able to schedule the script to run, log the output to a file and then email the file to the Admin at the end.
At the moment I've been able to run the script manually - sending data out of the Std-out with 'write-host'. However - how am I best to write to a log file??
Can I just write-host "test test" > logfile.log?? - if so - how can I append consistently to this log.
And Once I have it - how can I grab the contents in as a string to send as email??
I've done elements like this in the past with VB6 (I know, I know - thats just how old I am!). Creating a log object - init-ing the object creates the log, sending a method of "Add Entry" with the Text to add and the Object then writes with the current date/time followed by the string etc. Is this sort of Obj Orientated code possible here?? Would be nice and easy to port the Logging object/functions between scripts if called as a remote function (see answers from Hal and Luc in my previous post).
Thanks, as always, in anticipation.
TG
For writing output to a file you best use the Write-Output cmdlet.
And you use the "classic" redirection (like in DOS) to direct it to file.
Something like this
Write-Output "Line 1" > c:\temp\report.txt Write-Output "Line 2" >> c:\temp\report.txt Write-Output "Line 3" >> c:\temp\report.txt
The first redirect initialises the output file.
If you don't want that replace ">" by ">>".
Getting the contents of the file into a variable is something that PowerShell does very well.
$report = Get-Content c:\temp\report.txt
Sending the report with email can be done like this
$SmtpClient = New-Object system.net.mail.smtpClient $MailMessage = New-Object system.net.mail.mailmessage $SmtpClient.host = "<smtp-server>" $MailMessage.from = "<from-address>" $MailMessage.To.add("<to-address>") $MailMessage.IsBodyHtml = 1 $MailMessage.Subject = "My report" $MailMessage.body = "report attached" $MailMessage.Attachments.Add("c:\temp\report.txt") $SmtpClient.Send($MailMessage)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
For writing output to a file you best use the Write-Output cmdlet.
And you use the "classic" redirection (like in DOS) to direct it to file.
Something like this
Write-Output "Line 1" > c:\temp\report.txt Write-Output "Line 2" >> c:\temp\report.txt Write-Output "Line 3" >> c:\temp\report.txt
The first redirect initialises the output file.
If you don't want that replace ">" by ">>".
Getting the contents of the file into a variable is something that PowerShell does very well.
$report = Get-Content c:\temp\report.txt
Sending the report with email can be done like this
$SmtpClient = New-Object system.net.mail.smtpClient $MailMessage = New-Object system.net.mail.mailmessage $SmtpClient.host = "<smtp-server>" $MailMessage.from = "<from-address>" $MailMessage.To.add("<to-address>") $MailMessage.IsBodyHtml = 1 $MailMessage.Subject = "My report" $MailMessage.body = "report attached" $MailMessage.Attachments.Add("c:\temp\report.txt") $SmtpClient.Send($MailMessage)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi,
am trying to send out a mail with the exact above commands in a script, but I keep on getting an error on the last send command:
Exception calling "Send" with "1" argument(s): "Failure sending mail."
At C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\Scripts\DMZvm.ps1:21 char:17
+ $SmtpClient.Send( <<<< $MailMessage)
Am using the exact commands like above (obviously tried some variations), but always get same error ??
Any idea ?
BR, Peter
All,
never mind, forgot to remove the >< signs...
All is working now.
Peter