Unable to remove the file using powershell script in task scheduler

Raju, Nunna Sathi 1 Reputation point
2021-06-10T10:18:01.31+00:00

While running the powershell, the file is deleted with the following command

Remove-Item -Path Q:\ALSAutomation\Appln_issue.txt

But the same file is not deleted, when we run the script using task scheduler using windows 10.

Kindly help me to resolve it.

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-06-10T11:52:16.65+00:00

    A "Q" drive would typically be a mapped network drive. A scheduled task does not go though the full desktop logon process where that drive would get mapped. The easiest solution is to use the UNC path to the file

    Remove-Item -Path \\MyServer\ALSAutomation\Appln_issue.txt
    

    Also the account that the task runs as needs to have access to that folder. You can use Test-Path or Get-Childitem to verify that your task can "see" the folder.


  2. MotoX80 36,291 Reputation points
    2021-06-10T22:29:05.28+00:00

    Have the script create a transcript. Examine the MyScript.log file and verify that the script executes at the time that you scheduled the task for. Verify that the task is running as the account that you expect it to run as. When you define the task be sure to enter the user's password so that it can execute at the scheduled time and does not depend on an account being logged on to the desktop.

    start-transcript c:\temp\Myscript.log 
    "Looking at the shared folder to verify that we can access it...."
    get-childitem \\MyServer\ALSAutomation\
    "Removing item"
    Remove-Item \\MyServer\ALSAutomation\Appln_issue.txt
    Stop-Transcript
      
    

  3. Anonymous
    2021-06-11T12:27:56.657+00:00

    Hi,

    It seems the powershell process stoppped before the last line finished running. You may test if the file has been removed successfully at the end of the script.

     $file = '\\MyServer\ALSAutomation\Appln_issue.txt'  
     Remove-Item -Path $file  
     while($true){  
        if(-not (Test-Path -Path $file)){  
            break  
        }  
        Start-Sleep 5  
     }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. MotoX80 36,291 Reputation points
    2021-06-11T12:38:56.097+00:00

    "Start-Transcript : This host does not support transcription." -- Error. How can we resolve this?
    Note: My issue is resolved, as I kept Remove-Item in starting of my code and then it works.
    Appreciate if you can demonstrate why it is not working at the end of the code.

    You are asking for a "Magic Crystal Ball" answer. You want to know why transcripts don't work, but you have provided no details about your environment. Forum users have no idea if you are running Win7, Win10, or Linux. Or what version of Powershell you have installed.

    You want me to explain to you why moving statements in the script causes it to work. But you haven't shared the source code for me to examine. I have have no idea why YOUR CODE is doing what it is doing. If transcripts are not available, then add in your own debugging and logging statements so that you can examine what execution path the script is taking. Be sure to include try/catch error handling in case your script runs into unexpected errors.

    Start-Transcript is available (at least) in Powershell 5.1. You could install that version, but I have no idea what impact that would have on other scripts.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-5.1

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.