2010-12-08 154 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数据库。

相关问题