【问题标题】:How to extract PowerShell results to DataTable in vb.net如何将 PowerShell 结果提取到 vb.net 中的 DataTable
【发布时间】:2020-07-24 21:17:46
【问题描述】:

我想提取 PowerShell 输出并提取数据并存储在 VB.NET 中的 DataTable 中,我想在 GridView 中显示输出 我需要从 VB.NET 中的 PowerShell 输出中提取列和值

这是 PowerShell:

Get-ADComputer -Filter { OperatingSystem -NotLike '*Windows Server*'} -Property * | 
select Name, operatingSystem, LastLogonDate, Description, whenChanged | 
Where {($_.LastLogonDate -lt (Get-Date).AddDays(-90)) -and ($_.LastLogonDate -ne $NULL)}

这是 PowerShell 结果:

Name            : PCO37
operatingSystem : Windows 7 Professional
LastLogonDate   : 1/13/2020 10:04:23 AM
Description     :
whenChanged     : 1/17/2020 1:22:08 PM

Name            : PCO41
operatingSystem : Windows 7 Professional
LastLogonDate   : 2/10/2019 4:43:31 PM
Description     :
whenChanged     : 2/10/2019 4:47:05 PM

Name            : PCO40
operatingSystem : Windows 7 Professional
LastLogonDate   : 4/8/2019 10:03:38 PM
Description     :
whenChanged     : 4/11/2019 6:29:25 AM

Name            : PCO09
operatingSystem : Windows 7 Professional
LastLogonDate   : 3/6/2019 8:04:59 AM
Description     :
whenChanged     : 3/6/2019 8:09:39 AM

这是我在 VB.NET 中的代码:

Function PSObjectToDataTable(ByVal command As String) As DataTable

    ' Initialize PowerShell engine
    Dim Ps As PowerShell = PowerShell.Create()

    ' add the script the PowerShell command
    Ps.Commands.AddScript(command)

    ' Execute the script
    Dim results = Ps.Invoke()

    Dim dt As DataTable = New DataTable()
    dt.Columns.Add("Detail")
    Dim Name As String
    Dim CanonicalName As String
    For Each psObject As PSObject In results
        dt.Rows.Add(psObject.ToString)
    Next

    Return dt

End Function

这是 VB.NET 的结果:

Detail
@{Name=PC037; operatingSystem=Windows 7 Professional; LastLogonDate=1/13/2020 10:04:23 AM; Description=; whenChanged=1/17/2020 1:22:08 PM}   
@{Name=PC041;  operatingSystem=Windows 7 Professional; LastLogonDate=2/10/2019 4:43:31 PM; Description=; whenChanged=2/10/2019 4:47:05 PM}   
@{Name=PC040; operatingSystem=Windows 7 Professional; LastLogonDate=4/8/2019 10:03:38 PM; Description=; whenChanged=4/11/2019 6:29:25 AM}    
@{Name=PC009;  operatingSystem=Windows 7 Professional; LastLogonDate=3/6/2019 8:04:59 AM; Description=; whenChanged=3/6/2019 8:09:39 AM}

【问题讨论】:

  • 如果您希望将 PSObject 数组转换为 .Net 的 DataTable 类型,请查看ConvertTo-DataTable,在此处定义:powersnippets.com/convertto-datatable。如果您需要与VB.Net交互,请您分享用于调用PowerShell的VB.Net代码,或者多描述一下两种语言之间的交互?
  • 我需要一个代码来将数据从 PowerShell 输出提取到 VB.NET,我不需要 PowerShell 中的 DataTable

标签: powershell psobject


【解决方案1】:

终于我自己找到了答案。

要从 PowerShell 对象中提取属性,我必须指定项目名称,不能使用 PowerShell 对象属性的索引。

## 正确的语法##

psObject.Properties("Name").Value.ToString
psObject.Properties("CanonicalName").Value.ToString
psObject.Properties("operatingSystem").Value.ToString
psObject.Properties("LastLogonDate").Value.ToString
psObject.Properties("whenChanged").Value.ToString
psObject.Properties("Description").Value.ToString

