我有一个运行良好的 powershell 脚本,
我在 power shelle 命令行中调用此脚本:
PS C:> .\myscript.ps1 -var1 variable1 -var2 variable2
我需要使用愚蠢的 sc create 将其安装在服务中,但我找不到启动它的方法
有什么帮助吗?
多谢。
Use Windows 服务包装器 https://github.com/kohsuke/winsw。它具有高度可配置性,并提供许多有用的选项,例如日志轮换、服务帐户、失败操作等。它甚至可以自动从 URL 下载资源并将其作为文件放置在本地,因此您可以将脚本托管在某处并自动更新。以下是启动并运行 PowerShell 服务的基本步骤:
- 下载\构建winsw.可以找到 1.x 版本的二进制文件here http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/。构建最新的温莎2.x https://github.com/kohsuke/winsw/tree/winsw-2.0 use Visual Studio 社区 2013 http://www.visualstudio.com/en-us/news/vs2013-community-vs.aspx(对于开源项目免费)。更详细的构建说明是here https://github.com/kohsuke/winsw#build-environment.
-
为您的服务创建 XML 配置文件。例子:
<service>
<id>MyPsSvc</id>
<name>My PowerShell Service</name>
<description>Does funny stuff with your PC</description>
<executable>PowerShell.exe</executable>
<logmode>reset</logmode>
<arguments>-ExecutionPolicy Bypass -NoLogo -NoProfile -NonInteractive -WindowStyle Hidden -File "myscript.ps1" -var1 variable1 -var2 variable2</arguments>
</service>
配置文件必须与 winsw 可执行文件具有相同的基本名称,即:
-
如果您打算在没有互联网连接的电脑上使用您的服务,禁用 winsw 的数字签名验证 https://github.com/kohsuke/winsw/tree/winsw-2.0#offline-environment-and-authenticode。为此,创建文件winsw.exe.config
。例子:
<configuration>
<runtime>
<generatePublisherEvidence enabled="false"/>
</runtime>
</configuration>
-
如果您运行的是 Windows Server 2012 或 Windows 8 并且不想安装 .Net 2.0 支持,指定 .NET 4.0 运行时支持 https://github.com/kohsuke/winsw/tree/winsw-2.0#net-runtime-40在上述配置文件中:
<configuration>
<runtime>
<generatePublisherEvidence enabled="false"/>
</runtime>
<startup>
<supportedRuntime version="v2.0.50727" />
<supportedRuntime version="v4.0" />
</startup>
</configuration>
安装你的服务,运行winsw.exe install
从命令行。安装该服务后,您可以从 Windows 服务管理器启动它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)