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?
Comments
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?
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.
Just realized you have a typo in Update. Can you see if that's the issue?
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
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