windows - 如何创建 Windows 服务来运行 powershell 脚本?

标签 windows powershell windows-services powershell-2.0 windows-server-2008-r2

我有无限循环 PowerShell(测试目的)脚本,我想在 Windows Server 2008 R2(标准版)中作为服务运行。

我正在使用以下命令创建 Windows 服务,

sc.exe create "My PS1Service" binPath= "powershell.exe -NoLogo -Path D:\TEST\test.ps1"

结果是[SC] CreateService SUCCESS

但是当我尝试运行服务 My PS1Service 时,它返回以下错误

Windows Could not srart the My PS1Service service on Local
Computer
Error 1053: The service did not respond to the start or control
request in a family

这里有任何帮助!!!

最佳答案

由于文章太长,我不想将它们粘贴到答案中。

查看这篇文章,这是我找到的有关 powershell 服务的最全面的信息来源:https://msdn.microsoft.com/en-us/magazine/mt703436.aspx

链接到示例 powershell 服务: http://jf.larvoire.free.fr/progs/PSService.ps1

我最终没有成功修改它,因为与对我的用例使用不同的方法相比,我需要更长的时间来阅读和理解它,但我肯定会回到那个如果我将来需要在 powershell 中创建服务。

还有这个:https://www.sapien.com/blog/2017/07/12/write-a-windows-service-in-powershell/

关于windows - 如何创建 Windows 服务来运行 powershell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26506010/

相关文章:

windows - 从其他子模块访问子模块的函数

powershell - 比较 Powershell 中的 Filehash

parsing - 从 CSV 中删除行

c# - 在 Windows 服务上调用公共(public)方法

windows-7 - Redis 服务不会在 Windows 上启动

windows - 如何自动更新 Subversion 工作副本?

windows - 检索环境变量的RAW内容[不扩展]

windows - NetBIOS 与 Socket API 的关系

function - 从 powershell 为 azure 函数创建函数键

node.js - Windows 服务在我的 NodeJS 项目目录上执行 `npm start`