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

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
milesriAuthor Commented:
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
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
This does not answer my question. Do you use mstsc /admin for connecting, to get the admin console?
What were the top attacks of Q1 2018?

The Threat Lab team analyzes data from WatchGuard’s Firebox Feed, internal and partner threat intelligence, and a research honeynet, to provide insightful analysis about the top threats on the Internet. Check out our Q1 2018 report for smart, practical security advice today!

milesriAuthor Commented:
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?
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
milesriAuthor Commented:
So how does it run on a reboot when no one has logged?
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2003

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.