0

I have some ps1 that I need to run it with SharePoint management shell. I dont have the possibility to run it with the traditionnal powershell even i havce the administrator role :

  public string Runps1()
    {
        try
        {
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            using (PowerShell powerShell = PowerShell.Create())
            {
                powerShell.Runspace = runspace;
                powerShell.AddCommand("Set-ExecutionPolicy").AddArgument("Unrestricted");
                powerShell.AddScript(Environment.CurrentDirectory + @"\Data_tier\PS1\STSadm_PreUpgrade.ps1");

                StringBuilder stringBuilder = new StringBuilder();

                foreach (PSObject obj in powerShell.Invoke())
                {
                    stringBuilder.AppendLine(obj.ToString());

                }


                runspace.Close();

                return stringBuilder.ToString();
            }
        }
        catch (Exception exp) { throw; }
    }

I know that this code is for traditionnal Power Shell , There is any idea for SharePoint management shell.

3

You could add PowerShell snap-in in you .ps1 file. But in this case you should have some permissions to sql and SharePoint

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
  • thanks Andrey ,It works for me , but I just can inderstande the case of permission , would you please explaine more . – Imen Turki Apr 11 '14 at 13:52
  • In my case I use this scenario to get data from ULS logs and the user in this case should be local admin and get access to temp db in sql where SharePoint save temp data from ULS logs when PowerShell command run. In many cases when you get some data from SharePoint Using PowerShell command you should have some permission on sql or sharepoint. That's why I mentioned this. – Andrey Ungur Apr 11 '14 at 14:21

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.