2012-09-06 87 views
1

我試圖獲取使用以下代碼的用戶的上次登錄日期。我的問題是從powershell變量$ MailboxData的PowerShell變成VB.net變量。將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

它看起來像你的VB代碼,這些行:

"foreach ($mailbox in $mailboxes) { " & _ 
      "$DBObject = new-object PSObject " & _ 
      "$DBObject | add-member NoteProperty -Name Alias $Mailbox.alias " & _ 

傳遞到PowerShell中與不換行一行。我要麼添加vbCrLf到每個行的末尾或者使用PowerShell的語句分隔符在每個$DBObject線例如爲:

"foreach ($mailbox in $mailboxes) { " & _ 
      "$DBObject = new-object PSObject; " & _ 
      "$DBObject | add-member NoteProperty -Name Alias $Mailbox.alias; " & _ 
+0

我知道了,謝謝基思的結束,即正常工作。 – Dougs