Unregister-Scheduledtask help

This topic contains 2 replies, has 2 voices, and was last updated by  Matt 1 month ago.

  • Author
    Posts
  • #99178

    Matt
    Participant

    Having an issue unregistering my scheduled tasks on servers I have admin rights to.

    $target = "servername"
    $tasks = Invoke-Command -ComputerName $target -Scriptblock{Get-ScheduledTask -TaskName "TaskName"}
    Write-host $tasks.TaskName //I outputted this to ensure the task DOES exist on the server and can confirm it does

    $taskname = $tasks.TaskName

    if ($tasks)
    {
    Write-Host "Scheduled task found"

    $Session = New-CimSession $target

    Unregister-ScheduledTask -Taskname $taskname -CimSession $Session -Confirm $false

    }
    Else
    {
    Write-Host "scheduled task not found"
    }

    So I know the task exists, but I get this error message:

    Unregister-ScheduledTask : Servername: No matching MSFT_ScheduledTask objects found by CIM query for instances of the
    Root/Microsoft/Windows/TaskScheduler/MSFT_ScheduledTask class on the Servername CIM server: SELECT * FROM MSFT_ScheduledTask WHERE ((TaskName LIKE 'TaskName'))
    AND ((TaskPath LIKE 'False')). Verify query parameters and retry.

  • #99193

    Joel Sallow
    Participant

    I'm not all that familiar with scheduled tasks, and it's unlikely that this will actually solve the issue, but your syntax here isn't correct:

    Unregister-ScheduledTask -Taskname $taskname -CimSession $Session -Confirm $false

    That will probably not work properly with the -Confirm switch. The proper syntax is -Confirm:$false (no space, with a colon). Boolean parameters are weird.

    Anyhow, is it possible you may be overcomplicating it? Why not just do the query, test for task existing, and removal all at once inside the Invoke-Command block?

    • #99199

      Matt
      Participant

      That was it (face palm). Thanks for the second set of eyes!

You must be logged in to reply to this topic.