question

RajuNunnaSathi-8080 avatar image
0 Votes"
RajuNunnaSathi-8080 asked MotoX80 edited

Unable to remove the file using powershell script in task scheduler

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-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered RajuNunnaSathi-8080 commented

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.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Followed your instruction by giving an UNC Path, But it didn't works.

Strange is that the file is deleted when I manually click on Run in task scheduler, But it's not deleting when the task happens at scheduled time.

0 Votes 0 ·

Have you checked the last run result of the scheduled task? Make sure the task completed successfully.

0 Votes 0 ·

Yes, it is success.


some part of the code is working perfectly. This remove-item code I put it last, This is only not executing.

I kept in starting of my code and then it works.


0 Votes 0 ·
MotoX80 avatar image
0 Votes"
MotoX80 answered RajuNunnaSathi-8080 commented

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


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HI,

I ran the above 6 lines of code given by you.

"Start-Transcript : This host does not support transcription." -- Error. How can we resolve this?

And the file is getting removed while I run manually.

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.

Also, Please let us know the use of transcript commands. Are these used for creating logs?

0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered MotoX80 edited

"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://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-5.1

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.