Problem with $trigger

Tagged: 

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 3 months, 1 week ago.

  • Author
    Posts
  • #64185
    Profile photo of test
    test
    Participant

    Hi there
    I got a script, where I can create normally a Task.

    $psexe = Join-Path $PSHOME powershell.exe
    $action = New-ScheduledTaskAction -Execute $psexe -Argument '-command  "Get-ChildItem | Out-File tasktest.txt"' #$action = New-ScheduledTaskAction -Execute $psexe -Argument "-file d:\test\myscript.ps1"
    $trigger = New-ScheduledTaskTrigger -AT '13:00' -Weekly -DaysOfWeek 1, 2, 3
    $task = New-ScheduledTask -Action
    $action -Trigger $trigger
    $task | Register-ScheduledTask -TaskName MyTestTaskas
    Get-ScheduledTask MyTestTaskas

    Now there is a problem with the variable trigger. 
    In my previous script the user can choose between monthly, daily and weekly. He can also choose between the week days. Now I want that the wished Option of the user will be executed.
    I did the script like this but it does not work.
    if ($time -eq "daily") {

    $taskName = Read-Host "Name"
    $user = "username"
    $password = "username"
    $action = New-ScheduledTaskAction -Execute "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-command path-to-powershell-script"
    $trigger = New-ScheduledTaskTrigger -Daily -AT $clock -DaysOfWeek $day
    $settings = New-ScheduledTaskSettingsSet
    $inputObject = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings
    Register-ScheduledTask -TaskName $taskName -InputObject $inputObject -User $user -Password $password }

    elseif ($time -eq "weekly") {

    $taskName = Read-Host "Name"
    $user = "username"
    $password = "username"
    $action = New-ScheduledTaskAction -Execute "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-command path-to-powershell-script"
    $trigger = New-ScheduledTaskTrigger -Weekly -AT $clock -DaysOfWeek $day
    $settings = New-ScheduledTaskSettingsSet
    $inputObject = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings
    Register-ScheduledTask -TaskName $taskName -InputObject $inputObject -User $user -Password $password }

    elseif ($time -eq "monthly") {

    $taskName = Read-Host "Name"
    $user = "username"
    $password = "username"
    $action = New-ScheduledTaskAction -Execute "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-command path-to-powershell-script"
    $trigger = New-ScheduledTaskTrigger -Monthly -AT $clock -DaysOfWeek $day
    $settings = New-ScheduledTaskSettingsSet
    $inputObject = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings
    Register-ScheduledTask -TaskName $taskName -InputObject $inputObject -User $user -Password $password }
    What do I have to change so that the Option weekly, monthly and daily works as wished.
    Regards

  • #64194
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Where are $Clock and $Day defined?

    New-ScheduledTaskTrigger -Monthly -AT $clock -DaysOfWeek $day

  • #64200
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    I also don't see where you define $time. Take a look at the format of this script so you're not copying and pasting the same code over and over:

    $taskName = Read-Host "Name"
    $user = "username"
    $password = "username"
    
    $actionParams = @{
        Execute  = "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe" 
        Argument = "-command path-to-powershell-script"
    }
    
    $trigParams = @{
        AT = $clock 
        DaysOfWeek = $day
    }
    
    switch ($time) {
        "daily"   { $trigParams.Add("Daily", $true) }
        "weekly"  { $trigParams.Add("Weekly", $true) }
        "monthly" { $trigParams.Add("Monthly", $true) }
    }
    
    $taskParams = @{
        Action   = New-ScheduledTaskAction @actionParams 
        Trigger  = New-ScheduledTaskTrigger @trigParams 
        Settings = New-ScheduledTaskSettingsSet
    }
    
    $regTaskParams = @{
        TaskName    = $taskName 
        InputObject = New-ScheduledTask $taskParams
        User        = $user 
        Password    = $password
    }
    
    Register-ScheduledTask @regTaskParams
    

You must be logged in to reply to this topic.