ตัวอย่างโค้ด Runspace01 (VB.NET)
ต่อไปนี้เป็นตัวอย่างโค้ดสำหรับรันสเปซที่อธิบายไว้ในการสร้างแอปพลิเคชันคอนโซลที่รันคำสั่งที่ระบุ เมื่อต้องการทำเช่นนี้ แอปพลิเคชันจะเรียกใช้ runspace จากนั้นจึงเรียกใช้คำสั่ง (โปรดทราบว่าแอปพลิเคชันนี้ไม่ได้ระบุข้อมูลการกำหนดค่า runspace และไม่ได้สร้างไปป์ไลน์อย่างชัดเจน) คำสั่งที่ถูกเรียกใช้คือ Get-Process cmdlet
ตัวอย่างโค้ด
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Management.Automation
Imports System.Management.Automation.Host
Imports System.Management.Automation.Runspaces
Namespace Microsoft.Samples.PowerShell.Runspaces
Module Runspace01
' <summary>
' This sample uses the RunspaceInvoke class to execute
' the get-process cmdlet synchronously. The name and
' handlecount are then extracted from the PSObjects
' returned and displayed.
' </summary>
' <param name="args">Unused</param>
' <remarks>
' This sample demonstrates the following:
' 1. Creating an instance of the RunspaceInvoke class.
' 2. Using this instance to invoke a PowerShell command.
' 3. Using PSObject to extract properties from the objects
' returned by this command.
' </remarks>
Sub Main()
' Create an instance of the RunspaceInvoke class.
' This takes care of all building all of the other
' data structures needed...
Dim invoker As RunspaceInvoke = New RunspaceInvoke()
Console.WriteLine("Process HandleCount")
Console.WriteLine("--------------------------------")
' Now invoke the runspace and display the objects that are
' returned...
For Each result As PSObject In invoker.Invoke("get-process")
Console.WriteLine("{0,-20} {1}", _
result.Members("ProcessName").Value, _
result.Members("HandleCount").Value)
Next
System.Console.WriteLine("Hit any key to exit...")
System.Console.ReadKey()
End Sub
End Module
End Namespace
ดูเพิ่มเติม
Windows PowerShell SDK