简体   繁体   English

重新启动后如何运行PowerShell脚本?

[英]How can I run a PowerShell script after reboot?

I have a powershell script that tails specifics logs. 我有一个Powershell脚本,它尾随特定日志。 If there is no update to the logs within a specific time span, an alert is then sent to nagios (as this depicts that the service is no longer running). 如果在特定时间段内没有更新日志,则会向nagios发送警报(因为这表明该服务不再运行)。

The powershell script works great when run manually, but my issue is that I want it to load up on reboot. 当手动运行时,powershell脚本运行良好,但是我的问题是我希望它在重新启动时加载。 I've tried creating a scheduled task that repeats itself every 5 minutes using the arguments '-noexit -file C::\\script.ps1'. 我尝试创建一个计划任务,该任务使用参数'-noexit -file C :: \\ script.ps1'每5分钟重复一次。 The problem is then that my script doesn't actually work when run as a scheduled task. 问题是,当我按计划任务运行时,我的脚本实际上不起作用。

The execution policy is set to Unrestricted, so the script runs, but the code doesn't execute and work like it does when manually run. 执行策略设置为“不受限制”,因此脚本可以运行,但是代码无法执行,无法像手动运行时那样工作。

FWIW, the code is: FWIW,代码为:

function Write-EventlogCustom($msg) {
    Write-EventLog System -source System -eventid 12345 -message $msg
}

Get-Content -Path C:\test.log -Wait | % {Write-EventlogCustom $_}

So if I update test.log while the powershell script runs a scheduled task, the event log doesn't get updated. 因此,如果我在powershell脚本运行计划任务时更新了test.log,则事件日志不会更新。 However, when I run this script manually, and update to test.log, it does appear in the event viewer. 但是,当我手动运行此脚本并更新到test.log时,它确实出现在事件查看器中。

I'm hoping that a second set of eyes might find something that I may have missed? 我希望第二只眼睛可以找到我可能错过的东西?

As @Tim Ferrill has mentioned, I needed to run the process with task schedulers 'Run with highest privileges' setting. 正如@Tim Ferrill所提到的,我需要使用任务调度程序“以最高特权运行”设置来运行该过程。 This resolved the issue. 这解决了问题。

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

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