Author Posts

April 18, 2018 at 4:23 pm

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

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.

April 18, 2018 at 6:43 pm

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?

April 18, 2018 at 6:47 pm

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