从 vb.net 中执行 Powershell 命令 "Get-Volume"

Executing the Powershell command "Get-Volume" from within vb.net

使用 Powershell ISE,我 运行 "Get-Volume" 命令并产生以下结果。

Header:

Drive FileSystemLabel FileSystem DriveType HealthStatus SizeRemaining Size

详情:

C     NTFS            Fixed                Healthy      18.88 GB      59.4 GB

现在我想从 vb.net 程序中提取 SizeRemaining 和 Size。

运行 下面的代码在调试中传递 "Get-Volume" 命令,collection 变量 cResults 包含 1 个条目:

(新 System.Collections.Generic.Mscorlib_CollectionDebugView(属于 System.Management.Automation.PSObject)(cResults))。项目(0)

{MSFT_Volume (ObjectId = "\?\Volume{8a68dbb9-122c-4890-a81e-bf70...)}

如何从中获取 SizeRemaining 和 Size (18.88 GB 59.4 GB)?

注意:如果我 运行 代码传入 "Get-Process",collection 变量 cResults 包含 43 System.Diagnostics.Process 个条目:

(新 System.Collections.Generic.Mscorlib_CollectionDebugView(属于 System.Management.Automation.PSObject)(cResults))。项目(0)

{System.Diagnostics.Process(主机)}

在这种情况下,我能够钻取到 BaseObject 并使用 "myProcess." 代码行获取该命令的各个结果字段。

  RunScript("Get-Volume", diskViewData, StrMessage)


  Sub RunScript(ByVal scriptText As String, ByRef byRefDiskViewData As DiskViewData, ByRef byRefMessage As String)

    Dim runspace As Runspace

    Dim diskViewDataList As New DiskViewData

    Dim myProcess As System.Diagnostics.Process

    Try
        runspace = RunspaceFactory.CreateRunspace()

        Try
             runspace.Open()

            Dim pipeline As Pipeline = runspace.CreatePipeline()

            pipeline.Commands.AddScript(scriptText)

            ' Execute the script and add put result in a collection.
            Dim cResults As Collection(Of PSObject) = pipeline.Invoke()

            runspace.Close()

            If cResults.Count > 0 Then
                For Each obj As PSObject In cResults
                    Dim diskViewData As New DiskViewData

                    myProcess = obj.BaseObject

                    'diskViewData.Total_Size = (myProcess.?)
                    'diskViewData.Remaining_Space = (myProcess.?)

                Next
            Else
                byRefMessage = "Empty object returned after runnng the powershell command."
            End If
        Catch ex As Exception
            byRefMessage = "Failing AFTER create of runspace. " & ex.Message
        End Try
    Catch ex As Exception
        byRefMessage = "Failing at create of runspace. " & ex.Message
    End Try
End Sub

如果我对你的问题的理解正确,可以使用 Properties collection of the PSObject class 轻松获取结果,如下所示:

' create PowerShell instance for the specified command
dim ps as PowerShell = PowerShell.Create().AddCommand("Get-Volume")
' iterate over the result
for each result as PSObject in ps.Invoke()
    ' results only for drive C
    if (result.Properties("DriveLetter").Value = "C") then
        ' access property using the Property properties collection
        Console.WriteLine("{0} - Total: {1}; Remaining: {2}", result.Properties("DriveLetter").Value, result.Properties("Size").Value, result.Properties("SizeRemaining").Value)
    end if
next result

结果是(您可以使用除以 1024 来格式化它以获得 KB / MB / GB):

C - Total: 1057254976; Remaining: 38872832