Thursday, May 23, 2013

Run a SharePoint PowerShell Script from Task Scheduler


היי חברים,
היום נתקלתי בבעיה, לא הצלחתי להריץ Script שכתבתי ב-PowerShell בעזרת Task Scheduler של מערכת ההפעלה. ה-Script נכתב בסביבה SharePointית והורץ כבדיקה דרך SharePoint 2010 Management Shell, זהו בעצם Cmdlet שמאפשר הרצה של Scriptים שנכתבו ב-PowerShell בסביבת SharePoint (לצורך העניין ה-Cmdlet של PowerShell שהוא לא SharePointי מופעל בנפרד, ראה תמונה )


כדי להתגבר על הבעיה מה שצריך לעשות הוא להוסיף שורת קוד (השורה הראשונה ב-Script) ת "Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue"  ורק לאחר מכן לבנות Task שירוץ אוטומטית. המשמעות של השורה הזו היא שאנחנו מוסיפים ל-PowerShell של מערכת ההפעלה (הרגיל) את ה-DLL של SharePoint PowerShell כלומר, מאפשרים הרצת SharePoint Scripts דרך ה-PowerShell הרגיל.



דוגמא :




פתח את ה- Task Scheduler וצור משימה חדשה


מלא את הפרטים הבאים :



הוסף תזמון למשימה (מתי היא תרוץ)
ובלשונית הפעולות הזן את הפרטים הבאים
בשדה "Program/script" הזן את התוכנה שתריץ את ה-Script ובשדה "Add arguments (optional) הוסף את המיקום של קובץ ההרצה בתוספת התו & :
 &'C:\scripts\MyScript.PS1'



לבסוף, הגדירו את שאר הפרמטרים בלשוניות האחרונות עפ"י הצורך ולחצו OK.
הריצו את ה-Script כדי לראות שהפקודות עובדות.

בהצלחה !
רון נס.

 ============================================================================================================================================================================================





Hello Friends,
Today I dealt with a problem … I could not run a script written in PowerShell using Task Scheduler. The script was written in SharePoint environment and was run through the "SharePoint 2010 Management Shell". SharePoint 2010 Management Shell is basically Cmdlet allow the running of scripts written in Sea PowerShell SharePoint environment


To overcome this problem you need to add a line of code (the first line in the script)      "Add-PSSnapin Microsoft.SharePoint.PowerShell-erroraction SilentlyContinue" and then build a Task to run automatically. The meaning of this line is that we let to the regular PowerShell run with PowerShell SharePoint's DLL, This Action allow SharePoint running scripts via the regular PowerShell.



Example:


Open the Task Scheduler and create a new task



Fill out the following information:



Add to scheduling task (when it will run( And in the Actions tab enter the following information :
In the "Program / ​​script" Enter the Application that run the script and in the "Add arguments (optional) Add the location of the executable file plus the & character:
  & 'C: \ scripts \ MyScript.PS1'



Finally, fill the other parameters in the tabs according to the needed and click OK.
Run the script to see the commands work.

Good Luck   :)
Ron Ness.

No comments:

Post a Comment