加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:20:01 所属栏目:百科 来源:网络整理
导读:我想将一个C#对象传递给Power Shell,这样我就可以为主UI提供状态更新.我在SO上看过几篇关于此的帖子,但它们似乎都没有帮助解决我的问题. Runspace runspace = RunspaceFactory.CreateRunspace();runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
我想将一个C#对象传递给Power Shell,这样我就可以为主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脚本中的变量不匹配.这里的代码示例没有任何问题.

解决方法

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

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);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读