将变量从C#传递到PowerShell不起作用

时间:2015-09-08 15:34:52

标签: c# powershell

我想将一个C#对象传递给PowerShell,这样我就可以为主UI提供状态更新。我在SO上看过几篇有关此事的帖子,但似乎没有一篇文章能帮助解决我的问题。

Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
runspace.Open();

runspace.SessionStateProxy.SetVariable("LogReporter", this.LogReporter);

Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(script);

StringBuilder builder = new StringBuilder();
Collection<PSObject> objects = pipeline.Invoke();

在PowerShell脚本中,我想访问LogReporter(基本类型:System.Windows.Window),如下面的代码段所示

$RunningInstances= @(Get-ChildItem | Where-Object { $_.InstanceStatus.ToString() -eq "Running" })
$LogReporter.ReportProgress($RunningInstances.Count.ToString() + " instances running currently...")

然而,我得到的只是

You cannot call a method on a null-valued expression.

   at CallSite.Target(Closure , CallSite , Object , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

我试图列出$ LogReporter变量的所有成员,但结果是

No object has been specified to the get-member cmdlet.

这意味着,$LogReporter变量为null。现在的问题是:为什么以及如何解决这个问题?

请注意,为了更好的可读性,代码略有简化,但显示了必要和失败的部分。

关于出了什么问题的任何想法?我是否必须以某种方式注册我的LogReporter类型?

我感谢任何帮助!

解决方案

我有一个拼写错误,C#代码中的变量与PowerShell脚本中的变量不匹配。我的代码示例没有任何问题。

1 个答案:

答案 0 :(得分:1)

这是完整的工作示例,向您展示问题所在。问题不在于LogReporter变量,而在于&#34; $ _。InstanceStatus.ToString()&#34;部分。在您的情况下,InstanceStatus为null,ToString()抛出上面的异常。在下面的代码中,我刚刚删除了ToString()调用,你看到&#34; 0个实例正在运行&#34;来自LogReporter的消息。

class Program
{
    private static void Main(string[] args) {
        new ScriptRunner().Run();   
        Console.ReadKey(); 
    }        
}

class ScriptRunner {
    public ScriptRunner() {
        this.LogReporter = new LogReporter();
    }

    public void Run() {
        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
        runspace.Open();



        Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript("$RunningInstances=@(Get-ChildItem | Where-Object {$_.InstanceStatus -eq \"Running\"})");
        runspace.SessionStateProxy.SetVariable("LogReporter", this.LogReporter);
        pipeline.Commands.AddScript("$LogReporter.ReportProgress($RunningInstances.Count.ToString()+\" instances running currently...\")");

        StringBuilder builder = new StringBuilder();
        Collection<PSObject> objects = pipeline.Invoke();            
    }

    public LogReporter LogReporter { get; private set; }

}

class LogReporter
{
    public void ReportProgress(string msg)
    {
        Console.WriteLine(msg);
    }
}