Powershell Error with Register-ClusteredScheduledTask function RRS feed

  • Question

  • Hello !

    I'm facing an issue with the custom script i'm writing, it basically create a scheduled action, then create a trigger, then create a registered clustered task as wrote in the documentation here https://docs.microsoft.com/en-us/powershell/module/scheduledtasks/Register-ClusteredScheduledTask?view=win10-ps#parameters

    I wrote 3 functions to automate action creation, trigger creation and register the task.

    Here's my 3 functions code :

    function CreateAction {
            [Parameter(Mandatory = $true, Position = 0)]$script,
            [Parameter(Mandatory = $false, Position = 1)]$exec_dir
        $action1 = "New-ScheduledTaskAction -Execute $script"
        if ($exec_dir) { $action1 = $action1 + " -WorkingDirectory " + $exec_dir }
        Invoke-Expression $action1   
    function CreateTrigger {
            [Parameter(Mandatory = $true, Position = 0)]$freq,
            [Parameter(Mandatory = $true, Position = 1)]$starttime
        $trigger = 'New-ScheduledTaskTrigger ' + $freq + ' -At ' + $starttime
        Invoke-Expression $trigger  
    function CreateTask {
            [Parameter(Mandatory = $true, Position = 0)]$taskname,
            [Parameter(Mandatory = $true, Position = 1)]$action,
            [Parameter(Mandatory = $true, Position = 2)]$trigger,
            [Parameter(Mandatory = $true, Position = 3)]$resource_name           
        $task = "Register-ClusteredScheduledTask -TaskName $taskname -TaskType ResourceSpecific -Action  $action  -Trigger $trigger -Resource $resource_name"
        Invoke-Expression $task | Out-Null

    Here's how i use them :

    $ActionCopylog = CreateAction $task_script_copylog $task_dir_copylog
    $TriggerCopylog = CreateTrigger $trigger_freq $task_starttime_copylog
    $TaskCopylog = CreateTask $task_name_copy_log $ActionCopylog $TriggerCopylog $base_SID

    2 of my 3 functions work well (CreateAction and CreateTrigger) BUT i always have the same error while the CreateTask function is used :

    Register-ClusteredScheduledTask : Cannot bind argument to parameter 'Action', because PSTypeNames of the argument do not match the PSTypeName required by the parameter: 
    At line:1 char:95
    + ... pyLog -TaskType ResourceSpecific -Action  MSFT_TaskExecAction  -Trigg ...
    +                                               ~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Register-ClusteredScheduledTask], ParameterBindingArgumentTransformationException
        + FullyQualifiedErrorId : MismatchedPSTypeName,Register-ClusteredScheduledTask

    When i do a getType() of my 3 variables, i get the same type as the 3 in the Microsoft examples :

    PS C:\Users\xxx\Desktop> $Action.GetType()
    IsPublic IsSerial Name                                     BaseType                                                                  
    -------- -------- ----                                     --------                                                                  
    True     True     CimInstance                              System.Object                                                             
    PS C:\Users\xxx\Desktop> $ActionCopyLog.GetType()
    IsPublic IsSerial Name                                     BaseType                                                                  
    -------- -------- ----                                     --------                                                                  
    True     True     CimInstance                              System.Object                                                             
    PS C:\Users\xxx\Desktop> $Trigger.GetType()
    IsPublic IsSerial Name                                     BaseType                                                                  
    -------- -------- ----                                     --------                                                                  
    True     True     CimInstance                              System.Object                                                             
    PS C:\Users\xxx\Desktop> $TriggerCopylog.GetType()
    IsPublic IsSerial Name                                     BaseType                                                                  
    -------- -------- ----                                     --------                                                                  
    True     True     CimInstance                              System.Object                                                             

    Any idea what missing ?

    Thanks !


    Wednesday, September 4, 2019 9:37 AM

All replies

  • There is no point in wrapping a simple function in a function. Eliminate the functions and do this correctly and your issue will be more obvious.


    Wednesday, September 4, 2019 11:34 AM
  • Hi,

    In fact I have to keep a function for the trigger and action creation because i have different cases to manage.

    Do you think i can keep createaction and createtrigger function but eliminate create task function and use it "normally" as you mean ?

    Wednesday, September 4, 2019 12:04 PM
  • Do it without functions until you have working code.

    Do not try to use "Invoke-Expression. It serves no purpose and can introduce errors.

    New users with no coding experience tend to create wrapper functions when they should not be used.  Don't make that mistake.


    Wednesday, September 4, 2019 12:17 PM
  • Your functions are also a bit wacky as far as code is concerned.


    function CreateAction {
        Param    (
            [Parameter(Mandatory = $true)]
        if ($exec_dir) {
            New-ScheduledTaskAction -Execute $script -WorkingDirectory $exec_dir
            New-ScheduledTaskAction -Execute $script
    Your approach just doesn't make sense.


    Wednesday, September 4, 2019 12:21 PM
  • Yes you right, i'm gonna give it an overhaul to ease them.

    I made a mistake while trying to put a function result into a variable whereas the function itself give me the expected result.. i have to carefully watch my code again and again.

    I'm learning PS by myself so I make mistakes, like everyone who's learning new language or technology.

    Thanks for your advices, i'll ease my script :)

    Wednesday, September 4, 2019 12:34 PM
  • It is impossible to learn any technology by guessing and copting things.  You must first take time to formally learn the basics.


    Wednesday, September 4, 2019 12:37 PM
  • For sure, but does most company don't let their employees learn but work and produce , no matter the quality.. but it's another discussion ;)

    I've just ease my whole script and I encounter an issue, i think it's basics but i can't found any solution.

    If i do :

    $freq_task = "-Daily"

    $starttime = "6am"

    CreateAction $freq_task $starttime

    New-ScheduledTaskTrigger : Cannot process argument transformation on parameter 'Once'. Cannot convert value "System.String" to type
    "System.Management.Automation.SwitchParameter". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1
    or 0.
    At line:16 char:34
    +         New-ScheduledTaskTrigger $freq -At $starttime
    +                                  ~~~~~
        + CategoryInfo          : InvalidData: (:) [New-ScheduledTaskTrigger], ParameterBindingArgumentTransformationException
        + FullyQualifiedErrorId : ParameterArgumentTransformationError,New-ScheduledTaskTrigger

    I've tried to pass parameters through $myvariable to create a command like "New-ScheduledTaskTrigger -Daily or -Weekly) but i think it's the wrong way...

    Wednesday, September 4, 2019 1:21 PM
  • Please skip the fuctions and learn to use the commands directly. 

    We cannot compensate for your company issues.  You must learn the basics.  We cannot write or design your code for you.

    Always start by rading the full help for any command you are trying to su:

    help new-ScheduledTaskTrigger -online


    Wednesday, September 4, 2019 1:37 PM
  • As I said i have to manage different cases and for me function is the easiest way to do it.

    I'm able to run commands directly one by one and it works, that's why I'd like to script it to re-use it simply on different servers.

    I didn't ask you to write my code or design it for me, i'm trying to learn it by myself to ease some tasks of my daily work because i'm not a 10 years old full time developper.

    So if asking help in this forum is not allowed because most of people aren't high-skilled as you are and haven't the same facilities as you have so yeah i let it down. Please be aware that everybody have been beginners and learn quickly or slowly.

    A bon entendeur...

    Wednesday, September 4, 2019 1:59 PM
  • You still need to learn how to use the base commands before trying to turn them into functions.

    I am trying to show you how to do this correctly.  If you insist on stubbornly doing things that make no sense then you will have to figure it out on your own.  If you can't take direction then I cannot help you.


    Wednesday, September 4, 2019 2:12 PM