locked
Script runs when input manually, but will not run in task scheduler RRS feed

  • Question

  • Hello,

    I have created an incremental backup that when input manually into powershell it works perfectly.  But when put into task scheduler and ran, it shows that it executes for one second successfully.  Here is the what the script is:

    $Source = 'C:\'
    $Destination = 'Q:\'
    $CopyFiles = Get-ChildItem $Source -Force -Recurse -File

    ForEach ($CopyFile in $CopyFiles) {
        $FileDestination = Get-ChildItem $($CopyFile.Fullname -replace [regex]::Escape($Source),$Destination) -ErrorAction SilentlyContinue

        If ($CopyFile.LastWriteTime -ne $FileDestination.LastWriteTime) {
                $FileDestination = $CopyFile.Fullname -replace [regex]::Escape($Source),$Destination
                $Directory = Split-path -Path $FileDestination
                If ( !(Test-Path -path $Directory -ErrorAction SilentlyContinue) ) {
                            New-Item -Path $Directory -Type Directory -Verbose
                }
                Copy-Item $CopyFile.FullName -Destination $FileDestination -Verbose
        }
    }

    I am at a loss as to what to do.  I have edited the actions of "Start In (Optional)" and put the path to the script (it's just C:\Users\Administrator\Desktop\Testbatch.bat).  Cmd.exe flickers for a second when the .bat is executed and does not stay up.  Appreciate any help on the matter, thank you. 

    Thursday, April 19, 2018 1:44 AM

All replies

  • To add, I have Action: Start a program and that is also to the same path stated above to the testbatch.bat.  
    Thursday, April 19, 2018 1:45 AM
  • Permissions can always be an issue. Make sure the task user has permissions to access to the resources. Also note that mapped drive would not exist when no one is logged on so try using UNC path.

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.


    Thursday, April 19, 2018 2:14 AM
  • I gave it full permissions and it still doesn't run it.  Cmd.exe for the bat file just pops up for a second and goes away.  I have another .bat file where the cmd.exe pops up and shows the command being executed, but that is not happening with this .bat file.
    Thursday, April 19, 2018 6:04 AM
  • I gave it full permissions and it still doesn't run it.  Cmd.exe for the bat file just pops up for a second and goes away.  I have another .bat file where the cmd.exe pops up and shows the command being executed, but that is not happening with this .bat file.

    I'd check if the destination happens to be a network location. For scripting issues I'd ask for help over here.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=scripting

    https://social.technet.microsoft.com/Forums/scriptcenter/en-US/home?forum=ITCG

     

     



    Regards, Dave Patrick ....
    Microsoft Certified Professional
    Microsoft MVP [Windows Server] Datacenter Management

    Disclaimer: This posting is provided "AS IS" with no warranties or guarantees, and confers no rights.

    Thursday, April 19, 2018 11:19 AM