0

I'm a beginner to powershell. I have a custom cmdlet I created. This code will break at the last line:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
PSSnapInException psEx = null;
runspaceConfiguration.AddPSSnapIn(mySnapin, out psEx); 
using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
    runspace.Open(); // breaks here.

It gives me Access to registry is not allowed error, unless I enclose the above code inside:

SPSecurity.RunWithElevatedPrivileges(delegate()
{

});

To access the SPSecurity, I have to of course bring in my references: Microsoft.Sharepoint. It actually works, but my powershell code has nothing to do with sharepoint. Is there a more general powershell security dll that I can use instead of the SPSecurity (even though it works)? I can't find one. thanks.

user1019042
  • 2,428
  • 9
  • 43
  • 85
  • Is your snapin registered under the 32-bit or 64-bit hive? What bitness does your C# program run as? – Keith Hill Nov 15 '13 at 22:59
  • the snapin is powershell 3.0 (64 bit) and caller of the powershell is a web api service written in C# v 4.5 (64 bits). – user1019042 Nov 16 '13 at 03:18
  • I haven't fired up Reflector to actually look at what RunWithElevatedPrivileges does but it is my understanding it just changes the security context for SharePoint calls to be an account with site collection admin rights. I can't imagine it has much effect outside of the SharePoint object model. I am surprised by your results. – Goyuix Nov 16 '13 at 18:16

0 Answers0