我正在尝试使用以下代码获取用户的上次登录日期。我的问题是将来自powershell变量$ MailboxData的powershell结果转换为VB.net变量。
displayname = obj.Members("alias").Value.ToString
行不会返回任何错误。
有没有人有任何想法?
Dim scripttext As String
scripttext = "$MailboxData = @() " & vbCrLf & _
"$mailboxes = Get-Mailbox -ResultSize unlimited " & vbCrLf & _
"foreach ($mailbox in $mailboxes) { " & _
"$DBObject = new-object PSObject " & _
"$DBObject | add-member NoteProperty -Name Alias $Mailbox.alias " & _
"$DBObject | add-member NoteProperty -Name LastLogonTime (Get-MailboxStatistics $mailbox.alias).LastLogonTime " & vbCrLf & _
"$MailboxData = $MailboxData + $DBObject " & _
"}" & vbCrLf & _
"$mailboxData " & vbCrLf
powershell.Commands.AddScript(scripttext)
powershell.Runspace = myRunSpace
results = powershell.Invoke()
Dim displayname As String
Dim lastLogon As String
For Each obj As PSObject In results
Try
displayname = obj.Members("alias").Value.ToString
Catch ex As Exception
displayname = Nothing
End Try
Try
lastLogon = Convert.ToDateTime(obj.Members("LastLogonTime").Value.ToString)
Catch exc As Exception
lastLogon = Nothing
End Try
Next
答案 0 :(得分:0)
看起来像VB代码的这些行:
"foreach ($mailbox in $mailboxes) { " & _
"$DBObject = new-object PSObject " & _
"$DBObject | add-member NoteProperty -Name Alias $Mailbox.alias " & _
作为单行传递给PowerShell,没有换行符。我要么将vbCrLf添加到每个行的末尾,要么在每个$DBObject
行的末尾使用PowerShell语句分隔符,例如:
"foreach ($mailbox in $mailboxes) { " & _
"$DBObject = new-object PSObject; " & _
"$DBObject | add-member NoteProperty -Name Alias $Mailbox.alias; " & _