Solved

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

Posted on 2011-10-06
487 Views
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
0
Question by:milesri

LVL 67

Expert Comment

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.
0

Author Comment

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

$debug="C:\temp\rm\debug.log" while(1) { 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
0

LVL 67

Expert Comment

This does not answer my question. Do you use mstsc /admin for connecting, to get the admin console?
0

Author Comment

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?
0

LVL 67

Expert Comment

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.
0

Author Comment

So how does it run on a reboot when no one has logged?
0

LVL 67

Accepted Solution

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.
0

## Featured Post

A quick step-by-step overview of installing and configuring Carbonite Server Backup.
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…