当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Runspace01 (VB.NET) 代码示例

作者:精品下载站 日期:2024-12-14 02:27:57 浏览:12 分类:玩电脑

Runspace01 (VB.NET) 代码示例


以下是创建运行指定命令的控制台应用程序中描述的运行空间的代码示例。为此,应用程序调用运行空间,然后调用命令。 (请注意,此应用程序不指定运行空间配置信息,也不显式创建管道。)调用的命令是 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

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