locked
PowerShell Eventing without console window open RRS feed

  • Question

  • Hello all,

    I am trying to use PowerShell eventing to refresh a webpage every so often, and I thought using PowerShell eventing with a Timer would be a good way to accomplish this. However, it appears I have to keep the PowerShell console window open for the script to work. Is it possible for this script to work without the console window open, i.e. to run as a background task?

    cls
    
    $Shell = New-Object -ComObject Shell.application
    
    #First, let's practice by creating a timer object
    $Timer = New-Object system.timers.timer
    
    Register-ObjectEvent -InputObject $Timer -EventName Elapsed -MaxTriggerCount 5 -Action {
        
        #The output from a triggered event action is discarded, so we need to use write-host to capture it
        write-host "Refreshing IE window..." -NoNewline
    
        #Refresh any "Google" IE windows
        $Shell.Windows() | ?{$_.document.url -like "*Google*"} | %{$_.refresh()}
    
        write-host "Done" -ForegroundColor Green
    }
    
    $Timer.Interval = 1000
    
    $Timer.AutoReset = $true
    
    $Timer.Enabled = $true

    Thursday, October 13, 2016 9:06 PM

Answers

  • You need to keep the script running or the eventing will stop.

    Place this at the end of your script:

    while(1){sleep -m 100}


    \_(ツ)_/

    • Proposed as answer by Hello_2018 Tuesday, November 8, 2016 6:23 AM
    • Marked as answer by Hello_2018 Tuesday, November 8, 2016 6:23 AM
    Thursday, October 20, 2016 5:34 PM

All replies

  • No.  PowerShell must be running for a script to run.

    You can place this at t he bottom of your script so it can be launched from a shortcut and minimized.

    while(1){sleep -m 100}


    \_(ツ)_/

    Thursday, October 13, 2016 9:37 PM
  • Thursday, October 13, 2016 9:44 PM
  • JRV,

    Well, I try not to download anything from download.cnet.com... but thank you for the link! :)

    And I think you misunderstand my question, as I'm not asking about running a PowerShell script without PowerShell, just without the PowerShell window open (like as a background process).

    In any case, I can get this to work with Do\Until loops and run the script with Batch and VBS files to have it run in the background as a process without any console window, but I just wondered if this was possible using the Eventing approach above, i.e. can PowerShell Eventing be run in the background as a process without having to have the console window open (PowerShell.exe is running as a process, but no console window is open)?

    [Side Note]

    This actually brings up a larger question, that for any Eventing script, do you have to have the PowerShell window open? Currently, I cannot get an Eventing script to work as a Scheduled Task, and I would think a programmer would want to be able to use Eventing without having to have a console window open? This question may be out of the scope of this post, but I just wanted to throw it out there.

    [\Side Note]

    • Marked as answer by KentYeabower Thursday, October 20, 2016 5:15 PM
    • Unmarked as answer by KentYeabower Thursday, October 20, 2016 5:15 PM
    Thursday, October 13, 2016 9:55 PM
  • You need to keep the script running or the eventing will stop.

    Place this at the end of your script:

    while(1){sleep -m 100}


    \_(ツ)_/

    • Proposed as answer by Hello_2018 Tuesday, November 8, 2016 6:23 AM
    • Marked as answer by Hello_2018 Tuesday, November 8, 2016 6:23 AM
    Thursday, October 20, 2016 5:34 PM