I would like to extract the PowerShell output and extract the data and store in DataTable in VB.NET, I want to display the output in a GridView I need extract the column and value from the PowerShell output in VB.NET
Here is the 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)}
Here is the PowerShell result:
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
Here is my code in 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
Here is the result of the 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}