Powershell can be used to automate tasks. Where you run three-four powershell files one after one and need to run the first script through task scheduler. Task goes into running state and not coming back to ready state. When you see the error it says one of its intsance is already running.
I came to this situation when I was trying to run a sequence of multiple ps1 files one after one. I had created a batch file to run first script. It will check certain program status, if its not running it just restart the ps1 script sequence from beginning. In batch file I had only mention the single ps1 script file path and other scripts are triggered after every script. The task scheduler was stuck to Running state where it was not able to exit the batch file window. Thus it would not proceed the next script. Then I started searching on how to get rid of running state after it runs the batch file.
I do not know how you understand my scenario. But the question is How to exit the 'Running' state after running the batch file. You will not believe, you just need to type Start before Powershell script command inside batch file. It all worked well after changing the command. Task scheduler never went to Running after this change. The Batchfile commands were looking like below in the end.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
CD "C:\Program Files\ProgramFolder"
TASKLIST | FINDSTR ProgramName > NUL 2> NUL
IF %ERRORLEVEL%==1 (
taskkill /F /IM Powershell.exe
Start PowerShell.exe -NoExit -file "c:\XXX.ps1"
)
endlocal
Exit
You may find this helpful with your complex scripting.
I came to this situation when I was trying to run a sequence of multiple ps1 files one after one. I had created a batch file to run first script. It will check certain program status, if its not running it just restart the ps1 script sequence from beginning. In batch file I had only mention the single ps1 script file path and other scripts are triggered after every script. The task scheduler was stuck to Running state where it was not able to exit the batch file window. Thus it would not proceed the next script. Then I started searching on how to get rid of running state after it runs the batch file.
I do not know how you understand my scenario. But the question is How to exit the 'Running' state after running the batch file. You will not believe, you just need to type Start before Powershell script command inside batch file. It all worked well after changing the command. Task scheduler never went to Running after this change. The Batchfile commands were looking like below in the end.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
CD "C:\Program Files\ProgramFolder"
TASKLIST | FINDSTR ProgramName > NUL 2> NUL
IF %ERRORLEVEL%==1 (
taskkill /F /IM Powershell.exe
Start PowerShell.exe -NoExit -file "c:\XXX.ps1"
)
endlocal
Exit
You may find this helpful with your complex scripting.
No comments:
Post a Comment