In the example
only one item in the collection is passed back to you. You access this collection entry by specifying its
index in the collection [0] . At this point you are looking at a PSObject and because the PSObject cannot
be cast to a ScriptBlock object, another solution is needed. It just happens that the PSObject class has a
BaseObject attribute, which is of type Object . An instance of type Object can be cast to an instance of
type ScriptBlock. Now that the ScriptBlock has been properly cast you can use this in the pipeline as the
value for the filterscript parameter, as you can see in the following code sample:
RunspaceConfiguration EMSConfig =
RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info =
EMSConfig.AddPSSnapIn(???Microsoft.Exchange.Management.PowerShell.Admin???,
out snapInException);
Runspace EMSRunSpace =
RunspaceFactory.CreateRunspace(EMSConfig);
EMSRunSpace.Open();
491
Chapter 17: Using the .NET Framework
Pipeline pipeLine = EMSRunSpace.CreatePipeline();
Collection < PSObject > cmdData = pipeLine.Invoke();
foreach (PSObject cmdlet in cmdData)
{
string cmdletnm =
cmdlet.Properties[???Name???].Value.ToString();
TextBox1.Text += cmdletnm.ToString() + ???\r\n???;
}
pipeLine=null;
To see the data returned from the PowerShell runspace, add a text box control named TextBox1 and a
button control that runs the preceding code to Default.
Pages:
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684