Question : PowerShell task will not run when logged off

Hi,

I've got the following PowerShell script:

$dtStart = Get-Date
Add-Content c:\log.txt ("Started: " + $dtStart)

I have it setup to run as a scheduled task.  Here's the scheduled task command being executed:

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe c:\test.ps1

It runs just fine when I'm logged in.  When I logoff the machine, the task stops running (I can tell because lines cease to be added to c:\log.txt).  As soon as I log back in, lines start getting added again.

I've got some vbscript code scheduled tasks that aren't working correctly, and I was going to try and port them to powershell.  Here's the vbscript issue I'm having, in case there is any relevant information you can glean from this discussion.
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/2003_Server/Q_23986503.html

Answer : PowerShell task will not run when logged off

I omitted one bit, right before the last paragraph--I never said what "this method" is that I had some partial success with.  Here's what it should have been...

....

That's based on what I found here:
http://www.microsoft.com/technet/scriptcenter/resources/pstips/nov07/pstip1130.mspx


Now what I wound up doing is checking the $objIE.ReadyState, and if it's not in the valid range (0-4), I then try to take control of the existing IE process, and if that fails, then I try to kill any existing iexplore.exe processes and spawn a new instance if IE.  Here's the code for that:

if(($objIE.ReadyState -ne 0) -AND ($objIE.ReadyState -ne 1) -AND ($objIE.ReadyState -ne 2) -AND ($objIE.ReadyState -ne 3) -AND ($objIE.ReadyState -ne 4))
{
   $objIE = [System.Runtime.InteropServices.Marshal]::GetActiveObject("InternetExplorer.Application")
   if(($objIE.ReadyState -ne 0) -AND ($objIE.ReadyState -ne 1) -AND ($objIE.ReadyState -ne 2) -AND ($objIE.ReadyState -ne 3) -AND ($objIE.ReadyState -ne 4))
   {
       Stop-Process -processname iexplore
       $objIE = New-Object -ComObject "InternetExplorer.Application"
   }
}


Now, I found that with this method, the task ran successfully for several days, but then hung at "while($objIE.Busy)" loop.  It seems like it got stuck in that loop and never got any further.  It's like the page never finished loading--even after 10 hours had gone by.  Seems like by that time it would have timed out, at the very least...


Random Solutions  
 
programming4us programming4us