Heidi
Heidi

Reputation: 75

Running a Powershell script from Task Scheduler

I need to run a PowerShell script from Task scheduler for SharePoint online. The script :

  1. Gets the Document version from a https://xxx.sharePoint.com/site/ggg.
  2. Outputs to a .csv file on local drive.
  3. Acccess https://xxx-admin.sharepoint.com to post the CSV to a xxx.Sharepoint.com/site/aaaa.

The script works perfectly in PowerShell. I can't get it to run off my machine. There is no error message, or output file on C:/.

Can this script be run of my local machine?

Task Scheduler Settings

enter image description here

Upvotes: 4

Views: 23759

Answers (2)

Larry Sigley
Larry Sigley

Reputation: 81

To run PowerShell scripts you need to do one of two things. Change the Execution policy to either "RemoteSigned" (script must be created on this machine) or "Unrestricted" (not recommend), because by default it is set to "Restricted" which will not run any scripts. The second option ignores the execution policy. To start you open up\edit your original powershell script and encapsulate it with this syntax "powershell -ExecutionPolicy ByPass { script code goes here } " Also, if you want to see if an error is generated when you run the scheduled task then you need to add this argument "-NoExit". The reason the console closes is because by default the scheduled task runs the script in a background console and this closes right after and you need the "-NoExit" command to keep it open. However, if you leave the "-NoExit" argument then it will keep open all of these consoles each time the task runs. So make sure you remove it when you are sure the script is running successfully and without errors.

Encapsulated script syntax example:

powershell -ExecutionPolicy ByPass {

   # Original script code here
}

Encapsulated script that keeps the console from closing syntax example:

powershell -ExecutionPolicy ByPass -NoExit {

   # Original script code here
}

Creating A Scheduled Task:

  1. Open Task Scheduler by pressing "Windows Key + R" this will bring up the run dialog and in the "Open" text-box type "taskschd.msc"

  2. Click "Create Task" and type in the NAME field the name you want to give this task. Then determine security options you want to use, to run as administrator use the "Run with highest privileges" option.

  3. Click "Triggers" tab and then click "New". This is where you choose what begins the task by choosing from the drop down, by default the on a schedule is selected. Then you choose the frequency, times and other advance settings you want.

  4. Click "Actions" tab then click "New" and for actions leave as "Start a program" in the drop-down. In the "Program/script" text-box type "powershell.exe" and in the "Add arguments (option)" field type -File "FULL FILE PATH" and add the opening and closing quotation marks too. example: -File "C:\Users\Public\Documents"

  5. Click "Conditions" tab and leave the defaults for the most part or select other conditions.

  6. Click "Settings" tab and normally the default is fine but you can choose other settings if you like.

https://blog.netwrix.com/2018/07/03/how-to-automate-powershell-scripts-with-task-scheduler/

Upvotes: 8

En.Kidu
En.Kidu

Reputation: 1

Not really sure, how you run it, but to answer your question: Yes, it can if you have PowerShell and all necessary libraries installed. You may need to set Execution policy, if you haven't yet and run the script with user that has enough permissions.

Check these questions and responses: How to run a PowerShell script

Upvotes: 0

Related Questions