## 语法错误##

psObject.Properties(0).Value.ToString
psObject.Properties(1).Value.ToString
psObject.Properties(2).Value.ToString
psObject.Properties(3).Value.ToString
psObject.Properties(4).Value.ToString
psObject.Properties(5).Value.ToString

代码如下(在Function: PSObjectToDataTable中动态创建列):

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim func As New clsFunction
    Dim command As String = "Get-ADComputer -Filter { OperatingSystem -NotLike '*Windows Server*'} -Property * | select Name, CanonicalName, operatingSystem, LastLogonDate, Description, whenChanged | Where {($_.LastLogonDate -lt (Get-Date).AddDays(-90)) -and ($_.LastLogonDate -ne $NULL)}"

    Dim arr As New ArrayList
    arr.Add("Name")
    arr.Add("CanonicalName")
    arr.Add("operatingSystem")
    arr.Add("LastLogonDate")
    arr.Add("whenChanged")
    arr.Add("Description")

    Me.GridView1.DataSource = func.PSObjectToDataTable(command, arr)
    Me.GridView1.DataBind()

End Sub

Function PSObjectToDataTable(ByVal command As String, ByVal arr As ArrayList) As DataTable

    ' Initialize PowerShell engine
    Dim Ps As PowerShell = PowerShell.Create()

    ' add the script the PowerShell command
    Ps.Commands.AddScript(command)

    ' Execute the script
    Dim results = Ps.Invoke()

    Dim dt As DataTable = New DataTable()
    Dim dr As DataRow = Nothing

    'Dynamic create cloumn
    For i = 0 To arr.Count - 1
        dt.Columns.Add(arr(i))
    Next

    For Each psObject As PSObject In results

        'Assign value to dynamic column
        dr = dt.NewRow()
        For i = 0 To arr.Count - 1

            'Check if object is null
            If psObject.Properties(arr(i)).Value Is Nothing Then
                dr(i) = ""
            Else
                dr(i) = psObject.Properties(arr(i)).Value.ToString
            End If

        Next
        dt.Rows.Add(dr)
    Next

    Return dt

End Function

【讨论】:

  • 我正在开发一个为你包装所有这些东西的库。我会在接下来的几天里开源它。
  • @Bhaal22 - 你做过你提到的库吗?
  • @DarrenRose 很好的提醒!在过去的几个月里,我有太多事情要做,但我会努力的。快到了。
  • @Bhaal22 - 非常好 - 期待在适当的时候听到更多消息
【解决方案2】:

我想我也会在这里拍摄一个例子。我尝试使其尽可能动态,包括字段类型。 Int32 字符串等。 温柔,这是我第一次在这里发帖。

Imports System.Management.Automation

Function PSObjectToDataTable(ByVal command As String) As DataTable

        ' Initialize PowerShell engine
        Dim Ps As PowerShell = PowerShell.Create()

        ' add the script the PowerShell command
        Ps.Commands.AddScript(command)

        ' Execute the script
        Dim results = Ps.Invoke()
        Dim dt As DataTable = New DataTable()
        Dim x As Integer = 0
        For Each psObject As PSObject In results
            If x = 0 Then
                For Each prop As PSPropertyInfo In psObject.Properties
                    dt.Columns.Add(prop.Name, prop.Value.GetType)
                Next
            End If
            Dim dtrow As DataRow
            dtrow = dt.NewRow
                For Each prop As PSPropertyInfo In psObject.Properties
                    Try
                        dtrow(prop.Name) = prop.Value
                    Catch ex As Exception
                        'do some debugging in here if you want to
                    End Try
                Next
                dt.Rows.Add(dtrow)
            x = x + 1
        Next
        Return dt

End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2016-07-15
    • 1970-01-01
    相关资源
    最近更新 更多