2010-12-08 150 views
1

我目前有一個ASP.NET編程,創建一個Powershell CmdLet來創建一個郵箱交換。VB.NET Powershell從一個命令到另一個命令的結果

我遇到的問題有時無法創建郵箱,因爲「找不到」我指定的交換數據庫。

所以我想要做的是運行Get-Mailbox,然後將結果傳遞給Enable-Mailbox命令。

下面是我用來做它的代碼:

Public Sub CreateMailbox() 

    Dim getMailbox As Command 
    getMailbox = GetMailboxCommand() 

    Dim createCommand As Command 
    createCommand = GetCreateCommand() 

    Dim results As String 

    results = RunCommand(createCommand, getMailbox) 

    Dim setCommand As Command 
    setCommand = GetSetCommand() 

    results = RunCommand(setCommand) 

End Sub 

這是命令獲取郵箱:

Private Function GetMailboxCommand() As Command 
    Dim cmd As New Command("Get-Mailbox") 

    cmd.Parameters.Add("Database", UserDatabase) 

    Return cmd 
End Function 

命令創建郵箱:

Private Function GetCreateCommand() As Command 
    Dim cmd As New Command("Enable-Mailbox") 

    cmd.Parameters.Add("Identity", DistinguishedName) 
    cmd.Parameters.Add("Database") 
    cmd.Parameters.Add("Alias", UserAlias) 
    cmd.Parameters.Add("PrimarySmtpAddress", PrimarySMTP) 

    Return cmd 

End Function 

執行所有powershell命令的代碼:

Private Function RunCommand(ByVal createCommand As Command, ByVal getMailbox As Command) As String 
    'Create Runspace configuration 
    Dim rsConfig As RunspaceConfiguration 
    rsConfig = RunspaceConfiguration.Create() 

    Dim snapInException As PSSnapInException = Nothing 
    Dim info As PSSnapInInfo 
    info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", snapInException) 

    Dim myRunSpace As Runspace 
    myRunSpace = RunspaceFactory.CreateRunspace(rsConfig) 
    myRunSpace.Open() 

    Dim pipeLine As Pipeline 
    pipeLine = myRunSpace.CreatePipeline() 

    pipeLine.Commands.Add(getMailbox) 
    pipeLine.Commands.Add(createCommand) 

    pipeLine.Commands.Add("Out-String") 

    Dim commandResults As Collection(Of PSObject) 
    commandResults = pipeLine.Invoke() 

    myRunSpace.Close() 

    Dim sb As String = "Results: " 
    For Each result As PSObject In commandResults 
     sb &= result.ToString 
    Next 

    Return sb 
End Function 
+0

是否有任何模式的失敗? – 2010-12-12 20:18:27

回答

0

事實證明,問題在於用戶正在用來運行應用程序池沒有權限訪問Exchange數據庫。

相關問題