Important Notice: On February 29th, this community was put into read-only mode. All existing posts will remain but customers are unable to add new posts or comment on existing. Please feel to join our Community Discord for any questions and discussions.

Powershell script to remove Scheduled Task fails when deployed but works on the local device

I am using a short PS script to remove a Scheduled Task. 

It works fine from the Powershell windows on a test workstation, but fails everytime when I try to deploy the exact same command.

Unregister-ScheduledTask -Taskname "MPI Upate" -Confirm:$false

Error output in Deploy is:

Unregister-ScheduledTask : No MSFT_ScheduledTask objects found with property 'TaskName' equal to 'MPI Upate'.  Verify 
the value of the property and retry.
At C:\WINDOWS\AdminArsenal\PDQDeployRunner\service-1\exec\user.ps1:1 char:1
+ Unregister-ScheduledTask -Taskname "MPI Upate" -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (MPI Upate:String) [Unregister-ScheduledTask], CimJobException
    + FullyQualifiedErrorId : CmdletizationQuery_NotFound_TaskName,Unregister-ScheduledTask
 
Remove-Item : A positional parameter cannot be found that accepts argument '-'.
At C:\WINDOWS\AdminArsenal\PDQDeployRunner\service-1\exec\user.ps1:2 char:1
+ Remove-Item "c:\MPI" - Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-Item], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
 

The task name is EXACTLY the same and I have literally copied/pasted the failing command from the package to the workstation PS command window and it works.

What am I missing?

0

Comments

5 comments
Date Votes
  • I have tried both methods.. imbedding the command directly in the Powershell step window, and created a .ps1 script in my Repository and pointed the PS step to it using the Insert PowerShell Script option. Note I have also tried " " and ' ' and "' '" around the task name to to avail. the task is on the root of the scheduler library, so no need to specify the path.

    1
  • I made a task called "MPI Update" and was able to run your command over Inventory and Deploy and both worked. Can you find the task over PDQ? Maybe you can get the task and pipe it into unregister. 

    Get-ScheduledTask -TaskName "*MPI*" | Unregister-ScheduledTask -Confirm:$false

    1
  • Are you calling a PS1 file or is the script input directly into PDQ? If the script is directly in PDQ, does it run if you reference a script file instead?

    0
  • Just realized you have a typo in Update. Can you see if that's the issue?

    0
  • Nope, corrected that and still the same error output:

    Unregister-ScheduledTask : No MSFT_ScheduledTask objects found with property 'TaskName' equal to 'MPI Update'.  Verify 
    the value of the property and retry.
    At C:\WINDOWS\AdminArsenal\PDQDeployRunner\service-1\exec\user.ps1:1 char:1
    + Unregister-ScheduledTask -Taskname "MPI Update" -Confirm:$false
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (MPI Update:String) [Unregister-ScheduledTask], CimJobException
        + FullyQualifiedErrorId : CmdletizationQuery_NotFound_TaskName,Unregister-ScheduledTask
     

    0