使用VB.Net禁用Exchange 2010邮箱

时间:2013-12-03 01:26:59

标签: vb.net powershell exchange-server-2010

我正在尝试使用VB.Net在Exchange 2010中禁用邮箱。

    Dim rsConfig As RunspaceConfiguration
    rsConfig = RunspaceConfiguration.Create()
    Dim snapInException As PSSnapInException = Nothing
    Dim info As PSSnapInInfo = rsConfig.AddPSSnapIn("microsoft.exchange.management.powershell.e2010", snapInException)
    Dim myRunSpace As Runspace
    myRunSpace = RunspaceFactory.CreateRunspace(rsConfig)
    myRunSpace.Open()
    Dim pipeLine As Pipeline
    pipeLine = myRunSpace.CreatePipeline()
    Dim sScript As String = "disable-mailbox -Identity 'Bill Smith' -Confirm:$false"
    pipeLine.Commands.AddScript(sScript)
    pipeLine.Invoke()
    pipeLine.Dispose()

我收到此错误:

  System.Management.Automation.CmdletInvocationException was unhandled
  Message=Value cannot be null.
Parameter name: serverSettings
  Source=System.Management.Automation
  WasThrownFromThrowStatement=False
  StackTrace:
       at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate)
       at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
       at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
  InnerException: System.ArgumentNullException
       Message=Value cannot be null.
Parameter name: serverSettings
       ParamName=serverSettings
       Source=Microsoft.Exchange.Configuration.ObjectModel
       StackTrace:
            at Microsoft.Exchange.Configuration.Tasks.TaskVerboseStringHelper.GetADServerSettings(String cmdletName, ADServerSettings serverSettings)
            at Microsoft.Exchange.Configuration.Tasks.TaskVerboseStringHelper.GetADServerSettings(ADServerSettings serverSettings)
            at Microsoft.Exchange.Configuration.Tasks.Task.LogCmdletIterationEvent()
            at Microsoft.Exchange.Configuration.Tasks.Task.BeginProcessing()
            at System.Management.Automation.Cmdlet.DoBeginProcessing()
            at System.Management.Automation.CommandProcessorBase.DoBegin()
       InnerException: 

有人可以帮忙吗? 在此先感谢。

1 个答案:

答案 0 :(得分:1)

这是错误的重要部分:

  

Message = Value不能为null。参数名称:serverSetting

堆栈跟踪可能很难阅读,但第一印象是您将null / Nothing值传递给想要某个实例的函数。

我不熟悉Exchange对象并且您没有共享哪一行引发错误,但我最好的猜测通过代码读取此行会引发错误:

myRunSpace = RunspaceFactory.CreateRunspace(rsConfig)

你可以通过改变顶部附近的这条线来修复它:

Dim rsConfig As RunspaceConfiguration

到此:

Dim rsConfig As New RunspaceConfiguration

不幸的是,我怀疑这只会帮助您发现下一个错误。我希望您需要将配置对象传递给该方法,并且默认实例可能不够好。