Dazza76
Dazza76

Reputation: 21

Disable an Exchange 2010 mailbox using VB.Net

I'm trying to disable a mailbox in Exchange 2010 using VB.Net.

    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()

I get this error:

  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: 

Can anyone help? Thanks in Advance.

Upvotes: 2

Views: 944

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 415735

This is the important part of the error:

Message=Value cannot be null. Parameter name: serverSetting

Stack traces can be tricky to read, but the first impression is that you're passing a null/Nothing value to a function that wants an instance of something.

I'm not familiar with the Exchange objects and you didn't share which line throws the error, but my best guess reading through the code is this line throws the error:

myRunSpace = RunspaceFactory.CreateRunspace(rsConfig)

And you can fix it by changing this line near the top:

Dim rsConfig As RunspaceConfiguration

to this:

Dim rsConfig As New RunspaceConfiguration

Unfortunately, I suspect that this will only help you spot the next error. I expect there is a reason you need to pass a configuration object to that method, and the default instance may not be good enough.

Upvotes: 1

Related Questions