Solved

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

Posted on 2011-10-06
Medium Priority
490 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
• 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.
0

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

$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 71

Expert Comment

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

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

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

Author Comment

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

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

## Featured Post

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
###### Suggested Courses
Course of the Month17 days, 4 hours left to enroll