Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
+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. –  Anders Rask May 5 '11 at 13:55
    
Yes +1 for noting that not everything is a nail and needs a hammer. –  MichaelF May 5 '11 at 14:00
add comment

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.

share|improve this answer
    
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 '11 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 '11 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. –  PirateEric May 5 '11 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 '11 at 13:25
    
Thanks to all ... True as suggested by SPDoctor due to scalability reasons, we are inclined towards timer job –  JeeZ May 6 '11 at 4:53
show 1 more comment

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.

share|improve this answer
    
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# –  Anders Rask May 5 '11 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. –  Tim Ebenezer May 6 '11 at 6:47
add comment

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...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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