Tuesday, May 31, 2016

How to Run a SharePoint PowerShell Script From Task Scheduler

Prerequisites:

Go ahead and add this line to your SharePoint PowerShell script.  It is absolutely necessary because we have to run this script in the context of Windows PowerShell and not the SharePoint Manangement PowerShell.

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

Make certain the user account running the task has the following permissions:

  • Log on as a batch job rights in the User Rights Assignment section of the server Local Security Policy. These rights could be set on the server itself or by Group Policy if managed from Active Directory – see http://technet.microsoft.com/en-us/library/dd277404.aspx for details
  • Permissions to use PowerShell on the SharePoint farm. You can add these for a new user by running Add-SPShellAdmin –UserName domain\username in the SharePoint 2010 Management Shell console as a current administrator – see http://technet.microsoft.com/en-us/library/ff607596.aspx for details
  • SharePoint permissions to be able to perform the script operation in SharePoint.


1. Open Task Scheduler

Open Task Scheduler and Create a new task. Name it and set your security options.  Check "Run with highest privileges" as most scripts need to run as an admin.





2. Set Triggers

Click on the Triggers tab and set your schedule or event that will trigger the running of your PowerShell script.        





3. Create your Action

Click on the Actions tab and click on New.

Action: Start a program

In the Program/Script text box browse to and select PowerShell.exe


4. Set Argument - this is the important part!

Copy this and paste into the Argument text box.  Be sure to change the path and script filename accordingly but make sure you leave the quotes as they are.  Each parameter is explained below in case you are interested.

-version 2 -PSConsoleFile "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\psconsole.psc1" -executionpolicy bypass -command ".'D:\Scripts\MyPSSript.ps1'"




  • -version 2: Uses the PowerShell Engine 2.0
  • -PSConsoleFile: Specifies the SharePoint Management PowerShell environment to allow SharePoint commands to run within the context of Windows PowerShell
  • -executionpolicy bypass: Opens security to run the script. This security policy will only be in effect for the script running.
  • -noexit: Add this to keep the PowerShell window open while testing.


5. Save and Test

3 comments:

  1. For arguments I used -ExecutionPolicy Bypass C:\scripts\examplea.ps1 and set the account to my login name for testing. If you select Run Only When User is Logged In you can run the task manually and view any output in the script. Hope this helps!

    ReplyDelete
  2. Did not work at first, but was able to get it working changing it a bit. Thanks!
    Posting for others supporting SharePoint 2010 in 2020 (included debugging comments)


    #Write-Host "Ektron Search Server - Start Crawl nameOfYourCrawl !"

    # If running in the console, wait for input before closing. Leave for debbugging
    if ($Host.Name -eq "ConsoleHost")
    {

    #Get-ExecutionPolicy
    #Set-ExecutionPolicy Unrestricted

    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
    {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    }

    $searchapp = Get-SPEnterpriseSearchServiceApplication -Identity 'Search Service Application'
    $contentsource = Get-SPEnterpriseSearchCrawlContentSource -SearchApplication $searchapp -Identity 'nameOfYourCrawl'
    $contentsource.StartIncrementalCrawl()

    #$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null
    }

    #Read-Host -Prompt "Press Enter to exit"

    task schedule: -version 2.0 -PSConsoleFile "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\psconsole.psc1" -executionpolicy bypass & "D:\ScheduledTasks\yourScript.ps1"
    # 11/30/2020 - Ektron Search Service

    ReplyDelete