2

Is it possible to call a powershell script from a sharepoint timer job ? or are there any security issues involved in doing so?

Regards, Jeevan

4 Answers 4

3

There are a couple of ways of calling PowerShell from within code (e.g. within a timer job):

Run PowerShell Script using automation interface.

Run PowerShell using command line.

You have to give some thought to where the timer job is running, if you have a farm with multiple servers. The timer job normally runs as the farm account. You will also need to load up the SharePoint snap-in.

I can't see much technical advantage in doing this as everything you can do in PowerShell can be achieved using the object model. However, you may already have script written, or you might want to use a PowerShell cmdlet that would need to be re-implemented programmatically. I don't think it is completely unreasonable to do this, depending on what you are trying to achieve.

2
  • +1 for not doing it. I did some BPOS calls with a custom host that called PowerShell from a web part, but that was a business need and there was no other options since calling the web service directly was not supported. Careful of thinking PowerShell is a golden hammer that is good for everything. May 5, 2011 at 13:55
  • Yes +1 for noting that not everything is a nail and needs a hammer.
    – MichaelF
    May 5, 2011 at 14:00
3

You can do it from a scheduled task, we do that for certain administrative tasks. Do you really need this to happen as a Timer Job from SharePoint or wouldn't a Scheduled Task do just as well? I don't see the need for a Timer Job since you can set the same sort of schedule as a Scheduled Task.

6
  • In a simple situation this is certainly an easier option. The disadvantage of a Windows Scheduled Task is that it is outside the scope of the SharePoint deployment and management infrastructure. This becomes increasingly important as you scale out your farm.
    – SPDoctor
    May 5, 2011 at 12:37
  • True, but it depends on what the need is. But I also don't see much administrative overhead in maintaining tasks across the machines when they are all the same. We've automated and scripted setting these up when its appropriate. It's all preference.
    – MichaelF
    May 5, 2011 at 13:06
  • Both of you make valid points, I think it JeeZ clarifies the intent, the better option might present itself. I would lean towards the Scheduled Task route if its something administrative like a backup. If its something that might do some sort of nightly list processing, then I'd lean towards a Timer Job. May 5, 2011 at 13:12
  • The questioner asks if it is possible to run PowerShell from a timer job - so strictly the answer is "yes". But you have to wonder why, starting from a script anyway, you wouldn't just schedule it at the OS level. So, yes, Michael has a valid point (+1).
    – SPDoctor
    May 5, 2011 at 13:25
  • Thanks to all ... True as suggested by SPDoctor due to scalability reasons, we are inclined towards timer job
    – JeeZ
    May 6, 2011 at 4:53
0

A timer job would usually be written in a .NET language and would therefore not have access to Powershell as such. There are some bodges you could use to do this, but it doesn't sound like a good idea.

2
  • its not a problem calling PowerShell from C# using custom hosts, but I agree its hard to see why you should prefer it over C# May 5, 2011 at 13:57
  • @Anders; agreed - this is what I was trying to get across. And calling of cmdlets is a bit of a pain, I find, compared to simply writing the code that would take a similar effect. May 6, 2011 at 6:47
0

What about scheduling a timer job to run this one?

Update-SPProfilePhotoStore –CreateThumbnailsForImportedPhotos 1 –MySiteHostLocation http://mysitehost/my

Seems like that should already run on some sort of a schedule.... doesn't appear to though...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.