简体   繁体   English

执行内联Powershell脚本

[英]Executing inline powershell script

I'm trying to invoke an inline powershell script from Task Scheduler to send an email when a particular event is triggered. 我正在尝试从Task Scheduler调用嵌入式Powershell脚本,以在触发特定事件时发送电子邮件。 I can't use the native 'Send an e-mail' action in the Task Scheduler action window because the SMTP server requires SSL and there's no way to specify this in the action window. 我无法在“任务计划程序”操作窗口中使用本机的“发送电子邮件”操作,因为SMTP服务器需要SSL,并且无法在操作窗口中指定此操作。 So I'm looking to 'Start a program' and invoke something to send email but I want to avoid using 3rd party applications such as sendEmail if possible so was hoping to be able to invoke an inline powershell script similar to the following. 因此,我希望“启动一个程序”并调用某些东西来发送电子邮件,但我想避免使用第三方应用程序(例如sendEmail),以期希望能够调用类似于以下内容的内联Powershell脚本。

Setting the 'Program/script' field to powershell and the arguments field to: 将“程序/脚本”字段设置为powershell ,将参数字段设置为:

-Command "{Send-MailMessage -From "Name <name@domain.com>" -To "name@domain.com" -Subject "Test Subject" -Body "Test body at $(Get-Date -Format "dd/MM/yyyy")." -SmtpServer "smtp.domain.com" -UseSSL}"

This obviously doesn't work due to the nested quotes etc, so I've been trying different variations in command prompt, but I can't for the life of me figure out which characters I need to escape and how to escape them. 由于嵌套引号等原因,这显然不起作用,因此我一直尝试在命令提示符中使用不同的变体,但是我一生都无法弄清楚我需要转义哪些字符以及如何转义它们。

Any help would be much appreciated. 任何帮助将非常感激。

This should work: 这应该工作:

powershell -Command 'Send-MailMessage -From "Name <name@domain.com>" -To "name@domain.com" -Subject "Test Subject" -Body "Test body at $(Get-Date -Format "dd/MM/yyyy")." -SmtpServer "smtp.domain.com" -UseSSL'

You can use single quotes around double quotes, and you don't need the curly brackets around the command. 您可以在双引号周围使用单引号,并且不需要在命令前后使用大括号。

  1. Just take a look to the example section of "powershell /?" 只需看一下“ powershell /?”的示例部分 ... ...

PowerShell -Command " & {Get-EventLog -LogName Application}" PowerShell-命令“ {Get-EventLog -LogName应用程序}”


  1. Take a look how to use quotes. 看看如何使用引号。

PowerShell -Command " & {Send-MailMessage -From 'Name ' -To 'name@domain.com' -Subject 'Test Subject' -Body ('Test body at {0}.' -f (Get-Date -Format 'dd/MM/yyyy')) -SmtpServer 'smtp.domain.com' -UseSSL} " PowerShell-命令 &{发送邮件消息-从'名称'-到'name@domain.com'-主题'测试主题' -正文('测试正文在{0}。'-f(获取日期-格式' dd / MM / yyyy')) -SmtpServer'smtp.domain.com'-UseSSL}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM