Thursday, 13 January 2011

Running a SharePoint PowerShell script from Task Scheduler

There are often times where you may want to run a PowerShell script for SharePoint on a schedule – for example, a backup routine, report, or script to automate a process – just like the good old days of running a batch file in Task Scheduler. Well, things aren’t really that different these days either – yes, the scripting language may have changed, but Task Scheduler can still be used as before.

In this example, I am going to script the backup of a site collection and run it every night at 2:00AM. First, the script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
Backup-SPSite -Identity
http://intranet.pacdomain2.local -Path C:\Install\Intranet.bak –Force

Although the script is quite simple, note the Add-PSSnapin command on the first line. We need this because we are going to run the script using the standard Windows PowerShell console application and not the SharePoint 2010 Management Shell that has the snap-in already loaded. The –erroraction switch is there to prevent any errors should the snap-in already be loaded through a profile on the server.

Save your script to a local drive on the server as a PS1 file.

Next, load Task Scheduler from Start > All Programs > Accessories > System Tools. The version I am using here ships with Windows Server 2008 R2, but the concepts should be the same on earlier releases.

image

To create a new scheduled task, click Create Task from the right-hand “Actions” panel. At a minimum, you should fill out the following information:

General Tab

  • Name of the task – e.g., Intranet Backup. A description is also useful, but not mandatory
  • Ensure 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
    • Permissions to be able to perform the script operation in SharePoint. For example, for the site collection backup in this example, the account used in the scheduled task required:
      • dbo access in SQL to the database hosting the site collection
      • Membership of the local Administrators group on the server – you may be able to experiment with fewer permissions than this, but this was required on my development server, which is also a domain controller
  • Select Run whether the user is logged in or not and tick the Do not store password checkbox
  • For certain operations, you may also need to enable the Run with highest privileges option. As this should only be enabled when necessary, I would recommend testing the script from Task Scheduler with the option disabled and only enable if the script does not work without it

image

Triggers Tab

Click New and add your schedule, as required. In the example below I am going for daily at 2:00AM

image

Actions Tab

Click New. Ensure the action is set to Start a program and type Powershell.exe in the “Program/Script” box. In the “Add arguments (optional)” box, type &'c:\scripts\script.ps1' replacing the path and name with the details for your script. Wrapping the file path and name in single quotation marks allows you to specify spaces in the text.

image

When you click OK to confirm task creation, you will be prompted to enter the password for the account you selected to run the script. Enter the password and click OK.

You should now see your script in the Task Scheduler Library (if not, click Refresh in the right-hand panel). To test the script, highlight it in the console and click Run from the right-hand panel.

image

Check that the changes required are performed (for this example it was checking to ensure the backup file was being created in the folder when the task ran) and leave it to run on the schedule. Don’t forget to perform regular checks in the future to test the script continues to run as expected.

