VB.NET中的Exchange Powershell

时间:2014-09-16 22:27:51

标签: vb.net powershell exchange-server

我们有以下代码我试图在Exchange 2010框上运行,但是我收到以下错误。

System.Management.Automation.CmdletInvocationException:值不能为null。 参数名称:参数 - > System.ArgumentNullException:Value不能为null。 参数名称:参数

这是代码

Public Shared Sub ExecuteExchange2010Powershell(ByRef script As String, ByRef append As Boolean, Optional ByRef outputString As String = Nothing)
        Try
            Dim psOutput As String = ""
            Dim runspace As Runspace
            Dim runspaceConfig = RunspaceConfiguration.Create()
            runspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.Powershell.E2010", Nothing)
            runspace = RunspaceFactory.CreateRunspace(runspaceConfig)
            runspace.Open()
            Dim pipeline As Pipeline = runspace.CreatePipeline()
            pipeline.Commands.AddScript(script)
            If Not IsNothing(outputString) Then
                pipeline.Commands.Add("Out-String")
                Dim results As Collection(Of PSObject) = pipeline.Invoke()
                For Each obj As PSObject In results
                    psOutput = (obj.ToString())
                Next
                runspace.Close()
                Using objFile As StreamWriter = New StreamWriter(outputString, append)
                    objFile.Write(psOutput)
                End Using
            Else
                pipeline.Invoke()
                runspace.Close()
            End If
        Catch ex As Exception
            ErrorHandling.RaiseEventLogEntry("CCL Execution Service (Pulse.Core)", String.Format("Failed Executing ExecuteExchange2010Powershell{0}{0}{1}",
                                                                                    vbNewLine,
                                                                                    ex.ToString()), "Error")
        End Try
    End Sub

我发送此子邮件的实际脚本是

Get-MailboxDatabase -Status |格式列表

0 个答案:

没有答案