I'm trying to run a PowerShell script from a C# application and I need the script to run when my C# app is running as a non-admin user (e.g. Network Service or some other domain account).
Previously, I was using the following code:
using (RunspaceInvoke invoker = new RunspaceInvoke())
{
// load the powershell module
invoker.Invoke("Import-Module MyModule");
// run the cmdlet defined in the module
invoker.Invoke("MyCmdlet");
}
I don't know whether this is the best approach for running cmdlets defined in a module (please teach me if there's a better way!). In any case, this works perfectly if I'm running as an administrative user. I tried running this as Network Service, however, and I got an unfriendly error in the constructor of RunspaceInvoke:
Requested registry access is not allowed.
Is there a way I can run my PowerShell cmdlets as a non-elevated user such as Network Service? I do not need or desire to access the registry. The cmdlet itself also does not require elevated privileges.