117 comments:

  1. Replies
    1. JUAL Obat Aborsi Medan MANJUR UNTUK USIA 1-7 BULAN.
      Obat Penggugur Kandungan Medan YANG KAMI JUAL RESMI DARI RUMAH SAKIT, JADI ANDA TIDAK PERLU RAGU LAGI UNTUK ORDER Cytotec Asli Medan
      YANG KAMI JUAL KARENA BERGARANSI DAN DIJAMIN 100% TUNTAS TANPA HARUS KURET LAGI.
      Ciri Cytotec Asli adalah obatnya tidak ada bentuk lain selain persegi enam dan warnanya pun cuma putih.

      INFO LEBIH LANJUT KLIK WEBSITE RESMI KAMI YANG TERTERA DI BAWAH INI:

      Obat Aborsi Di Medan
      Obat Penggugur Kandungan Di Medan
      Penggugur Kandungan Di Medan
      Obat Penggugur Janin Di Medan
      Penggugur Janin Di Medan
      Obat Telat Bulan Di Medan
      Obat Cytotec Asli Di Medan
      Cytotec Asli Di Medan

      Obat Aborsi
      Cara Menggugurkan Kandungan
      Menggugurkan Kandungan
      Menggugurkan Janin
      Obat Penggugur Kandungan
      Penggugur Kandungan
      Obat Cytotec Asli
      Cytotec Asli
      Obat Peluntur Janin
      Peluntur Janin
      Obat Penggugur Janin
      Penggugur Janin
      Obat Telat Bulan
      Cytotec Asli

      CALL/WA : 081325000880
      BBM : 54D5479D

      Delete
  2. Great post. I am having a problem, however. I have created my SharePoint script to check the status of the content being crawled, when it last completed the crawl, the schedule, etc. then email it to me in a .csv file.

    If I open windows powershell and run the script, it sends the message fine and the .csv has the necessary information. However, if I create the task in Task Scheduler and run it, it sends the attachment but it doesn't have the information in it. I threw a 'start-transcript' into the script and the error that comes up is that it can't find the SPFarm when it tries to execute the script.

    The script adds the proper snapin to get the SharePoint items, but it fails only when ran using task scheduler. Any ideas?

    ReplyDelete
  3. Nevermind, I resolved my issue. What I did was create a batch file that ran powershell then called the command to run my script. Something like this:

    powershell -command "& ' C:\Utility\crawl.ps1 ' "

    ReplyDelete
  4. Clear interface and effectiveness of this scheduler looks suitable for all user community. But its seamless scheme is not for serious and specific projects and tasks. In my projects we use comindware task management http://www.comindware.com/task-management/ and its flexibility to solve specific tasks looks rather perspective. I think that Windows task managers are not the best tool for it.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. @sam Thanks for the tip on the catch file. It fixed my issue too with running http://get-spscripts.com/2012/03/sending-sharepoint-system-notification.html?showComment=1339095028183#c5689996936969159589

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. How can I do it programatically using Powershell for create Task Scheduler ?thx

    ReplyDelete
  9. Thank you so much!! Time saving...:)

    ReplyDelete
  10. Thank you. Saved a lot of time. My script didn't work without Add-PSSnapin .

    ReplyDelete
  11. Mở bán chung cư Eco Spring Nguyễn Xiển | Thông tin chung cư GoldSeason Nguyễn Tuân | Mở bán chung cư Gold Season | Phân phối chung cư GoldSeason 47 Nguyễn Tuân | Tin tức chung cư 47 nguyễn tuân | Thông tin dự án GoldSeason | Tìm hiểu chủ đầu tư GoldSeason | Thông tin chung cư Eco Spring

    ReplyDelete
  12. chung cu gemek du an gemek premium mo bat dot moi nam 2016.
    Du an can ho cao cap Gemek Premium chinh thuc mo ban dot 1.
    Danh sach chung cu Ha Noi can ho chung cu cac quan Ha Dong, Cau Giay, Hoang Mai….
    FLC Group ra mat du an cao cap tai Ha Dong du an flc quang trung voi quan the tien ich chat luong cao.
    Mot du an cao cap voi quan the can ho chung cu va biet thu lien ke chung cu flc garden city quan nam Tu Liem, Ha Noi.
    Mot du an cao cap voi quan the can ho chung cu Tay Ho chung cu 69b thuy khue chu dau tu SUN GROUP.
    Chung cu mat duong Nguyen Xien Eco Green City mo ban block Eco Spring.
    Chung cu mat duong Quang Trung flc star tower mo ban FLC Star Tower. Thong tin chung cu Ha Noi du an bds chung cu ha noi.

    ReplyDelete

  13. If you schedule your home changing with one of good packers and movers in Riyadh then it is for certain that you will have hassle-free relocation experience. You will have peace of mind. You will save your valuable money, energy and time. You will need to do nothing as all jobs associated with your move will be properly done and carried out by experts of professional movers and packers in Riyadh. Experts will pack your household belongings carefully and load them onto the truck for safe transport. They will use especially designed goods carriers or trucks to transport your possessions to your new destination door step. These people will also unpack containers and rearrange items at your new place. A good mover will also provide goods insurance policy along with service. Plus hence you will have good thing about insurance for your goods during transit. It indicates mover will cover the loss if your possessions are sadly damaged or lost in transit. So it is but apparent that selecting services of a good moving company in Riyadh will ease your entire moving issues whether you are shifting from one vicinity to another within metropolis of Hyderabad or transferring to a new city
    شركة تخزين الاثاث بالرياض
    افضل شركة تنظيف بالرياض
    افضل شركة تنظيف خزانات بالرياض
    افضل شركة مكافحة حشرات بالدمام


    A move is only considered as a successful move when it’s guarded and superintend. Yes management is a key factor for a safe relocation. If you are planning for a moving and shifting, then you are supposed to be managed. And this management needs to be clearly reflected in your packing and moving. In case you are unable to spare adequate time for your managed move, then we would suggest you to go for the best remedy of that which is Removal Company.

    شركة نقل اثاث بالرياض
    شركة رش مبيدات بالرياض
    افضل شركة مكافحة حشرات وفئران
    افضل شركة غسيل مجالس بالرياض

    شراء اثاث مستعمل بالرياض
    كشف تسربات المياه
    افضل شركة تنظيف منازل بالرياض

    ReplyDelete
  14. - رش مبيدات
    تتميز شركة الاول بانها تستخدم افضل معدات رش المبيدات في المملكة
    وتم تعريف المبيد الحشري على انه هو مبيد آفات يستخدم ضد الحشرات في جميع أطوار نموها.

    وتستخدم الاول جميع انواع المبيدات مثل :
    1- المبيدات النقية
    2- المبيدات البودرة
    3- المبيدات المركزة المستحلب
    4- المبيدات القابلة للذوبان في مواد اخرى مذيبه
    بالاضافة الى وسائل الرش مثل الرش الدخانى والرزازى والتعفيري والضبابى البارد والحرارى
    الاول لتفوقها في مجال رش المبيدات تعد افضل شركة رش مبيدات شمال بالرياض وافضل شركة رش مبيدات بجدة وافضل شركة رش مبيدات بالمدينة المنورة وشركة رش مبيدات بالاحساء وشركة رش مبيدات بالخبر و شركة رش مبيدات بالخرجو شركة رش مبيدات بابها و شركة رش مبيدات بالقصيم و شركة رش مبيدات بينبع
    وافضل شركة رش مبيدات بالجبيل

    ReplyDelete
  15. Hiện tại sàn giao dịch Kiều Anh đang mở bán dự án Grand World Phú Quốc

    ReplyDelete
  16. Our Browns House Flag hangs vertically and provides a top sleeve for insertion of your flagpole. The Browns House Flag measures 28x42 inches, is constructed of 2-ply polyester, and both sides are dye sublimated with the NFL team logo as shown.
    house divided flags nhl
    nfl house flagswholesale Minnesota Vikings banners
    New York Giants american banners

    ReplyDelete
  17. Impiegando design tradizionale del club, il kit a casa Pescara 16-17 presenta un aspetto a strisce in bianco e blu. Un modo interessante costruito V-collare e dettagli sui polsini e orlo completano il look unico della maglia Pescara 2016-17, che si combina con pantaloncini bianchi e calzettoni.maglie calcio 2017,
    maglie calcio poco prezzo, Maglia Athletic Bilbao 2017
    comprar Maglia Atletico Madrid

    ReplyDelete
  18. Impiegando design tradizionale del club,

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. This comment has been removed by the author.

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete
  23. Wow, Excellent post. This article is really very interesting and effective. I think its must be helpful for us. Thanks for sharing your informative.
    SEO jobs in Hyderabad
    SharePoint jobs in Hyderabad
    SharePoint jobs in Chennai
    PerfectProfile

    ReplyDelete
  24. http://www.nfljerseys.us/ Cheap NFL Jerseys

    http://www.raybanoutletclearance.com/ Ray Ban Outlet

    http://www.longchampbags.us.com/ Longchamp Outlet

    http://www.tomsoutlet-online.us.com/ Toms Outlet

    http://www.adidas-uk.org.uk/ Adidas

    http://www.yeezy.com.co/ Yeezy

    http://www.raybansunglasses.ca/ Ray Ban Sunglasses

    http://www.yeezys.org/ Yeezys

    http://www.kade-sapde.us.com/ Kade Sapde

    http://www.adidasoutlets.us.com/ Adidas Outlet

    http://www.nike-stores.fr/ Nike Store

    http://www.underarmouroutlet.us.com/ Under Armour

    http://www.timberlanduk.org.uk/ Timberland

    http://www.jordan12.us/ Jordan 12

    http://www.kedsshoesforwomen.com/ Keds Shoes

    http://www.nikeairmax.us/ Nike Air Max

    http://www.outlettoms.us/toms-women-c-1/ Toms Outlet

    http://www.ultraboostuncaged.us/ Ultra Boost

    http://www.mlb-jerseys.us/ Cheap MLB Jerseys

    http://www.adidas-nmds.com/ Adidas NMD

    http://www.oakleyoutlet.net.co/ Oakley Outlet

    http://www.katespadeoutlets.us/ Kate Spade Outlet

    http://www.adidas-outlet.org/ Adidas Outlet

    http://www.nikeairmax-90.com/ Nike Air Max 90

    http://www.timberland-outlet.us/ Timberland Outlet

    http://www.adidas-nmd.org.uk/ Adidas NMD

    http://www.adidaseqt.com/ Adidas EQT

    http://www.ralphlaurens.org.uk/ Ralph Lauren UK

    http://www.jordan4.us/ Jordan 4

    http://www.cheapjordan-shoes.com/ Cheap Jordan

    http://www.adidasnmdoutlet.com/ Adidas NMD

    http://www.katespadeoutlet.us.org/ Kate Spade Outlet

    http://www.longchamp-outlet.ca/ Longchamp Outlet

    http://www.adidasnmdsales.us/ Adidas NMD R1

    http://www.nike-airmax90.com/ Nike Air Max90

    http://www.running-shoes.top/ Running shoes

    http://www.starboots.us/ Basketball Shoes

    http://www.michael-kors-bags.us/ Michael Kors bags

    http://www.yeezy-outlet.us/ Adidas Yeezy

    http://www.asics.us.com/ Asics shoes

    http://www.coach-online.us/ Coach online

    http://www.timberland-outlets.org/ Timberland shoes

    http://www.jordan-air.us/ Jordan Shoes

    http://www.adidasyeezy.org/ Adidas Yeezy Shoes

    http://www.vapor-max.com/ Nike Air Vapor Max

    http://www.rayban.com.co ray ban sunglasses

    http://www.nike-store.us Nike Outlet

    http://www.longchamp--bags.com longchamp bags

    http://www.adidas-shoes.us Adidas Shoes

    http://www.nike-outletstore.com Nike Shoes

    http://www.yeezy-boost350.com Yeezy Shoes

    http://www.jamesshoes.org james Sale

    http://www.adidas-online.us Adidas NMD

    http://www.jordan--shoes.org Jordan 12

    http://www.air-max2017.com Air Max Outlet

    http://www.asicsgel.org Asics Gel

    http://www.flipflops.us.com Flip Flopst

    http://www.ray-bans.in.net ray ban sunglasses

    http://www.oakleysunglassesoutlet.net oakley sunglasses

    http://www.oakleysunglassesstore.us oakley sunglasses

    http://www.cheapoakleysunglasses.biz cheap oakley sunglasses

    http://www.cheapraybansunglasses.biz cheap rayban sunglasses

    http://www.dressesforwomen.us Nike Shoes

    ReplyDelete
  25. Nice blog with having good information. It’s very useful for everyone. Thanks and keep posting this type of blog
    CCNA jobs in Hyderabad .

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. http://imova.vn/du-an-chung-cu-emerald-ct8-dinh-thon-my-dinh.html

    ReplyDelete
  28. This comment has been removed by the author.

    ReplyDelete
  29. Thống kê 1 số dự án như sau:chung cư hồng hà eco city chungcuhonghaecocity và cả dự án giá rẻ http://chungcugiagocvn.com/ chung cư giá gốc hà nội http://chungcuhanoixanh.net/ chung cư đường giải phóng http://www.chungcu360giaiphongs.com/ căn hộ giá rẻ long biên http://chungcuquanlongbien.com/ hãy tham khảo nhé

    ReplyDelete
  30. Sửa điện nước tại quận 1
    Sửa điện nước tại quận 2
    sửa điện nước tại quận 3
    sửa điện nước tại quận 4
    sửa điện nước tại quận 5
    Sửa điện nước tại quận 6
    Sửa điện nước tại quận 7
    Sửa điện nước tại quận 8
    Sửa điện nước tại quận 9
    Sửa điện nước tại quận 10
    Sửa điện nước tại quận 11
    Sửa điện nước tại quận 12
    Sửa điện nước tại quận Tân Bình
    Sửa điện nước tại quận Tân Phú
    Sửa điện nước tại quận Thủ Đức
    Sửa điện nước tại quận Bình Thạnh
    Sửa điện nước tại quận Gò Vấp
    sửa điện nước tại quận Phú Nhuận

    ReplyDelete
  31. Sửa máy bơm nước tại quận 1
    Sửa máy bơm nước tại quận 2
    Sửa máy bơm nước tại quận 3
    Sửa máy bơm nước tại quận 4
    Sửa máy bơm nước tại quận 5
    Sửa máy bơm nước tại quận 6
    Sửa máy bơm nước tại quận 7
    Sửa máy bơm nước tại quận 8
    Sửa máy bơm nước tại quận 9
    Sửa máy bơm nước tại quận 10
    Sửa máy bơm nước tại quận 11
    Sửa máy bơm nước tại quận 12
    Sửa máy bơm nước tại quận Tân Bình
    Sửa máy bơm nước tại quận Tân Phú
    Sửa máy bơm nước tại quận Thủ Đức
    Sửa máy bơm nước tại quận Bình thạnh
    Sửa máy bơm nước tại quận gò Vấp
    Sửa máy bơm nước tại quận phú nhuận
    Sửa máy bơm nước tại quận bình Tân
    Sửa máy bơm nước tại quận nhà bè

    ReplyDelete
  32. This comment has been removed by the author.

    ReplyDelete
  33. This comment has been removed by the author.

    ReplyDelete
  34. Via declaring your favorite estimate, You are investing in buy in my article from owner if you're the getting prospective buyer. You read and be in agreement with the worldwide postage and packing technique agreements clears in a brand-new windows because tabs. Signific penalty service fees up to now offered can be foreclosures change as soon as you strengthen you utmost put money balance..

    By Maglie Da Calcio a Poco Prezzo means of camisetas de futbol baratas putting forward their place a bet, You are investing in buy the item unit maillot foot 2018 from the owner if you're the being victorious in prospective buyer. You read and be in accordance the worldwide transporting Maillot De Foot Pas Cher provider affiliate agreement unwraps in an innovative home eyeport or perhaps case. Transfer court case during the past quotation are generally at the Maglie Calcio Poco Prezzo mercy of change should raise you amazon müller trikot max Maglie Poco Prezzo buy value..

    Just with putting forward the particular say, You are investing buy specific model from owner if you're the attracting prospective buyer. You read and be in agreement with the worldwide hummel trikots transport solution fine print unwraps in the most current Coach Outlet Online Store display or just hook. Scan expenditure until now cited are perhaps controlled by change in case maillot de foot pas cher you build you the best say selection.

    ReplyDelete
  35. http://get-spscripts.com/2011/06/removing-features-from-content-database.html?showComment=1528276025523#c3068607043372917297

    ReplyDelete
  36. http://get-spscripts.com/2011/06/removing-features-from-content-database.html?showComment=1528276025523#c3068607043372917297

    ReplyDelete
  37. This is a maybe a couple sentences that abridges the plot. It shows up at the highest point of the content scope page and is the primary eye getting subtle element the peruser sees. coverage script

    ReplyDelete
  38. It is a time consuming process to locate a legitimate, high-quality designer Michael Kors Handbags On Sale at a decent price. One brand name that is sought after is the Michael Kors Bags On Sale. Anywhere you see high demand you will find people out to make a quick buck.

    ReplyDelete
  39. It is a time consuming process to locate a legitimate, high-quality designer Michael Kors Handbags Clearance at a decent price. One brand name that is sought after is the Michael Kors Outlet Store. Anywhere you see high demand you will find people out to make a quick buck.

    ReplyDelete
  40. There are those who want to collect vintage items like Michael Kors Factory Outlet with genuine and high quality leathers from Michael Kors. To be able to grab a quality Michael Kors Bags Outlet item of your choice, make sure to be very careful in selecting what to buy, how to buy and where to buy the Michael Kors Outlet Online.

    ReplyDelete
  41. Thanks for your sharing! I did it. Anyone need help can contact me at: benh vien tham my

    ReplyDelete
  42. Bolsos Michael Kors Rebajas está por delante del paquete con el nombre de la firma de EE. UU. En la categoría de accesorios de lujo y ropa deportiva.
    Michael Kors Bolsos son premios codiciados, pero están disponibles en varios lugares.
    Bolsos Michael Kors Outlet comprado en su tienda tiene una garantía limitada de un año.
    Michael Kors Väska är identifierbar i både slankhet och elegans.
    Michael Kors Väska Rea betyder ett oerhört öga för smak såväl som stilighet.
    Michael Kors Väska Rea erbjuder den eleganta kvinnan ett utmärkt tillbehör att sticka ut.

    ReplyDelete
  43. Designer Exposure es un buen lugar para comprar su Bolsos Michael Kors original.
    The Woven Tote es también una selección impresionante en el Bolsos Michael Kors Baratos.
    Bolso de alta calidad que debe contemplar absolutamente un Bolsos Michael Kors Outlet.
    Du kommer att upptäcka en handfull detaljer som du kan förvänta dig att komma över på en vanlig Michael Kors Rea.
    Du kan hitta ett antal platser som ger Väska Michael Kors.
    Sortimentet är fantastiskt för alla som letar efter en MK Väska.
    Håll dina ögon öppna för den här säsongens val som kommer att presenteras under bara några månader och det kommer utan tvekan att bli spektakulärt.

    ReplyDelete