Passing a variable from C # to PowerShell does not work

I want to pass a C # object to PowerShell so that I can provide the main user interface with status updates. I saw several posts about this on SO, but none of them seem to help in solving my problem.

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

And in the PowerShell script, I want to access the LogReporter (base type:) System.Windows.Window, as shown in the snippet below

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

However, all I have is

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)

I tried to list all the members of the $ LogReporter variable, but the result

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

This means a variable $LogReporter null. Now questions: why and how to fix it?

Remember that the code has been simplified for better readability, but shows the necessary and faulty parts.

Any ideas on what's going wrong? Should I register my type LogReportersomehow?

!

, # , PowerShell script. .

+4
1

, . LogReporter, "$ _. InstanceStatus.ToString()". InstanceStatus null , ToString() . ToString(), "0 " 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);
    }
}
+1

Source: https://habr.com/ru/post/1606445/


All Articles