将脚本输出到日志文件 [英] Email script output to logfile

查看:117
本文介绍了将脚本输出到日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我对这门语言还不熟悉并得到了你们的帮助。我写了一个脚本(有一些帮助)来发送文件夹中的几个文件。这非常有效。但由于这个脚本是用在一个糟糕的互联网连接上,我想将b $ b输出从脚本到日志文件的结果,所以我可以定期检查这个,看看是否所有的邮件都是sendt。希望有人可以帮助我:-)我的代码到目前为止是: 

I am fairly new to this language and has gotten good help from you guys. I have written a script (with some help) to email several files in a folder. This works great. But since this script is to be used on a bad internet connection I would like to maybe output the results from the script to a logfile so I can check this on a regular basis to see if all the mails have been sendt. Hope someone can help me :-) My code so far is: 

#Connection Details
$username="jfgs@gmail.com"
$password="xxxxxxxx"
$smtpServer = "smtp.gmail.com"
$msg = new-object Net.Mail.MailMessage

#Change port number for SSL to 587
$smtp = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 

#Uncomment Next line for SSL  
$smtp.EnableSsl = $true

$smtp.Credentials = New-Object System.Net.NetworkCredential( $username, $password )

#From Address
$msg.From = "CMSUPPORT@NFO.no"
#To Address, Copy the below line for multiple recipients
$msg.To.Add("Chris@NFO.no")

#Message Body
$msg.Body="Please See Attached Database Files"

#Message Subject
$msg.Subject = "DATABASE"
write-host "SENDING FILES"

#your file location
$files=Get-ChildItem "C:\CM\Send\"
Foreach($file in $files)
{
Write-Host "Attaching File :- " $file
$attachment = New-Object System.Net.Mail.Attachment –ArgumentList $file.FullName
$msg.Attachments.Add($attachment)
$smtp.Send($msg)
$msg.Attachments.Clear()
}
$smtp.Send($msg)
$attachment.Dispose();
$msg.Dispose();
write-host "Mail Sent Successfully"

谢谢: - )

推荐答案

你在找这个


log =" deploy- {0:yyyyMMdd-HHmmss} .RTF" -f(Get-Date)
Start-Transcript -Path
log = "deploy-{0:yyyyMMdd-HHmmss}.RTF" -f (Get-Date) Start-Transcript -Path


log

#Connection Details
log #Connection Details


这篇关于将脚本输出到日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