title: What alternative do I have to run a scheduled task
body: <p>I have hacked together a fix for running a scheduled job on my web server that has worked well enough up until now.</p>
  
  <p>I had the need to run a job every <code>x</code> minutes on my IIS 7 box. To accomplish this, I created a webpage (runcronjob.aspx) that contains all of the logic that I want to run, checks job times in the database, last run time, send emails, query active directory, etc. I then used Task Scheduler on my workstation (not the web server) to call the page I made. I no longer have a dedicated workstation that will always be on the network, so I'd like to move the scheduled task to the web server and this is where I'm getting stuck.</p>
  
  <p>The web server is a Windows 2008 x64 machine running IIS 7. I tried to create the scheduled task on the web server the same way as I did on my local machine but it does not work. The scheduled task calls <code>IEXPLORE.EXE</code> with the webpage as the parameter "http://mydomain.com/cron/runcronjob.aspx". When IE 7 launches on the web server, it prompts for a username and password. I also tried running a vbs script locally on the server:</p>
  
  <p><strong>runjob.vbs</strong></p>
  
  <pre><code>Call LogEntry()
  
  Sub LogEntry()
  
          'Force the script to finish on an error.
          On Error Resume Next
  
          'Declare variables
          Dim objRequest
          Dim URL
  
          Set objRequest = CreateObject("Microsoft.XMLHTTP")
  
          'Put together the URL link appending the Variables.
          URL = "http://mydomain.com/cron/runcronjob.aspx"
  
          'Open the HTTP request and pass the URL to the objRequest object
          objRequest.open "GET", URL , false
  
          'Send the HTML Request
          objRequest.Send
  
          'Set the object to nothing
          Set objRequest = Nothing
  
  End Sub
  </code></pre>
  
  <p>The problem that I've run into with this scenario is that I have forms authentication setup on website. So when this script is ran, it redirects to the log in page instead of running the code on runcronjob.aspx.  To get around this, I added this code to the code behind of the Page_Load secton of the login page:</p>
  
  <pre><code>If Request.ServerVariables("LOCAL_ADDR") = Request.ServerVariables("REMOTE_ADDR") Then
      FormsAuthentication.RedirectFromLoginPage("AD-ENT\accountToRunAs", False)
  End If
  </code></pre>
  
  <p>The login page executes the code, but it never redirects back to the runcronjob.aspx for that code to run.</p>
  
  <p>I'm starting to run out of ideas.</p>
  
  <p><strong>EDIT:</strong>
  The reason why I'm doing it in such a 'hacky' method is because I have very limited access to the web server and it was like pulling teeth even getting permission to let me run a scheduled task on the server.  That being said, options like creating a console app or a Windows service to run could prove to be difficult.</p>