powershell scripts running as windows service dies when user logsout from console

Posted on 2011-10-06
Medium Priority
Last Modified: 2012-05-12
I have a ps script that I am running as a window service using instsrv and srvany from Resource Kit.    If I logon remotely to the machine and then logoff powershell remains running.   If I logon via the console the powershell script is still running.  However if I logoff from the console,  powershell terminates.  

I created a very simple script that just writes to log file and I get the same behaviour,  I can login and out remotely via remote desktop and it stays running.   As soon as I login via the console and then logout,  powershell terminates on the console logout
Question by:milesri
  • 4
  • 3
LVL 71

Expert Comment

ID: 36924114
Are you connecting to the console session when you do from remote?
PowerShell requires that a console window (at Session 0 in W2003) is displayed. As soon as the Window is closed, PowerShell terminates. And if you log off from the console, you kill that window.

Author Comment

ID: 36924378
If I boot the system,   the service starts and powershell is running.    It only gets terminated when I logout after login via console.   Remote Desktop  loggin/logouts don't effect it

the script I am using to test - test.ps1 code


   start-sleep -m 1000
   $timestamp=Get-Date -format 'MMM dd yyy HH:mm:ss'
   $timestamp + "hi there" | Add-Content $debug


Application registry entry
       powershell -noexit test.ps1

test.ps1 is on the path
LVL 71

Expert Comment

ID: 36924402
This does not answer my question. Do you use mstsc /admin for connecting, to get the admin console?
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.


Author Comment

ID: 36924516
For remote conection  I am using mstsc.     So is the problem when I log onto the box via the Console (not remotely) it is using the console session?
LVL 71

Expert Comment

ID: 36924939
If you log in to the local Console session, you are using the console.
If you use mstsc with the /admin (in older RDP clients /console) switch you are using the same session.
If you use mstsc without that switch, you have an own session apart from the Console.

It also can make a difference if you have first logged on locally, then take control over that session via mstsc /admin.

Author Comment

ID: 36925189
So how does it run on a reboot when no one has logged?
LVL 71

Accepted Solution

Qlemo earned 2000 total points
ID: 36925481
The console window of PowerShell is "moved" to the Console session as soon as you log in. That is something very special, and hard to explain.

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Screencast - Getting to Know the Pipeline

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question