【发布时间】: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