none
vb.net exchange 2010 problem with enable-mailbox command RRS feed

  • Question

  • Hi

    I have problem with following code ... some how i get this error

     

    System.Management.Automation.CmdletInvocationException was unhandled
      Message=Value cannot be null.
    Parameter name: parameters
      Source=System.Management.Automation
      WasThrownFromThrowStatement=False
      StackTrace:
           at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate)
           at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
           at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()
      InnerException: System.ArgumentNullException
           Message=Value cannot be null.
    Parameter name: parameters
           ParamName=parameters
           Source=Microsoft.Exchange.Configuration.ObjectModel
           StackTrace:
                at Microsoft.Exchange.Configuration.Tasks.TaskVerboseStringHelper.FormatUserSpecifiedParameters(PropertyBag parameters)
                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:

    What i try to do here is run exchange power shell to execute cmd "enable-mailbox" for user, so user gets email address, but seems like some how its asking "parameter" witch does not even exist ... i have checked typed parameters are there and everything should work

    and here is code...

     Public Sub EnableExchangeAccount()
      Dim myRunspace As Runspace
      Dim rsConfig As RunspaceConfiguration
      Dim snapInException As PSSnapInException = Nothing
      Dim info As PSSnapInInfo
      Dim psCommand As String
      rsConfig = RunspaceConfiguration.Create
      info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", snapInException)
      myRunspace = RunspaceFactory.CreateRunspace(rsConfig)
      myRunspace.Open()
      Dim pipeLine As Pipeline
      pipeLine = myRunspace.CreatePipeline
      psCommand = "Enable-Mailbox"
      Dim myCommand As New Command(psCommand)
      pipeLine.Commands.Add(myCommand)
      myCommand.Parameters.Add("Identity", firstname & " " & surname)
      myCommand.Parameters.Add("Alias", username)
      myCommand.Parameters.Add("Database", "MDB01")
      Dim commandResults As ICollection(Of PSObject) = Nothing
      commandResults = pipeLine.Invoke
     End Sub
    
    
    
    
    Wednesday, December 15, 2010 11:47 AM

