且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Java程序内部将Powershell脚本作为Windows服务运行?

更新时间:2022-04-01 18:58:43

您可以创建一个批处理文件,该文件又可以触发如下所示的powershell脚本:

You can create a batch file which, in-turns, can trigger a powershell script like this:

@echo off
Powershell.exe set-executionpolicy remotesigned -File  C:\folder\MyScript.ps1
pause

将其另存为 "Trigger_ps.bat"

Save it as "Trigger_ps.bat"

然后,您可以使用 sc命令来创建Windows服务,方法是提及此批处理文件路径,如下所示:

Then you can use the sc command to create a windows service by mentioning this batch file path like this:

SC CREATE PS_Trigger_Service Displayname= "PS_Trigger_Service" binpath= "C:\folder\Trigger_ps.bat" start= auto

这应该可以解决您的目的.

This should solve your purpose.