执行内联Powershell脚本 [英] Executing inline powershell script

查看:89
本文介绍了执行内联Powershell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Task Scheduler调用嵌入式Powershell脚本,以在触发特定事件时发送电子邮件。我无法在任务计划程序操作窗口中使用本机的发送电子邮件操作,因为SMTP服务器需要SSL,并且无法在操作窗口中指定此操作。因此,我希望启动程序并调用某些东西来发送电子邮件,但我想避免使用第三方应用程序(例如sendEmail),以期希望能够调用类似于以下内容的内联Powershell脚本。 p>

将程序/脚本字段设置为 powershell ,并将参数字段设置为:



-命令 {Send-MailMessage-从名称< name@domain.com>到-至 name@domain.com-主题测试主题-正文位于$(Get-Date -Format dd / MM / yyyy)的测试正文。 -SmtpServer smtp.domain.com -UseSSL}



由于嵌套引号等原因,这显然不起作用,因此我一直在尝试在命令提示符中使用不同的变体,但是我一生都无法弄清楚我需要转义哪些字符并



任何帮助将不胜感激。

解决方案


  1. 只需看一下 powershell /?的示例部分...

PowerShell -Command & {Get-EventLog -LogName Application} p>





  1. 看看如何使用引号。

PowerShell -Command & {Send-MailMessage-从名称-到名称@ domain.com-主题 Test Subject -Body(测试正文位于{0}。 -f(Get-Date -Format'dd / MM / yyyy')) -SmtpServer'smtp .domain.com'-UseSSL}


I'm trying to invoke an inline powershell script from Task Scheduler to send an email when a particular event is triggered. 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. 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.

Setting the 'Program/script' field to powershell and the arguments field to:

-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.

解决方案

  1. Just take a look to the example section of "powershell /?" ...

PowerShell -Command "& {Get-EventLog -LogName Application}"


  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脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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