none
Dont want an error to show that file/folder exists RRS feed

  • Question

  • Hello I have a script that my company uses to perform scheduled task within our software. The script worked in everything but Server 2016. I added this code to the front of the script per other programmers suggestion.

    $scheduleObject = New-Object -ComObject schedule.service

        $scheduleObject.connect()

        $rootFolder = $scheduleObject.GetFolder("\")

        $rootFolder.CreateFolder("PROFITsystems_Tasks")

    It worked great but now on all older versions of Windows I get this error.

    Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7)
    At C:\Scripting\RV23_Scheduled_Tasks.ps1:13 char:5
    +     $rootFolder.CreateFolder("PROFITsystems_Tasks")
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

    The script still runs and does what it is supposed to do but the error freaks the customers out. Even though the script still runs its course. 

    Any Suggestions!

    Thanks

    Monday, May 8, 2017 8:44 PM

Answers

  • # note that folder does not exist
    PS>Get-ScheduledTask -TaskPath \
    
    TaskPath                                       TaskName                          State
    --------                                       --------                          -----
    \                                              Adobe Acrobat Update Task         Ready
    \                                              HPCeeScheduleForjvierra           Ready
    \                                              MyTestTask5                       Ready
    \                                              OneDrive Standalone Update Tas... Ready
    \                                              testme                            Ready
    \                                              User_Feed_Synchronization-{897... Ready
    \                                              YCMServiceAgent                   Disabled
    \                                              {58AE58B2-B106-4B83-987F-25EF3... Ready
    
    #create a task and folder
    PS>Register-ScheduledTask -TaskName test123 -TaskPath \testpath -Action $action -Trigger $t
    
    TaskPath                                       TaskName                          State
    --------                                       --------                          -----
    \testpath\                                     test123                           Ready
    # note that the task and folder are both created


    \_(ツ)_/

    Monday, May 8, 2017 10:16 PM

All replies

  • You cannot create a folder that already exists.

    You can create a task and give it a ath and the path will be created if it doesn't exist.  This is true on all current versions of Windows.

    Register-ScheduledTask -TaskName mytask -TaskPath \PROFITsystems_Tasks ...


    \_(ツ)_/

    Monday, May 8, 2017 8:55 PM
  • I know it wont recreate whats already there. It of course doesn't error out when creating new but sometimes we need to append the current selection with newer software and tasks or just add to the existing ones. The script allows for up to 7 different tasks to be created and sometimes the customers opt for 1 at the beginning but then request the others later on then error will arise. Like I said it still writes the tasks but gives the error and was hoping there was a way to make the error not show.

    Monday, May 8, 2017 9:13 PM
  • The code I posted does not error if the folder already exists and it creates the folder if it doesn't exist.

    help Register-ScheduledTask  -full


    \_(ツ)_/


    • Edited by jrv Monday, May 8, 2017 10:09 PM
    Monday, May 8, 2017 10:08 PM
  • # note that folder does not exist
    PS>Get-ScheduledTask -TaskPath \
    
    TaskPath                                       TaskName                          State
    --------                                       --------                          -----
    \                                              Adobe Acrobat Update Task         Ready
    \                                              HPCeeScheduleForjvierra           Ready
    \                                              MyTestTask5                       Ready
    \                                              OneDrive Standalone Update Tas... Ready
    \                                              testme                            Ready
    \                                              User_Feed_Synchronization-{897... Ready
    \                                              YCMServiceAgent                   Disabled
    \                                              {58AE58B2-B106-4B83-987F-25EF3... Ready
    
    #create a task and folder
    PS>Register-ScheduledTask -TaskName test123 -TaskPath \testpath -Action $action -Trigger $t
    
    TaskPath                                       TaskName                          State
    --------                                       --------                          -----
    \testpath\                                     test123                           Ready
    # note that the task and folder are both created


    \_(ツ)_/

    Monday, May 8, 2017 10:16 PM