2009-06-23 37 views
0

私はRunspaceConfiguration.Create()メソッド呼び出しがどのように使われているかを調べようとしています。すべてのCmdlet、Providersなどを利用できるようにすることで、考えられるPowerShellスクリプトをC#から実行できるように、##をホストするように設定したいと考えています。 Powershellサンプル(サンプル5)を見ると、次のようになります。c#Powershell。 RunspaceConfiguration.Createを使ってrunspaceを設定する

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

どのように.psc1が作成されるか、またはどこから格納されるかが取得されます。どんな助けもありがとう。

答えて

3

これを行うには、evilハッカーをしなければなりませんでした。あなたはコメントで読むことができますが、基本的に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; 
    } 
} 
+0

乾杯。 それは私が知りませんでしたその1つの問題を修正しました。ありがとう。ボブ。 –

2

.psc1ファイルがExport-Consoleコマンドレットを使用して作成することができます。通常は、目的のスナップインを使用してコンソール環境をセットアップし、その構成をエクスポートします。

RunspaceConfiguration.Createは、このようなファイルへの絶対パスまたは相対パスのいずれかをサポートしている可能性があります。

関連する問題