ScheduledJob

This topic contains 2 replies, has 2 voices, and was last updated by  Codernator 3 days, 9 hours ago.

  • Author
    Posts
  • #104494

    Codernator
    Participant

    Hi guys,
    I have a powershell script that updates data in an excel workbook.
    it runs fine if I executed from Windows PowerShell.
    I tried to put it in a scheduledJob and it's throwing off an exception.
    Here is the script for setting up my scheduledjob

       $Trigger = New-JobTrigger -Once -At 6:00PM
       $JobName = "UpdateData"
       Register-ScheduledJob -Name $JobName -Trigger $Trigger -ScriptBlock {& 'C:\Temp\UpdateData.ps1'}
       Get-ScheduledJob -Name $JobName | Set-ScheduledJob -RunNow
    

    here is the error that I'm catching in my error log

        There was an error in this command Update-Data.ps1 The script is located C:\Temp\Update-Data.ps1
        Here is the error message: Error On 07/13/2018 18:38:20; Unable to get the Open property of the Workbooks class - Line  
        Number: 649
    
        # Line Number 649 in the script is the following line.
        $Workbook = $Excel.Workbooks.Open($FileFullName)
    

    Any thoughts?

    Thanks

  • #104503

    postanote
    Participant

    Jobs don't display interactive GUI / apps. That is not their purpose. Background non-interactive stuff is the target for this.
    Try it. Simplify your test and use a simple script that pops a dialogbox or just start any office app, meaning just opening it.
    The job will run, but the dialog will never display, or the office app will start (you'll see that in the Task Manager) but not display. So, there's not interacting with it in anyway. Hence leading to errors.

    $Trigger = New-JobTrigger -Once -At 8:38PM
    $JobName = "UpdateData"
    Register-ScheduledJob -Name $JobName -Trigger $Trigger -ScriptBlock {& 'D:\Scripts\Test.ps1'}
    
    Id         Name            JobTriggers     Command                                  Enabled
    --         ----            -----------     -------                                  -------
    5          UpdateData      1               & 'D:\Scripts\Test.ps1'                  True
    
    
    Get-ScheduledJob -Name $JobName | Set-ScheduledJob -RunNow
    Get-Job
    
    Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
    --     ----            -------------   -----         -----------     --------             -------
    3      UpdateData      PSScheduledJob  Completed     True            localhost            & 'D:\Scripts\Test.ps1'
    
    
    Get-Job
    
    Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
    --     ----            -------------   -----         -----------     --------             -------
    3      UpdateData      PSScheduledJob  Completed     True            localhost            & 'D:\Scripts\Test.ps1'
    4      UpdateData      PSScheduledJob  Completed     True            localhost            & 'D:\Scripts\Test.ps1'
    
    
    Unregister-ScheduledJob -Name $JobName
    
    • #104546

      Codernator
      Participant

      Thank you.
      in this case, I will revert back to task scheduler

      Thanks

You must be logged in to reply to this topic.