I'm trying to find out how the RunspaceConfiguration.Create()
method call is used. I want to set the c# hosting up, in a manner that will enable any conceivable powershell script from executing from c# by ensuring all Cmdlets, Providers etc, are available. Looking at the powershell samples, sample 5, it's got the following.
RunspaceConfiguration config = RunspaceConfiguration.Create("SampleConsole.psc1", out warnings);
How does the .psc1 get created, or where is stored, retrieved from. Any help would be appreciated.
I had to do some evil hackery to get this to happen - you can read it in the comments, but basically PS can't do this currently.
var _currentRunspace = RunspaceFactory.CreateRunspace(this);
/* XXX: We need to enable dot-sourcing - unfortunately there is no
* way in code to just enable it in our host, it always goes out to
* the system-wide settings. So instead, we're installing our own
* dummy Auth manager. And since PSh makes us reimplement a ton of
* code to make a custom RunspaceConfiguration that can't even properly
* be done because we only have the public interface, I'm using
* Reflection to hack in the AuthManager into a private field.
* This will most likely come back to haunt me. */
var t = typeof(RunspaceConfiguration);
var f = t.GetField("_authorizationManager", BindingFlags.Instance | BindingFlags.NonPublic);
f.SetValue(_currentRunspace.RunspaceConfiguration, new DummyAuthorizationManager());
_currentRunspace.Open();
return _currentRunspace;
public class DummyAuthorizationManager : AuthorizationManager
{
const string constshellId = "Microsoft.PowerShell";
public DummyAuthorizationManager() : this (constshellId) {}
public DummyAuthorizationManager(string shellId) : base(shellId) {}
protected override bool ShouldRun(CommandInfo commandInfo, CommandOrigin origin, PSHost host, out Exception reason)
{
// Looks good to me!
reason = null;
return true;
}
}
A .psc1
file can be created with the Export-Console
cmdlet. Typically you would set up a console environment with your desired snap-ins and then export its configuration.
RunspaceConfiguration.Create
most likely supports either absolute or relative paths to such a file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.