保持 PowerShell window 打开和任务计划程序
Keeping PowerShell window open and Task Scheduler
我有一个 PowerShell 脚本,我每天为 运行 创建一个任务。是否可以打开 PowerShell window(而不是在 运行ning 之后关闭)以便我可以确保它 运行 没有错误?当我用下面的命令安排时,没有 window 弹出所以我不知道它是否真的完成了。
powershell -ExecutionPolicy ByPass -File “Q:\myscript.ps1” -NoExit
此外,在脚本本身中,我有一行可以在记事本中打开一个文本文件(来自 Start-Transcript 的脚本副本)。当我手动 运行 脚本时,它会打开记事本,但计划任务没有。是否有打开它的设置?
$validationPath = "Q:\transcript.txt"
& Notepad $validationPath
为了 运行 交互,您需要 select 运行 只有当用户登录时 在安全选项下。来自 Task Security Context help:
You can specify that a task should run even if the account under which the task is scheduled to run is not logged on when the task is triggered. To do this, select the radio button labeled Run whether user is logged on or not . If this radio button is selected, tasks will not run interactively. To make a task run interactively, select the Run only when user is logged on radio button.
也就是说,如果它是 运行ning 作为计划任务,在大多数情况下最好记录输出 and/or 失败时发送电子邮件。
对于日志记录,您可以使用 Start-Transcript
,如下所示:
Start-Transcript -Path ('{0}\TaskName.{1:yyyy-MM-dd}.log' -f $env:temp, (Get-Date))
# Code here
Stop-Transcript
对于电子邮件通知,您可以使用 Send-MailMessage
try {
# Code here
} catch {
Send-MailMessage -To 'errors@domain.com' -From 'alerting@domain.com' -Subject 'Error in script TaskName' -Body "Error $($_.Message) received while running TaskName" -SmtpServer smtp.domain.com
}
我有一个 PowerShell 脚本,我每天为 运行 创建一个任务。是否可以打开 PowerShell window(而不是在 运行ning 之后关闭)以便我可以确保它 运行 没有错误?当我用下面的命令安排时,没有 window 弹出所以我不知道它是否真的完成了。
powershell -ExecutionPolicy ByPass -File “Q:\myscript.ps1” -NoExit
此外,在脚本本身中,我有一行可以在记事本中打开一个文本文件(来自 Start-Transcript 的脚本副本)。当我手动 运行 脚本时,它会打开记事本,但计划任务没有。是否有打开它的设置?
$validationPath = "Q:\transcript.txt"
& Notepad $validationPath
为了 运行 交互,您需要 select 运行 只有当用户登录时 在安全选项下。来自 Task Security Context help:
You can specify that a task should run even if the account under which the task is scheduled to run is not logged on when the task is triggered. To do this, select the radio button labeled Run whether user is logged on or not . If this radio button is selected, tasks will not run interactively. To make a task run interactively, select the Run only when user is logged on radio button.
也就是说,如果它是 运行ning 作为计划任务,在大多数情况下最好记录输出 and/or 失败时发送电子邮件。
对于日志记录,您可以使用 Start-Transcript
,如下所示:
Start-Transcript -Path ('{0}\TaskName.{1:yyyy-MM-dd}.log' -f $env:temp, (Get-Date))
# Code here
Stop-Transcript
对于电子邮件通知,您可以使用 Send-MailMessage
try {
# Code here
} catch {
Send-MailMessage -To 'errors@domain.com' -From 'alerting@domain.com' -Subject 'Error in script TaskName' -Body "Error $($_.Message) received while running TaskName" -SmtpServer smtp.domain.com
}