Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Create a Scheduled Task for PowerShell Script with Windows Task Scheduler

$
0
0
PowerShell is really a game changer for repeatable processes, isn't it? We've a PowerShell script to generate report on SharePoint content databases size growth SharePoint Content Databases Size - Storage Report, We used to run it on first day of every Month on SharePoint server to generate report.
Why don't we automate it with Windows Task scheduler? Sure! Lets create a scheduled task for PowerShell script with Windows task scheduler in windows server 2008 R2.

How to Create a Scheduled Task for PowerShell Script with Windows Task Scheduler
Here is how you can create Scheduled Tasks manually:

1. Start>> Administrative Tools>> Task Scheduler.  From Actions menu click on "Create a Basic Task"
windows task scheduler run powershell script
2. Give it a Name & Description Say: Content Databases Report, and click "Next"
windows task scheduler powershell script 
3. Select the interval you want to run the program. In my case, I chose "Monthly"
powershell script task scheduler windows 2008 r2

4. Specify the Months, Days in which the script to run. I've selected "All Months" and Day 1
schedule powershell script using task scheduler

5. In Action Tab, Select "Start a program" option button. Click Next
task scheduler to run powershell script 
6. In Start a Program Tab:
  • In Program/script, Enter: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  • In Arguments, Enter the path of the script. Say: D:\Scripts\ContentDatabaseReport.ps1
  • In Start in, Enter the path where the script is located. say: D:\Scripts
powershell script scheduled task parameters
You must specify value for Start-in field, even though its optional. This is why because, if no value specified there, PowerShell will create the output in "C:\Windows\System32" directory.

7. Select the check box, "Open the Properties dialog for this task when I click Finish" and click Finish button.

8. Now, in the properties dialog, under the General tab, Make sure that the "Run when user is logged on or not" and "Run with highest privileges" check boxes are selected.
You will get a login prompt on clicking "OK" button. Enter the User Name & Password in which the task runs.

Create Tasks in Task Scheduler Command Line:
We can create scheduled task with the command line tool schtasks too!
schtasks /create /TN "Content Database Report" /SC monthly /mo 1 /ST 00:00 /RU "Domain\UserName" /RP "Password" /RL "HIGHEST" /TR "PowerShell D:\Scripts\ContentDatabaseReport.ps1" 

This will set the options "Run with highest privileges" and "Run whether the user is logged on or not"  for the Scheduled Task.

To Run the Scheduled Task on-demand:
  • Right click on the created task and choose Run.
  • switch over to the script file location and verify new report has been generated as an output from the script.
run scheduled task on demand

That's all! We've created a task in windows task scheduler for powershell script!

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>