none
Ok one more time - running Powershell as a service RRS feed

  • Question

  • Hi,

    I have a script that watches a folder for any new files created. The problem of using this is that Powershell needs to be running on the machine.

    If powershell is close the jobs go away.

    Is there any way I can run this script as a service ? So that powershell is on all the time.

    Thank you

     

     

     

    Friday, January 7, 2011 5:28 PM

Answers

  • Is there any reason why you cannot use the Task Scheduler for this?

    Set up the task running as a user who already has a powershell profile defined

    You can use the -noninteractive switch to avoid seeing a powershell console window.

    Set the task to run every 1 minute, and set the 'If the task is already running' to 'Do not start a new instance'

    This way if the script stops for any reason it will restart after 1 minute - I don't think running it as a service will do that.

    This applies only to the Windows Vista Task scheduler and above.

    See

    c:\Windows\System32\WindowsPowerShell\v1.0>powershell /?

    for command line options.

     


    Just a humble SysAdmin
    • Marked as answer by RoshDsa Monday, January 10, 2011 4:00 PM
    Friday, January 7, 2011 10:20 PM
  • You can run a PowerShell script as a service using srvany (http://support.microsoft.com/kb/137890 ).

    Just make sure you have a loop in your script (e.g. while ( $true ) { your code} )

    Best, Dajo

    • Marked as answer by RoshDsa Monday, January 10, 2011 4:00 PM
    Saturday, January 8, 2011 3:31 PM

All replies

  • Is there any reason why you cannot use the Task Scheduler for this?

    Set up the task running as a user who already has a powershell profile defined

    You can use the -noninteractive switch to avoid seeing a powershell console window.

    Set the task to run every 1 minute, and set the 'If the task is already running' to 'Do not start a new instance'

    This way if the script stops for any reason it will restart after 1 minute - I don't think running it as a service will do that.

    This applies only to the Windows Vista Task scheduler and above.

    See

    c:\Windows\System32\WindowsPowerShell\v1.0>powershell /?

    for command line options.

     


    Just a humble SysAdmin
    • Marked as answer by RoshDsa Monday, January 10, 2011 4:00 PM
    Friday, January 7, 2011 10:20 PM
  • You can run a PowerShell script as a service using srvany (http://support.microsoft.com/kb/137890 ).

    Just make sure you have a loop in your script (e.g. while ( $true ) { your code} )

    Best, Dajo

    • Marked as answer by RoshDsa Monday, January 10, 2011 4:00 PM
    Saturday, January 8, 2011 3:31 PM
  • Hi,

     

    Thanks for posting in Microsoft TechNet forums.

     

    Based on my research, I found the following links that are helpful:

     

    Running a Powershell Script as a Service

     

    Running PowerShell Scripts as a Service

     

    Please note: we provide the third party links for your references. Microsoft doesn’t hold these links and can’t guarantee any changes.

     

    There is also a similar post for your reference:

     

    Powershell scipt as service or running in background

     

    Best Regards

    Dale Qiao
    TechNet Subscriber Support in forum. If you have any feedback on our support, please contact tngfb@microsoft.com


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
    Monday, January 10, 2011 7:42 AM
    Moderator
  • Hi RoshDsa,

    Yes You can run a powershell program as a service by using visual studio windows service.

    1. Create a windows service.
    2. create a run space by using namespace System.Management.Automation.Runspaces;
    3. and create a pipeline and invoke it to execute the powershell script
    4. and then install the service by using Installutil

    And now your service will work on background asynchronously .

     

    -Raj

    • Proposed as answer by arajesh Tuesday, January 11, 2011 5:23 AM
    Monday, January 10, 2011 1:12 PM
  • This is a very good  suggestion, the only reason I donot want to use the task manager, is because I have to automate the installation of the script on nearly 2000 machines.

    Managing the task scheduler is another extra task.

    Monday, January 10, 2011 4:03 PM