1

In Task Scheduler, how may I stop a running task sooner than 1 hour, say immediately after execution?

I've tried manually entering '1 minute', but the Task is still 'running'. Suggestions?

I'm running a powershell script via Task to unlock my vault during backup tasks.. Maybe there's a means of closing its window so the task can actually End?

Task Scheduler Settings

Here's my original PS:

$SecureString = ConvertTo-SecureString "111111111111" -AsPlainText -Force
    Unlock-BitLocker -MountPoint "G:" -Password $SecureString

Here's my bat creator code...

function Convert-PowerShellToBatch
{
    param
    (
        [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [string]
        [Alias("FullName")]
        $Path
    )
 
    process
    {
        $encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $Path -Raw -Encoding UTF8)))
        $newPath = [Io.Path]::ChangeExtension($Path, ".bat")
        "@echo off`npowershell.exe -NoExit -encodedCommand $encoded" | Set-Content -Path $newPath -Encoding Ascii
    }
}
 
Get-ChildItem -Path D:\Scripts\vault-unlock.ps1 |
  Convert-PowerShellToBatch

1 Answer 1

0

How to stop a running powershell script task?

ScheduledTask | ? TaskName -eq 'TStop-On' | Stop-ScheduledTask

or

Get-ScheduledTask | Where {$_.State -eq 'Running' -and $_.TaskName -eq 'TStop-On'} | Stop-ScheduledTask

Replace TStop-On with your task name tobe closed.
To check it:
ScheduledTask
(ScheduledTask).Taskname
(Get-ScheduledTask|? State -eq 'Running').Taskname

More info:

5
  • Thank you; a couple follow-up questions... 1) Can I place 'Stop-ScheduledTask -TaskName "ScanSoftware"' in my original PS1, or how else to execute it? 2) Wjen I run 'Get-ScheduledTask', even though Task Scheduler shows 'Running', 'Get-ScheduledTask' shows 'Ready'... what's going on here?
    – Scott
    Commented May 21 at 19:32
  • perhaps you can edit the question and include your powershell script
    – Mr.Key7
    Commented May 21 at 19:40
  • Added. Thanks for the suggestion.
    – Scott
    Commented May 21 at 19:48
  • Perhaps you can try to enable stop task if it runs longer than, and the value like 1 minutes in Triggers tab.
    – Mr.Key7
    Commented May 22 at 8:23
  • I'm thinking the answer is that the Task isn't really still running even though indicated by such in the Task Scheduler, as 'Get-ScheduledTask' indicates 'Ready' not 'Running'. I'll check this over the next week and mark this a solution if still functioning the same. Thank you again for your input.
    – Scott
    Commented May 22 at 12:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .