none
Problem with a script when it launched tasks scheduler with "Run whether user is logged on or not" option RRS feed

  • Question

  • Hello,

     I have written a small powershell program which must to erase files on a folders and subfolders. It working when I launch it manually, with task scheduler too, but it not working when I Check the option « Run whether user is logged on or not ».

    With the Start-Transcrypt command I have access error on the folders ( with Run whether user is logged on or not ).

     

    When a script ( with Run whether user is logged on or not ) is launched to task scheduler, it don’t use user’s groups right ?

    Dionysoos
    • Edited by Dionysoos Tuesday, May 12, 2015 6:32 AM
    Tuesday, May 12, 2015 6:31 AM

Answers

  • I have written a small powershell program which must to erase files on a folders and subfolders. It working when I launch it manually, with task scheduler too, but it not working when I Check the option « Run whether user is logged on or not ».

    If you feel that this is a scripting problem then you should post your script.
    If you feel that this is a Task Scheduler problem you should post your question in the relevant Windows forum.

    Tuesday, May 12, 2015 7:36 AM

All replies

  • I have written a small powershell program which must to erase files on a folders and subfolders. It working when I launch it manually, with task scheduler too, but it not working when I Check the option « Run whether user is logged on or not ».

    If you feel that this is a scripting problem then you should post your script.
    If you feel that this is a Task Scheduler problem you should post your question in the relevant Windows forum.

    Tuesday, May 12, 2015 7:36 AM
  • #     INFODD 21-07-2014 Script permettant de supprimer les fichiers de plus de X jours
    # v1.0 21-07-2014 INFODD Création du script
    #-------------------------------------------------------------------------------------------------------------------------------------------------------#
    
    #-------------------------------------------------------------------------------------------------------------------------------------------------------#
    #Déclaration des varibles
    $sPathFolder = 'D:\Scan\' #Chemin du dossier à traiter
    $iNbJour ='7' #Nombre de jours des fichiers à traiter
    $dDate = Get-date #Date en cours
    
    Start-Transcript "D:\scheduled_task.log"
    
    #-------------------------------------------------------------------------------------------------------------------------------------------------------#
    #Suppression des fichiers ayant plus de x jours
    Clear-Host
    
    Write-Output "Début" | Out-File -FilePath 'C:\Temporaire\PurgeScan.log' -Encoding default
    Get-ChildItem -Path $sPathFolder -Recurse | Out-File -FilePath 'C:\Temporaire\PurgeScan.log' -Encoding default -Append
    
    Get-ChildItem -Path $sPathFolder -Recurse | ForEach-Object {
        $iNbJourFichier = $dDate - $_.LastWriteTime # Nombre de jour d'existence du fichier créé
      #  Write-Output "Fichier en cours de traitement $_.Name"
        if ( $iNbJourFichier.Days -gt $iNbJour) #Si le fichier a été modifié il y a plus de 7 jours
        {
           # Write-Output "Le fichier $_ a plus de 7 jours"
            if ( ($_.Attributes -ne 'Directory') -and ($_.Extension -eq '.pdf')) #Si le fichier en cours de traitement n'est pas un dossier
            {
               [string]$sPathFileDelete = $_.FullName
               $_.Name + $_.LastAccessTime | Out-File -FilePath 'C:\Temporaire\PurgeScan.log' -Encoding default -Append
               $sPathFileDelete
               #Remove-Item -Path $sPathFileDelete
               Write-Output $_.LastAccessTime
              
            }
    
        } 
    }
    
    Stop-Transcript
    
    
    
    I don't know what is the cause of problem, the script is working when I launch it manually, but it not working when I launch it to Scheduled tack with « Run whether user is logged on or not » option.
    Tuesday, May 12, 2015 7:45 AM
  • What are the errors?  You have no error tracking.  Almost any error will cause a failure.  I suspect you have other issues with setting up the task.

    Start with this simplified version and check the errors:

    There is no reason for what you are claiming.  It is either permissions or other errors in script.

    $sPathFolder='D:\Scan\' # '
    $dDate=[DateTime]::Today.AddDays(-7)
    
    Try{
        Get-ChildItem -Path $sPathFolder -include '*.pdf' -Recurse -File -ErrorAction Stop| 
            Where-Object{ $_.LastWriteTime -lt $dDate} |
            ForEach-Object {
                    Remove-Item -Path $_.FullName -Force -ErrorAction Stop
                }
    }
    Catch{
        "$_" | Out-File C:\Temporaire\PurgeScan.log
    }
    Also your date logic was a little convoluted.


    \_(ツ)_/




    • Edited by jrv Tuesday, May 12, 2015 11:14 AM
    Tuesday, May 12, 2015 11:11 AM
  • We can even simplify it more to make it readable and more concise as to what you want to do.  We can catch multiple errors this way:

    Get-ChildItem -Path 'D:\Scan\*' -Include '*.pdf' -Recurse| 
        Where-Object{ $_.LastWriteTime -lt [DateTime]::Today.AddDays(-7)} |
        ForEach-Object {
            Try {
                Remove-Item -Path $_.FullName -Force -ErrorAction Stop
            } 
            Catch {
                "$_" | Out-File C:\Temporaire\PurgeScan.log -append
            }
        }


    \_(ツ)_/



    • Edited by jrv Tuesday, May 12, 2015 11:19 AM
    Tuesday, May 12, 2015 11:18 AM
  • I don't know what is the cause of problem, the script is working when I launch it manually, but it not working when I launch it to Scheduled tack with « Run whether user is logged on or not » option.

    Instead of getting the Task Scheduler to launch the script directly, launch it via this batch file then examine the error log.

    @echo off
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  d:\Test.ps1 1> d:\Test.log 2>&1

    Tuesday, May 12, 2015 12:20 PM
  • Hi Jvr,

    Thank you for your answer and your advices to optimize my script. I have modified my script :

    $sPathFolder = 'D:\Scan\' 
    $iNbJour ='7' 
    
    
    Clear-Host
    
    Start-Transcript -Path C:\Temporaire\PurgeScan.log
    
    Write-Output "Début" | Out-File -FilePath 'C:\Temporaire\PurgeScan.log' -Encoding default
    Get-ChildItem -Path $sPathFolder -Recurse | Out-File -FilePath 'C:\Temporaire\PurgeScan.log' -Encoding default -Append
    
    Get-ChildItem -Path $sPathFolder -Recurse -Include '*.pdf' | 
        Where-Object{$_.LastWriteTime -lt [DateTime]::Today.AddDays($iNbJour)} |
        ForEach-Object{
            Try{
                $_.FullName | Out-File -FilePath C:\Temporaire\resultat.log -Append
                }
            Catch{
            "$_" | Out-File C:\Temporaire\debug.log
            }
        }

    debug.log is empty.

    PurgeScan.log (Transcrypt):

    Get-ChildItem : L'accès au chemin d'accès 'D:\Scan\AAAA' est refusé.
    Au caractère C:\Scripts\Purge_Dossiers_Scan_Utilisateursv1.1.ps1:21 : 1
    + Get-ChildItem -Path $sPathFolder -Recurse -Include '*.pdf' |
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : PermissionDenied: (D:\Scan\AAAA:String) [Get-
       ChildItem], UnauthorizedAccessException
        + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.
       Commands.GetChildItemCommand
     
    Get-ChildItem : L'accès au chemin d'accès 'D:\Scan\BBBB' est refusé.
    Au caractère C:\Scripts\Purge_Dossiers_Scan_Utilisateursv1.1.ps1:21 : 1
    + Get-ChildItem -Path $sPathFolder -Recurse -Include '*.pdf' |
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : PermissionDenied: (D:\Scan\BBBB:String) [Get-
       ChildItem], UnauthorizedAccessException
        + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.
       Commands.GetChildItemCommand

    ......

    Dionysoos


    • Edited by Dionysoos Tuesday, May 12, 2015 12:59 PM
    Tuesday, May 12, 2015 12:58 PM
  • Hi Frederik,

    I have the same error like with start-transcrypt command :

    Get-ChildItem : L'accŠs au chemin d'accŠs 'D:\Scan\AAAA' est refus‚.
    Au caractŠre C:\Scripts\Purge_Dossiers_Scan_Utilisateursv1.1.ps1:21 : 1
    + Get-ChildItem -Path $sPathFolder -Recurse -Include '*.pdf' |
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : PermissionDenied: (D:\Scan\AAAA:String) [Get-
       ChildItem], UnauthorizedAccessException
        + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.
       Commands.GetChildItemCommand
     
    Get-ChildItem : L'accŠs au chemin d'accŠs 'D:\Scan\BBBB' est refus‚.
    Au caractŠre C:\Scripts\Purge_Dossiers_Scan_Utilisateursv1.1.ps1:21 : 1
    + Get-ChildItem -Path $sPathFolder -Recurse -Include '*.pdf' |
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : PermissionDenied: (D:\Scan\BBBB:String) [Get-
       ChildItem], UnauthorizedAccessException
        + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.
       Commands.GetChildItemCommand


    • Edited by Dionysoos Tuesday, May 12, 2015 1:08 PM
    Tuesday, May 12, 2015 1:06 PM
  • Here is your error message:

    L'accès au chemin d'accès 'D:\Scan\AAAA' est refusé

    In other words, the account you use for the scheduled job has no access to the AAAA folder. If unsure what account is used when nobody is logged on, run the job like so to find out:

    @echo off
    echo %date% %time% %UserName% >> d:\Log.txt
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  d:\Test.ps1 1>> d:\Log.txt 2>>&1

    Tuesday, May 12, 2015 1:20 PM
  • Yes I know that is the error, to see my first post.

    When I launch my script manually from a session with the user which is use by the scheduled task, the script is working.

    When I launch the script from scheduled task with "Run whether user is logged on or not" option disable, the script is working, with the same user.

    When I launch the script from scheduled task with "Run whether user is logged on or not", the script isn't working, Access Denied..., with the same user.

    Hence my question :

    When a script ( with Run whether user is logged on or not ) is launched from task scheduler, it don’t use user’s groups right ? "

    Tuesday, May 12, 2015 2:10 PM
  • When a script ( with Run whether user is logged on or not ) is launched from task scheduler, it don’t use user’s groups right ? "

    You can easily find out for yourself with this batch file:

    @echo off

    echo %date% %time% %UserName% 1>> d:\Log.txt 2>>&1
    copy %comspec%  D:\Scan\AAAA d:\Test.ps1 1>> d:\Log.txt 2>>&1

    As I suspected before, this is not a scripting problem but a Task Scheduler / permissions problem.

    Tuesday, May 12, 2015 2:38 PM
  • The task scheduler does not have permissions.  Read the error messages carefully. "Access Denied".  This cannot be fixed with a script.


    \_(ツ)_/


    • Edited by jrv Tuesday, May 12, 2015 3:07 PM
    Tuesday, May 12, 2015 3:03 PM
  • Running as a user always uses groups. You cannot stop that in any simple way.  You are making a wrong analysis somewhere.  I suspect teat the user really has no direct access and is getting access through the "Interactive" group.  This is a group that only exists when logged in at the console.  Give the user permissions on the files and it will work.

    \_(ツ)_/

    Tuesday, May 12, 2015 3:09 PM
  • Thank you for your answers.

    The user that is used by the scheduled task is member of a group with all rights on the folders, but it have not all rights directly.

    This is my question on my first post " When a script ( with Run whether user is logged on or not ) is launched to task scheduler, it don’t use user’s groups right ?"

    Maybe your did not understand this question, because my english is bad…

    I thought to add rights for the user directly to the folder, but before i would like understand why when a scheduled task is started with this option " « Run whether user is logged on or not »", it doesn't use the rights of the user group.

    If I Understand when a scheduled task is started with this option « Run whether user is logged on or not », it does not use the inherited rights of groups the user is a member ?

    Wednesday, May 13, 2015 6:25 AM
  • How do you know the user  has all rights on the folder?

    A user gets rights through groups.  The account has those rights even when running as a task.

    The error is clear.  The user has no access.  Either the file is locked or the user does not have rights.  Only you can track down why this is happening.


    \_(ツ)_/

    Wednesday, May 13, 2015 6:33 AM
  • How do you know the user  has all rights on the folder?

    A user gets rights through groups.  The account has those rights even when running as a task.

    The error is clear.  The user has no access.  Either the file is locked or the user does not have rights.  Only you can track down why this is happening.


    \_(ツ)_/

    How do you know the user  has all rights on the folder?

    Because the user is member of Enterprise Administrator group, the Enterprise group has the all rights.

    A user gets rights through groups.  The account has those rights even when running as a task.

    Yes I know, because the task works when the property « Run whether user is logged on or not » is disable. So the task is started with an user who has the good rights

    But the same task does not work with the property« Run whether user is logged on or not »activated

    Wednesday, May 13, 2015 6:48 AM
  • Task works

    http://hpics.li/e4f1aab

    The same task with property « Run whether user is logged on or not » does not work :

    http://hpics.li/04e2fe6


    Wednesday, May 13, 2015 7:10 AM
  • When you say it doesn’t work what error is that?  How do you know it is not working?


    \_(ツ)_/

    Wednesday, May 13, 2015 7:20 AM
  • When you say it doesn’t work what error is that?

    With Start-transcrypt command I have these errors :

    Get-ChildItem : L'accès au chemin d'accès 'D:\Scan\AAAA' est refusé.
    Au caractère C:\Scripts\Purge_Dossiers_Scan_Utilisateursv1.1.ps1:21 : 1
    + Get-ChildItem -Path $sPathFolder -Recurse -Include '*.pdf' |
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : PermissionDenied: (D:\Scan\AAAA:String) [Get-
       ChildItem], UnauthorizedAccessException
        + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.
       Commands.GetChildItemCommand
     
    Get-ChildItem : L'accès au chemin d'accès 'D:\Scan\BBBB' est refusé.
    Au caractère C:\Scripts\Purge_Dossiers_Scan_Utilisateursv1.1.ps1:21 : 1
    + Get-ChildItem -Path $sPathFolder -Recurse -Include '*.pdf' |
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : PermissionDenied: (D:\Scan\BBBB:String) [Get-
       ChildItem], UnauthorizedAccessException
        + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.
       Commands.GetChildItemCommand

    How do you know it is not working?

    The resultat.log file is empty when I start the task the property« Run whether user is logged on or not »

    activated.

    Get-ChildItem -Path $sPathFolder -Recurse -Include '*.pdf' | 
        Where-Object{$_.LastWriteTime -lt [DateTime]::Today.AddDays($iNbJour)} |
        ForEach-Object{
            Try{
                $_.FullName | Out-File -FilePath C:\Temporaire\resultat.log -Append
                }
            Catch{
            "$_" | Out-File C:\Temporaire\debug.log
            }
        }

    When I start the task with the property « Run whether user is logged on or not » is disable,
    resultat.log file list the files which have not been accessed for more than 7 days.



    Wednesday, May 13, 2015 7:33 AM
  • The log file will always be empty because you are throwing an error which is terminating the script before you can write too the log.

    Run a scrip with only one line

    'Test of logfile' | Out-File  C:\Temporaire\resultat.log -Append

    If the account has access to that path and the log is not locked this will work.  I use it all of the time with no issues.


    \_(ツ)_/

    Wednesday, May 13, 2015 7:59 AM
  • The account has access to that path because the task work when the property « Run whether user is logged on or not » is disable.

    I have modified my task with the script which contain this line :

    'Test of logfile' | Out-File  C:\Temporaire\resultat.log -Append

    The file resultat.log contain one line with Test of logfile,after the execution of the task.


    • Edited by Dionysoos Wednesday, May 13, 2015 8:34 AM
    Wednesday, May 13, 2015 8:28 AM
  • So now you can believe the "Access Denied" messages. 

    Try this now:

    Get-ChildItem -Path 'D:\Scan\*' -Include '*.pdf' -Recurse -EA 0| 
        ForEach-Object{
            $_.Fullname | Out-File C:\Temporaire\resultat.log -Append
        }
    "Total errors $($error.count)" |  Out-File C:\Temporaire\resultat.log -Append
    


    \_(ツ)_/

    Wednesday, May 13, 2015 8:36 AM
  • The log file contain :

    Total errors 169

    Wednesday, May 13, 2015 9:31 AM
  • So the script has no access to any files. Now look closely at the security on the files.  They are probably owned by the process that created them and allow no access to the user.

    \_(ツ)_/

    Wednesday, May 13, 2015 1:28 PM
  • JRV

    The user has the right to access  the folders.

    - I'am logging to the server with this user, I can access to all folders.

    - The task works when the property « Run whether user is logged on or not » is disable. So the account has the right to access  the folders, No ?

    Thank you.

    Wednesday, May 13, 2015 1:46 PM
  • Don't care about folder.  Look at file permissions.


    \_(ツ)_/

    Wednesday, May 13, 2015 1:55 PM
  • The files have the same right that the folders, I have checked it.

    And the task works when the property « Run whether user is logged on or not » is disable, it list the all PDF that have more than 7 days to resultat.log.

    Wednesday, May 13, 2015 2:04 PM
  • If the script works standalone (not through the task scheduler), then this isn't a scripting question but rather a question about how to use the task scheduler.

    You need to ask your question in a more appropriate forum.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, May 13, 2015 2:15 PM
    Moderator
  • The script works with the task scheduler, but it doesn't work when the property « Run whether user is logged on or not » is activate to the task.
    Wednesday, May 13, 2015 2:20 PM
  • You stated in your initial message that the script works standalone (when you don't run it from the task scheduler). This means that the script itself works, and therefore you don't have a scripting question. Your question is about how to run the script using the task scheduler. This is not a scripting question but rather a task scheduler question. You are not asking your question in the correct forum.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, May 13, 2015 2:23 PM
    Moderator
  • I also suggest that you are missing some subtle item in the two scenarios. Since we cannot see your screen we cannot know what you might be missing.

    It is also possible that you have a system problem.

    It is not a scripting issue.  There is nothing we can put into a script that will alter this behavior.


    \_(ツ)_/

    Wednesday, May 13, 2015 2:32 PM
  • I also suggest that you are missing some subtle item in the two scenarios. Since we cannot see your screen we cannot know what you might be missing.

    It is also possible that you have a system problem.

    It is not a scripting issue.  There is nothing we can put into a script that will alter this behavior.


    \_(ツ)_/

    JRV you are a genius !!!! Thank you a lot !!

    I have created a new task, with the same script, the same parameters, the same actions, the same server, the same all, that the original task.

    WTF !!! The Task works !!

    Original task

    http://hpics.li/d43c8c4
    http://hpics.li/71c9012
    http://hpics.li/3e7cb97
    http://hpics.li/65d1fee
    http://hpics.li/63ee1ff

    New Task
    http://hpics.li/efce972
    http://hpics.li/7c8b5b2
    http://hpics.li/75962d0
    http://hpics.li/3306f27
    http://hpics.li/3138c3d

    It's not a bug, it's a feature ?

    Thank you for you investment and you time.


    • Edited by Dionysoos Wednesday, May 13, 2015 3:18 PM
    Wednesday, May 13, 2015 3:17 PM
  • Which is why it is not  a scripting issue.

    \_(ツ)_/

    Wednesday, May 13, 2015 3:25 PM