Trouble Scheduling Browser Window to Open

Tagged: 

This topic contains 6 replies, has 2 voices, and was last updated by Profile photo of Mark Noble Mark Noble 2 years, 3 months ago.

  • Author
    Posts
  • #17967
    Profile photo of Mark Noble
    Mark Noble
    Participant

    I tried to use PowerShell to launch a website every weekday... but it's not working as I expected. What am I missing?

    $trigger = New-JobTrigger -Weekly -DaysOfWeek Monday, Tuesday, Wednesday, Thursday, Friday -At "9:30 AM"
    Register-ScheduledJob -Name LaunchSite -Trigger $trigger -ScriptBlock {start iexplore URL}
    

    Nothing seems to happen, but when I do a Get-Job it says that the status is "Completed".

    I'd like it to open in the full-screen version of IE... but for now I'd settle for it working at all.

    This is in PS4 on Win 8.1.

  • #17968
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Looks like the Register-ScheduledJob creates scheduled tasks which run in the background (and has no option to specify otherwise.) You'll have to modify the scheduled task after Register-ScheduledJob is finished:

    $trigger = New-JobTrigger -Weekly -DaysOfWeek Monday, Tuesday, Wednesday, Thursday, Friday -At "9:30 AM"
    Register-ScheduledJob -Name LaunchSite -Trigger $trigger -ScriptBlock { start iexplore URL }
    
    $principal = New-ScheduledTaskPrincipal -LogonType Interactive -UserId $env:USERDOMAIN\$env:USERNAME
    Set-ScheduledTask -TaskName LaunchSite -TaskPath Microsoft\Windows\PowerShell\ScheduledJobs -Principal $principal
    

    This changes the task's options to "Run only when user is logged on", causing whatever programs it runs to show up on your interactive desktop.

    Edit: On a side note, the ScheduledTasks module is only avaialble in Windows 8 and later (which you have.) If you wanted this to work on older operating systems, you'd have to find a different way to modify the task. schtasks.exe /change /IT can do it, but unfortunately requires you to provide a password for the task as well. There may be other methods using WMI or the Win32 API, but I haven't looked into that since you mentioned Windows 8.1 anyway.

  • #17995
    Profile photo of Mark Noble
    Mark Noble
    Participant

    I added the two lines you mentioned to make it a non-background task and made sure it was scheduled properly.

    At 9:30a as scheduled, a window appeared – then abruptly closed before it had a chance to load the site even... It was just kind of a dark blue window – probably a browser as specified in the script.

    Any other ideas for how to make the window persist long enough for me to interact with it and close it on my own? 🙂

  • #17997
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I saw basically the same thing when I tested that code. The dark window you mentioned is the PowerShell console, but it doesn't have to stay open very long, because all it's doing is loading up your scheduled job scriptblock and invoking it. I'm not sure if it would be any help to try to leave that PowerShell window open, as it is just being launched with these parameters:

    -NoLogo -NonInteractive -WindowStyle Hidden -Command "Import-Module PSScheduledJob; $jobDef = [Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition]::LoadFromStore('
    LaunchSite', 'C:\Users\UserName\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs'); $jobDef.Run()"
    
  • #18002
    Profile photo of Mark Noble
    Mark Noble
    Participant

    Ahh... Well, the point is that the browser window didn't launch and remain open as intended. :/

    Maybe it's ok for the code to execute in the background without being visible at all – as long as the browser window is visible and remains that way.

  • #18004
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Those two things are tied together. If powershell.exe launches in the background, so does IE.

    I'm not sure why the browser isn't opening for you; that part worked fine for me. I tested the code before I posted it here. The only thing I changed was [b]URL[/b] to [b]http://www.google.com[/b]

  • #18136
    Profile photo of Mark Noble
    Mark Noble
    Participant

    Just a follow-up. I pasted the code from your reply (#17968) and now it's working fine.

    When I get a chance I need to figure out how my attempt to follow your lead resulted in a different output from when I cut and pasted your code. :/

You must be logged in to reply to this topic.