2017-06-03 70 views
1

我正在尝试创建一个批处理文件,该文件将创建一个将通过PowerShell发送电子邮件的文件。它做了很多事情,但电子邮件只是其中的一小部分。以下是我迄今为止:如何用引号将代码添加到使用批处理的文件中?

REM Creates batch file with contents of the powershell script 
 
echo "I need to add the below powershell script" > email.bat 
 
echo "into email.bat with all the quotes included">> email.bat 
 
echo "like what I'm doing now with appending text">> email.bat 
 
REM powershell script to send email. 
 

 

 
$smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com") 
 

 
if($Env:SmtpUseCredentials -eq "true") { 
 
    $credentials = new-object Net.NetworkCredential("username","password") 
 
    $smtp.Credentials = $credentials 
 
} 
 
$objMailMessage = New-Object System.Net.Mail.MailMessage 
 
$objMailMessage.From = "[email protected]" 
 
$objMailMessage.To.Add("[email protected]") 
 
$objMailMessage.Subject = "Logs for today" 
 
$objMailMessage.Body = "(the logs)" 
 

 
$smtp.send($objMailMessage)

在批,我将文本追加到另一个文件下来,如果有什么我加入没有报价,但我需要的方式将包含引号的文本添加到文件中。

+0

你有没有试图逃跑字符串里面的人物如'\“'? – Jonas

+0

用批处理文件分配并在PowerShell中写入脚本,该脚本包含一个内置的Send-MailMessage cmdlet –

回答

0

为什么你不简单地回应没有引号和逃脱可能的字符<>|&与插入符号^和双百分号?
在(代码块),你已经还逃避关闭括号^)

@Echo off 
(Echo:$smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com"^) 
    Echo: 
    Echo:if($Env:SmtpUseCredentials -eq "true" ^) { 
    Echo: $credentials = new-object Net.NetworkCredential("username","password"^) 
    Echo: $smtp.Credentials = $credentials 
    Echo:} 
    Echo:$objMailMessage = New-Object System.Net.Mail.MailMessage 
    Echo:$objMailMessage.From = "[email protected]" 
    Echo:$objMailMessage.To.Add("[email protected]"^) 
    Echo:$objMailMessage.Subject = "Logs for today" 
    Echo:$objMailMessage.Body = "(the logs^)" 
    Echo: 
    Echo:$smtp.send($objMailMessage^) 
) > "SomeFile.ps1" 
+0

非常感谢您试图通过分隔符或连接两个变量或一系列不同的问题,我只是试了一下,它的工作原理和它应该完全一样。谢谢! – Daniel

+0

那么,在Stack Exchange系列网站中,首选感谢的方式是投票和/或检查标记答案。 – LotPings

+0

我有,但它表示它不会显示,除非它收到15票,我只是现在勾选了它。 – Daniel

相关问题