如何将 PowerShell 结果提取到 vb.net 中的 DataTable
How to extract PowerShell results to DataTable in vb.net
我想提取 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}
最后我自己找到了答案
要从 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
这是代码(在函数中动态创建列: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
我想我也会在这里拍一个例子。我试着让它尽可能动态,包括字段类型。 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
我想提取 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}
最后我自己找到了答案
要从 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
这是代码(在函数中动态创建列: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
我想我也会在这里拍一个例子。我试着让它尽可能动态,包括字段类型。 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