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

标签 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 脚本中的变量不匹配。我这里的代码示例没有任何问题。

最佳答案

这是向您展示问题的完整工作示例。问题不在 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);
    }
}

关于c# - 将变量从 C# 传递到 PowerShell 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32461817/

相关文章:

Powershell 在 reg 导入时抛出终止错误,但操作成功完成

c# - 将字符串分隔到不在集合字典中的列表中

c# - 圆内的随机点

powershell - 正在处理从Invoke-Command返回的数据吗?

sql-server-2008 - 为什么 sqlpsx 不包含在 SQL Server 本身中?

regex - PowerShell Regex获取2个字符串之间的子字符串

c# - 如何使用 WebClient.DownloadData(到本地 DummyPage.aspx)

c# - 如何检测鼠标是否在整个表单和子控件内?

c# - 如何从 ADFS 获取 SAML 断言中的非空收件人属性

loops - 我无法让 PowerShell 正确转换为 int