3

I am attempting to programmatically launch the SharePoint Management Shell from C# code and I am getting different behavior that when I select it from the Start Menu. I have attempted to do this two different ways with the same result:

Attempt #1:

var startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
startInfo.FileName = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\PowerShell.exe";
startInfo.Arguments = "-NoExit \"  & 'C:\\Program Files\\Common Files\\Microsoft Shared\\Web Server Extensions\\14\\CONFIG\\POWERSHELL\\Registration\\sharepoint.ps1' \"";
Process.Start(startInfo);

Attempt #2:

var startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
startInfo.FileName = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Microsoft SharePoint 2010 Products\\SharePoint 2010 Management Shell.lnk";
Process.Start(startInfo);

Both of these methods launch a PowerShell window and display the following error message:

File C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CO NFIG\POWERSHELL\Registration\SharePoint.ps1 cannot be loaded because the execut ion of scripts is disabled on this system. Please see "get-help about_signing" for more details. At line:1 char:3 + & <<<< ' C:\Program Files\Common Files\Microsoft Shared\Web Server Extensio ns\14\CONFIG\POWERSHELL\Registration\sharepoint.ps1 ' + CategoryInfo : NotSpecified: (:) [], PSSecurityException + FullyQualifiedErrorId : RuntimeException

When I launch it from the Start Menu, it works just fine. Any ideas on why this behaves differently? In Attempt #1 I am running the same executable and arguments as the Link. In Attempt #2 I am trying to run the link itself.

Thanks!

1 Answer 1

2

This problem is usually caused by x86 and x64 PowerShell each requiring ExecutionPolicy to be set to something other than Restricted in order to run scripts. Perhaps your PowerShell console is x64 and your C# program is running as x86 or vice-versa. Either way, pass in an additional parameter to PowerShell -ExecutionPolicy ByPass to skip past this particular problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant! The x86 vs x64 was the issue. I added your flag and also set my application to compile to x64 (which was necessary anyway) and voila, it works. I'll mark as answer in a few minutes when it lets me. Thanks!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.