1

I need to excecute a PowerShell script from a ListEventReceiver under special User credentials. I found the Runspace which is working very fine to excecute scripts but how to make a secure (no passwords in the code) "run as"?

Pipeline

string cmdArg = "C:\\Users\\Administrator\\Documents\\userExist.ps1 " + mailstring;
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();

pipeline.Commands.AddScript(cmdArg);
pipeline.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
Collection<PSObject> results = pipeline.Invoke();
var error = pipeline.Error.ReadToEnd();
runspace.Close();

I read about secure store service but I think it is only for whole services, pages or lists and can't be used for single functions?

Does any one have an idea how to solve this problem?

3
  • 1
    Do you have a compelling reason to not just perform whatever the powershell script does in C# code? Most of what you can do in powershell you can do directly in C#, and within C# privilege escalation is easy enough.
    – Servy
    Commented Jun 11, 2012 at 13:50
  • @Servy there are huge powershellscripts that already exist, so I would prefer to use them.
    – HW90
    Commented Jun 11, 2012 at 15:29
  • As far as storing secrets, the web.config connectionStrings section offers SharePoint code running in the context of a web application one very convenient option. It's designed for connection strings, but ultimately it's just a key/value store with built in encryption. Commented Jul 31, 2012 at 15:50

1 Answer 1

0

Regarding running the script under another identity, have you tried RunWithElevatedPrivileges yet? If this doesn't do the trick it might be worth considering granting the application pool's account sufficent permisisons to complete the desired tasks.

2
  • Yes I already tried to RunWithElevatedPrivileges but with out success. The applications pool account can't get that permissions in ours environment.
    – HW90
    Commented Jun 21, 2012 at 6:38
  • It sounds to me as if this boils down to the more generic "how do I run my code as user X?" This post addresses that question: stackoverflow.com/questions/1168571/… Commented Jul 31, 2012 at 15:46

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.