代码之家  ›  专栏  ›  技术社区  ›  scope_creep

c#动力壳。使用R配置运行空间取消空间配置。创建

  •  0
  • scope_creep  · 技术社区  · 15 年前

    我想知道 RunspaceConfiguration.Create() 使用方法调用。我想设置c#宿主,通过确保所有cmdlet、提供程序等都可用,使任何可能的powershell脚本都能从c#执行。看看powershell示例,示例5,它有以下内容。

    RunspaceConfiguration config = RunspaceConfiguration.Create("SampleConsole.psc1", out warnings);
    

    如何创建.psc1,或者从何处存储和检索.psc1。任何帮助都将不胜感激。

    2 回复  |  直到 11 年前
        1
  •  3
  •   Ana Betts    15 年前

    我不得不做一些 黑客让这发生-你可以阅读它的评论,但基本上PS目前不能这样做。

    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;
        }
    }
    
        2
  •  2
  •   dahlbyk    15 年前

    A .psc1 Export-Console cmdlet。通常,您会使用所需的管理单元设置控制台环境,然后导出其配置。

    RunspaceConfiguration.Create 很可能支持指向此类文件的绝对或相对路径。