Reputation: 579
I am running in debug mode in ISE. I am starting a separate powershell script from another secondary script using start-process and specifying it's -file parameter from the primary script. I have a timer in the primary script where I am trying to execute some other code.
Please consider the following:
$timer = new-object timers.timer
$timerID = [guid]::NewGuid()
$timer.Interval = 10000
$complete = 0
$action = {
write-host "Complete Ticker: $complete"
if($complete -eq 1)
{
write-host "Completed"
$timer.stop()
Unregister-Event $timerID
}
}
Register-ObjectEvent -InputObject $timer -EventName elapsed –SourceIdentifier $timerID -Action $action
$timer.start()
$shellScript = "-file C:\Test.ps1"
$powershellArguments = $shellScript
Start-Process "powershell.exe" -Wait -ArgumentList $powershellArguments
$complete = 1
The other script is called successfully, and displays, however the timer does not appear to tick unless the script window started with the start-process is closed. The expected result is in that the interval specified that a breakpoint could be set in the action and hit while the secondary script is running.
Is there something perhaps wrong with the above code?
Upvotes: 1
Views: 1048
Reputation: 3451
The start-process
that starts PS uses the -Wait
parameter. That prevents $action
from running.
I'm not sure what the code is trying to accomplish via the timer and $action
. What it would do, though (if not for -Wait
), is write "Complete Ticker: 0" every ten seconds until the invoked PS completes when it will write "Complete Ticker: 1".
If the invoked PS runs longer than 10 seconds * height_of_the_parent_PS_console_in_lines, you won't really be able to see much in the way of output - the screen will be full of "Complete Ticker: 0" messages and you'd have to watch closely to see the screen kind of flicker every 10 seconds. Even if the invoked PS runs more quickly than that, the output doesn't tell how long the invoked PS will run or even that it's making progress.
In essence it's kind of like having the user watch der blinkenlights - interesting to look at possibly, but not too informative.
But if that's what the timer code is supposed to do it can be replaced by a simple loop after the start-process
that checks if the invoked PS has completed and if not outputs the "Complete Ticker: 0" message.
Use the -passthru
argument to start-process
and then check the status of via the returned process object in the loop.
Upvotes: 1