Наконец я нашел ответ самостоятельно.
Чтобы извлечь свойства из объекта PowerShell, мне нужно указать c имя элемента, не может использовать индекс свойства объекта 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
Вот код (Dynami c создать столбец в функции: 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