1

Context of Problem: I have a projects list in SP2013 root site collection, when a user creates project item. I have a powershellscript.ps1 file which will carry out following 1. Create subsites 2. Set Masterpages,3. Remove groups 4. set new groups etc..

How to call the powershellscript.ps1 after project item is saved?

I have tried the following does not work because spserver is 2013 http://ilovesharepoint.codeplex.com/wikipage?title=Execute%20PowerShell%20Script%20Action

I have SP2013 hence the wsp does not work well in 2013. Any possible solutions is appreciated. Thanks.

  • Why not use a event receiver on Project list and do it through code better and safe way. – Akshay Randive Jul 29 '15 at 5:32
0

You could schedule a task in windows server to execute the PowerShell script on given intervals. Check if there are new entries in the list and if there are create the new Sub site. Then it could be a good idea to have a flag on each item which is set by the script to indicate that you already created a sub site for that entry and don't need to check all entries every time the script runs.

The account running the batch job must have the right permissions to execute powershell on the SP farm. So the account running the task needs to be added as ShellAdmin with the Add-SPShellAdmin command. Add-SPShellAdmin

Has to have the permission to Log on ad batch job Configuring User Rights

Has to have permission to do operations in SharePoint.

When adding a Action to the Task you are creating input PowerShell.exe in the Program/script: box. and for Add argument &'c:\<path to your script>.ps1'

You probably also want to check the checkbox "Run whether user is Logged on or not" else the Task will only run when anyone is logged on to the server.

  • That's a good idea, I might have to do that way. if Its too hard to call powershell from workflow . – mahesh pullareddy Jul 29 '15 at 3:23
0

I figured out how to call custom powershell functions in C# on ItemAdded Event for the list. The Runscript is the function which gets called and it points to powershell createprojectsubsites.ps1 on the local folder of the server.

    public override void ItemAdded(SPItemEventProperties properties)
    {            
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {   using (SPWeb web = properties.OpenWeb())
            {  try
                {
                    properties.ListItem["Action"] = RunScript("CreateProjectSubSite '" + properties.ListItem["SiteTitle"].ToString() + "' '" + properties.ListItem["SiteUrl"].ToString() + "'");
                    properties.ListItem.Update();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        });
    }

   public string RunScript(string scriptText)
    {
        string psScript = "C:\\Work\\PowerShellScripts\\CreateProjectSubsite.ps1";
        if (File.Exists(psScript))
        {
            psScript = File.ReadAllText(psScript);
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript("Add-PSSnapin Microsoft.SharePoint.Powershell");
            pipeline.Commands.AddScript(psScript);
            pipeline.Commands.AddScript(scriptText);
            Collection<PSObject> results = pipeline.Invoke();
            runspace.Close();
            return "Site Created.";
        }
        else
            return "Site is NOT created as powershell script file is missing.";
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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