Answers

  • I solved this problem by using the remote powershell on our exchange servers.

    '-- vars
    Dim pCredential As PSCredential
    Dim pConnectionInfo As WSManConnectionInfo
    Dim pRunspace As Runspace
    Dim pShell As PowerShell
    Dim pCommand As PSCommand
    Dim pResult As Collection(Of PSObject)
    
    '-- set credentials      
    pCredential = New PSCredential(_rps_username, CreateSecurePasswordString(_rps_password))
    
    '-- set connection info
    pConnectionInfo = New WSManConnectionInfo(New Uri(_rps_connection_uri), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", pCredential)
    
    '-- create remote runspace
    pRunspace = RunspaceFactory.CreateRunspace(pConnectionInfo)
    pRunspace.Open()
    
    '-- create powershell
    pShell = PowerShell.Create
    pShell.Runspace = pRunspace
    
    '-- create command
    pCommand = New PSCommand
    With pCommand
       Select Case action
         Case CType("R", Char)  '= read mailbox
                .AddCommand("Get-Mailbox")
                .AddParameter("Identity", _username)
    
         Case CType("C", Char)  '= create mailbox
                .AddCommand("Enable-mailbox")
                .AddParameter("identity", _username)
                .AddParameter("database", _database)
    
         Case CType("D", Char)  '= disable mailbox
                .AddCommand("Disable-Mailbox")
                .AddParameter("Identity", _username)
    
       End Select
    End With
    
    '-- add command to powershell
    pShell.Commands = pCommand
    
    '-- invoke the powershell
    pResult = pShell.Invoke
    
    '-- return mailbox collection
    Return pResult
    
    • Proposed as answer by Nicolas Lekens Wednesday, February 2, 2011 12:01 PM
    • Marked as answer by Sami Hiltunen Wednesday, February 2, 2011 2:01 PM
    Wednesday, February 2, 2011 12:01 PM
  • At first I also installed the exchange management tools on my computer, but then you get the parameter exception.

    In this code I connect to the PowerShell on the remote server, being one of the Exchange Servers (pConnectionInfo).

    Just replace the _rps_connection_uri by the uri to one of your Exchange Servers, in our case http://<servername>/PowerShell.

    If all runs correctly the psResult should contain the created mailbox.

    • Marked as answer by Sami Hiltunen Wednesday, February 2, 2011 2:13 PM
    Wednesday, February 2, 2011 1:36 PM

All replies

  • Hi

     

    I'm having exact the same problem...  Can someone help us out!

    • Proposed as answer by Nicolas Lekens Wednesday, February 2, 2011 10:52 AM
    • Unproposed as answer by Sami Hiltunen Wednesday, February 2, 2011 10:57 AM
    • Proposed as answer by Nicolas Lekens Wednesday, February 2, 2011 10:58 AM
    Tuesday, January 25, 2011 1:53 PM
  • I solved this problem by using the remote powershell on our exchange servers.

    '-- vars
    Dim pCredential As PSCredential
    Dim pConnectionInfo As WSManConnectionInfo
    Dim pRunspace As Runspace
    Dim pShell As PowerShell
    Dim pCommand As PSCommand
    Dim pResult As Collection(Of PSObject)
    
    '-- set credentials      
    pCredential = New PSCredential(_rps_username, CreateSecurePasswordString(_rps_password))
    
    '-- set connection info
    pConnectionInfo = New WSManConnectionInfo(New Uri(_rps_connection_uri), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", pCredential)
    
    '-- create remote runspace
    pRunspace = RunspaceFactory.CreateRunspace(pConnectionInfo)
    pRunspace.Open()
    
    '-- create powershell
    pShell = PowerShell.Create
    pShell.Runspace = pRunspace
    
    '-- create command
    pCommand = New PSCommand
    With pCommand
       Select Case action
         Case CType("R", Char)  '= read mailbox
                .AddCommand("Get-Mailbox")
                .AddParameter("Identity", _username)
    
         Case CType("C", Char)  '= create mailbox
                .AddCommand("Enable-mailbox")
                .AddParameter("identity", _username)
                .AddParameter("database", _database)
    
         Case CType("D", Char)  '= disable mailbox
                .AddCommand("Disable-Mailbox")
                .AddParameter("Identity", _username)
    
       End Select
    End With
    
    '-- add command to powershell
    pShell.Commands = pCommand
    
    '-- invoke the powershell
    pResult = pShell.Invoke
    
    '-- return mailbox collection
    Return pResult
    
    • Proposed as answer by Nicolas Lekens Wednesday, February 2, 2011 12:01 PM
    • Marked as answer by Sami Hiltunen Wednesday, February 2, 2011 2:01 PM
    Wednesday, February 2, 2011 12:01 PM
  • Thanks for the reply

     

    I tested the code and it will return 0 as pResult but it wont enable mailbox for user, i have exchange management tools installed to my computer.

    are you running this code on server or remote computer ?


    Sami Hiltunen
    Wednesday, February 2, 2011 1:13 PM
  • At first I also installed the exchange management tools on my computer, but then you get the parameter exception.

    In this code I connect to the PowerShell on the remote server, being one of the Exchange Servers (pConnectionInfo).

    Just replace the _rps_connection_uri by the uri to one of your Exchange Servers, in our case http://<servername>/PowerShell.

    If all runs correctly the psResult should contain the created mailbox.

    • Marked as answer by Sami Hiltunen Wednesday, February 2, 2011 2:13 PM
    Wednesday, February 2, 2011 1:36 PM
  • Thanks that seems to work 

    In my program i did create new user with disabled account and thats why i did get 0 as return value

    and uri was http://servername/powershell?serializationLevel=Full

    servername was allso written to fqdn


    Sami Hiltunen
    Wednesday, February 2, 2011 2:12 PM
  • Glad it works for you too!
    Wednesday, February 2, 2011 2:21 PM
  • Hello, Nicolas

    I have similar code, but getting an error on WSManConnectionInfo - telling me that there is no such type. Is there some namespace that need to be imported?

    Currently I have following imports, but still getting an error.

    Imports System.Object
    Imports System.Management.Automation
    Imports System.Management.Automation.Host
    Imports System.Management.Automation.Runspaces
    

    Please let me know how to avoid the error. Thank you!


    Alla Sanders
    Thursday, March 10, 2011 9:14 PM
  • Sounds to me you are using an older version of the "System.Management.Automation.dll" (should be included in PowerShell 2.0 installation).

    I had to reinstall my computer two weeks back and now have the same problem (I'm using windows 7 pro x64).

    Default VS uses the System.Management.Automation.dll in "C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0", which is version 6.1.7600.16385 and misses a fiew of the needed classes like PSCommand and WSManConnectionInfo. 

    In my "C:\Windows\winsxs\msil_system.management.automation_31bf3856ad364e35_6.1.7601.17514_none_236c706c3e93d144" I have version 6.1.7601.17514, and when I change the reference to that one, everything works fine.

    I think I'm still missing some framework update which puts the latest version of the dll in "C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0". When I find out which one it is, I'll let you know...

    Friday, March 11, 2011 10:12 AM
  • Try to set your application to x64 and reimport system.management (its in framework) reference

    i think you currently try run x32 application and it does not work, because target system is x64

    at least i couldnt get it working in 32 bit mode, (windows 7 pro x64) --> (server 2008 r2 x64)


    Sami Hiltunen
    Friday, March 11, 2011 12:23 PM
  • My VS is set to x64... Lucky me I can get everything to work because I have the right dll on my computer.

    It's just not everywhere it should be. Thanks for the effort!

    Friday, March 11, 2011 12:47 PM
  • Thank you so much! I changed the reference and it recompiled just fine. I appreciate the tip!

    Sincerely, Alla


    Alla Sanders
    Friday, March 11, 2011 1:56 PM
  • I celebrated too soon. It compiles fine, runs with no errors, but won't create mailbox. I have error check - nothing shows up. Not sure if I didn't specify uri correctly? I listed full domain name -

    http://ourservername.companyname.net/powershell?serializationLevel=Full

    For database listed folders like "ourservername\Databases\OurMailbox"

    Is it correct way or not? Any suggestions on error trapping? I didn't have those problems with Exchange 2007! Thank you!


    Alla Sanders
    Friday, March 11, 2011 9:18 PM
  • Dim _rps_connection_uri As New Uri(http://servername/PowerShell)
    and database is only database name "ourmailbox" ?
      .AddParameter("database", "ourmailbox")
    did you enter credentials ?
    and is user added rigth active directory groups?


    Sami Hiltunen
    Monday, March 14, 2011 7:53 AM
  • Yes - I usually add new user to AD (works fine), then create mailbox. For .AddParameter command - first I define the database:

    DB_Main = "exchange2010\Databases\OurMailbox"

    Then use it:

    .AddParameter("Database", DB_Main)

    - so I specify full path.

    About uri - my question was if I should just say "exchange2010" as servername or "exchange2010.companyname.net"?

    I hardcoded the credentials for now and tested that I can manually add mailbox, using that u/p. For password I also use the code:

    For Each c In password.ToCharArray()
      securePassword.AppendChar(c)
    Next
    Thank you, Alla Sanders
    Monday, March 14, 2011 12:46 PM
  • Database must be just databasename i dont think  "exchange2010\Databases\OurMailbox" work.. you should get error parameter incorrect. it do work in older 2007 i think but not in 2010.

    you can test this by starting exchange management shell and typing "enable-mailbox test.user@testdomain.com -database exchange2010\Databases\OurMailbox"

    you probably get error something like this "Cannot process argument transformation on parameter 'Database'. Cannot convert value "exchange2010\Databases\OurMailbox" ......

    if you dont know whats your database name is you cant get it by running "Get-MailboxDatabase -Status" in shell

    user credentials what you are using in code need to have added to group "Exchange Windows Permissions", i dont remeber was there some other group too but you can see these groups in ad domain root under "Microsoft Exchange Security Groups" and there is description for each group what is does.

    about uri it should not matter if you write it exchange2010 or exchange2010.companyname.net if computer is joined in same domain, when computer is joined in same domain its using domain name suffix to get fqdn.

    so its basicly same thing, but if your computer is not in domain then its exchange2010.companyname.net

     


    Sami Hiltunen
    Monday, March 14, 2011 2:21 PM
  • Thank you for the tip. Not quite sure why, but it started working now. I had tried on friday to use just an actual database name, not full path and it didn't work. Today I was given local admin on exchange server - everything started working. Then network removed admin access - but it still works. Confused, but happy! Thanks again, Sami!
    Alla Sanders
    Monday, March 14, 2011 8:54 PM