Run a powershell script inside a windows service

若如初见. 提交于 2020-01-14 05:17:00

问题


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:

  1. 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.
  2. 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
  3. 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>
    
  4. 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>
    
  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!