p/s scripts and task scheduler windows 10

Welcome Forums General PowerShell Q&A p/s scripts and task scheduler windows 10

  • This topic has 10 replies, 2 voices, and was last updated 2 months ago by
    Participant
    .
Viewing 10 reply threads
  • Author
    Posts
    • #214545
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      Ok I’m sure this is a no brainer for you guys but this just really stumps me. I have a simple script that deletes files out of a temporary file folder I have setup for multiple video editing software. I can run it in p/s admin and it works. I can add and execution bypass in non admin and it works. but I can’t get it to run in task scheduler. here is my script. 0 days was set just to test. I haven’t determined how many days I want temp files there before deletion yet. I have t/s setup to run at highest privileges I enter the password I have tried calling on this script setup as a .ps1 file to be opened with p/s and I have called on p/s and entered this as an argument.  I am much more on the hardware side of computers and I use p/s alot for diskpart and robocopy….so I’m pretty clueless otherwise.

      Set-ExecutionPolicy -ExecutionPolicy AllSigned -Scope process
      Get-ChildItem -Path "E:\Temporary Files\Temp" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(0))} | Remove-Item

    • #214551
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      How do you start the script in the task scheduler? What’s command line?

      Why do have this line in your script? “Set-ExecutionPolicy -ExecutionPolicy AllSigned -Scope process”

      Is your script digitally signed?

    • #214557
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      I came to using all signed because it didn’t kick an execution error when running out of admin. Script is not signed. I have ts set to run daily if user is logged on or not and to be able to run on demand. I haven’t even seen if the task will run on its own yet I am simply hitting run while the task is selected in ts.

    • #214560
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      And honestly you can tell me it’s all wrong. I have done coding at a very low level for java,visual basic and sql but it’s been a while. I got so frustrated last night with it I can’t say if I made progress or not. All I know is the script that actually picks the files and deletes them does work ran manually.

    • #214563
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      OK, first – delete the line with the Set-ExecutionPolicy. It does not make any sense or serv any purpose for your script.

      Could please share the complete command line you’re using to run your script in the task scheduler. Please format it as code using the code tags “PRE“. Thanks

      Read Me Before Posting! You’ll be Glad You Did!

    • #214569
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      Sorry for my ignorance but I don’t understand what you asking. I am using the task scheduler gui and in the action tab I am calling on powershell.exe and adding my script as an argument.

    • #214575
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      Sorry for my ignorance but I don’t understand what you asking. I am using the task scheduler gui and in the action tab I am calling on powershell.exe and adding my script as an argument.

      OK. I’m assuming you’re using the “Action” – “Start a program”, right? What exactly do you have in “Settings” – “Program/script:”? And what do you have in “Add arguments (optional):”? And please do not describe it – copy it.

    • #214578
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      This actually worked but again your welcome to tell me that I need to do it differently. I have been putting random text documents into my temp folder to verify it is removing them.

      Program/script : powershell.exe

      Add Arguments:

      -ExecutionPolicy Bypass -File “C:\Users\Tom Lang\Documents\EditCleanup1.ps1” 

      Code in EditCleanup.ps1 :

       Get-ChildItem -Path “E:\Temporary Files\Temp” -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(0))} | Remove-Item
    • #214584
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      This actually worked but again your welcome to tell me that I need to do it differently. I have been putting random text documents into my temp folder to verify it is removing them.

      How “This actually worked … “? I understood that it does not work. Maybe I missunderstood your issue.

      OK, your command line looks just fine and your script (except of the Set-ExecutionPolicy thing) works as well … I just tested it.

    • #214590
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      sorry you did understand me correctly. At the start of this I could not get it to work. While talking this over with you I was trying different things and came across something that worked. I do thank you for taking the time to work with me on this.

    • #214593
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      While talking this over with you I was trying different things and came across something that worked. …

      Great. I’m glad you could solve your problem.

      Have a nice day.

Viewing 10 reply threads
  • You must be logged in to reply to this topic.