问题描述
我有一个运行良好的 powershell 脚本,我在 power shelle 命令行中调用此脚本:
PS C:>.\myscript.ps1 -var1 variable1 -var2 variable2
我需要使用愚蠢的 sc create 将其安装在服务中,但我找不到启动它的方法
有什么帮助吗?
非常感谢.
使用 Windows 服务包装器.它是高度可配置的,并提供了许多有用的选项,例如日志轮换、服务帐户、失败操作等.它甚至可以自动从 URL 下载资源并将其作为文件放置在本地,因此您可以将脚本托管在某处并自动更新.以下是启动和运行 PowerShell 服务的基本步骤:
- 下载\构建 winsw.可以在此处找到 1.x 版二进制文件.要构建最新的 winsw 2.x,请使用 Visual Studio Community 2013(开源项目免费).更详细的构建说明位于此处.
为您的服务创建 XML 配置文件.示例:
<id>MyPsSvc</id><name>我的 PowerShell 服务</name><description>用你的电脑做一些有趣的事情</description><executable>PowerShell.exe</executable><logmode>重置</logmode><arguments>-ExecutionPolicy Bypass -NoLogo -NoProfile -NonInteractive -WindowStyle Hidden -File "myscript.ps1" -var1 variable1 -var2 variable2</arguments></服务>
配置文件必须与 winsw 可执行文件具有相同的基名,即:
- winsw.exe
- winsw.xml
如果您打算在没有互联网连接的 PC 上使用您的服务,禁用 winsw 的数字签名验证.为此,请创建文件
<运行时><generatePublisherEvidence enabled="false"/></运行时></配置>winsw.exe.config
.示例:如果您运行的是 Windows Server 2012 或 Windows 8 并且不想安装 .Net 2.0 支持,在上述配置文件中指定.NET 4.0 运行时支持:
<运行时><generatePublisherEvidence enabled="false"/></运行时><启动><supportedRuntime version="v2.0.50727"/><supportedRuntime version="v4.0"/></启动></配置>安装您的服务,从命令行运行
winsw.exe install
.安装服务后,您可以从 Windows 服务管理器启动它.
I have a powershell script running very well ,I'm calling this script within a power shelle command line :
PS C:> .\myscript.ps1 -var1 variable1 -var2 variable2
I need to mount this inside a service with a stupid sc create but i couldn't find a way to launch it
any help please ?
Thanks a lot.
Use Windows service wrapper. It's highly configurable and provides a lot of useful options, like log rotation, service account, on failure actions and so on. It even can automatically download resources from URL and place it locally as a file, so you can host your script somewhere and have it auto updated. Here is the basic steps to get your PowerShell service up and running:
- Download\build winsw. Version 1.x binaries can be found here. To build latest winsw 2.x use Visual Studio Community 2013 (free for open-source projects). More detailed build instructions are here.
Create XML configuration file for your service. Example:
<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>
The configuration file must have the same basename as winsw executable, i.e.:
- winsw.exe
- winsw.xml
If you plan to use your service on the PC without internet connection, disable digital signature verification for winsw. To do this create file
winsw.exe.config
. Example:<configuration> <runtime> <generatePublisherEvidence enabled="false"/> </runtime> </configuration>
If you're running Windows Server 2012 or Windows 8 and and don't want to install .Net 2.0 support, specify .NET 4.0 runtime support in the abovementioned config file:
<configuration> <runtime> <generatePublisherEvidence enabled="false"/> </runtime> <startup> <supportedRuntime version="v2.0.50727" /> <supportedRuntime version="v4.0" /> </startup> </configuration>
Install your service, running
winsw.exe install
from command line. Once the service is installed, you can start it from Windows service manager.
这篇关于在 Windows 服务中运行 powershell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!