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. .