Tuesday, March 12, 2013

Create a Scheduled Task for PowerShell Script with Windows Task Scheduler

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/2012 R2. How to schedule a PowerShell script using Windows task scheduler?

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!

Here is how you can Run PowerShell Script from Batch files: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -command "<Your PowerShell Script Location>"

PowerShell script with parameters in scheduled task:
Say, We've a Parameter "$WebAppURL" in our PowerShell script:

       [parameter(Mandatory=$true)] $WebAppURL
 # We'll trigger the script as:  .\StorageReport "http://sharepoint.crescent.com"

So in Task scheduler, Add arguments(optional), Enter: d:\scripts\StorageReport "http://sharepoint.crescent.com"

When there are multiple parameters, you can separate them by giving parameter name as key. such as:

       [parameter(Mandatory=$true)] $WebAppURL,
       [parameter(Mandatory=$true)] $OutPut

# We trigger it as: StorageReport -WebAppURL "http://sharepoint.crescent.com" -output "d:\Reports\StorageReport.csv"

Create Scheduled Tasks with PowerShell:
From PowerShell 3.0 (Windows Server 2012 R2) onwards , We've a new cmdlets to create and manage Scheduled Tasks! Here is an example:
$TaskName = "Audit Large Lists"
$username ="Crescent\SP13_FarmAdmin" 
$password ="Password Here"

#Create Scheduled Task
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "E:\Scripts\AuditLists.ps1"
$Trigger = New-ScheduledTaskTrigger -Daily -At 1am
$ScheduledTask = New-ScheduledTask -Action $action -Trigger $trigger  

Register-ScheduledTask -TaskName $TaskName -InputObject $ScheduledTask -User $username -Password $password  

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...