问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/26890393/run-a-powershell-script-inside-a-windows-service