Can you write Windows services in Powershell

前端 未结 5 1313
执念已碎
执念已碎 2020-12-18 14:38

I have written a program in PowerShell that loops and checks stuff. I would like to convert this into a Windows service. I\'ve created a Windows service (in Admin->Services)

5条回答
  •  臣服心动
    2020-12-18 14:58

    There are a few items at issue here:

    • Are you sure that you really need a service? I agree with the comments about using the scheduler for running periodic tasks. The scheduler can start tasks as Administrator.
    • Services call a special set of APIs to communicate with the Service Control Manager, so an ordinary program can't be used directly.
    • The Service Control Manager uses CreateProcessAsUser to start the service process, so you need to point the SCM to an executable binary, as opposed to a script.

    If you can't use the scheduler (though I strongly encourage you to try), I suppose that you could write an executable binary that acts as a service. It would then execute PowerShell and your script on your behalf. I'm thinking something like the srvany program that used to be included with the Windows Server 2003 Resource Kit could bootstrap the service for you.

提交回复
热议问题