none
Remotepowershell error for Get-MailboxStatistics. RRS feed

  • Question

  • Hi,,
    Curently I have developesd a task to get disabled mailbox details from my Exchange server 2010 using C#.net (VS2010). I got the error from the following line..

    (Remote powershell)Code snipt:

    try
    {
     ..........
     ..........
     cmd = new Command("Get-MailboxStatistics -Server RD85|where{ $_.DisconnectDate -ne $null }");
     pipline = runspace.CreatePipeline();
     pipline.Commands.Add(cmd);
     Collection<PSObject> psCol = pipline.Invoke(); //<--------- error comes from this line
     .........
     .........
    }
    catch (RemoteException re)
    {
    strError = re.Message;
    }


    Error Message:

    RemoteException was caught
    The term 'Get-MailboxStatistics -Server RD85|where{ $_.DisconnectDate -ne $null }' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

    I have run the same script in my Exchange server 2010 management console and it works fine without any error

    [PS] C:\Windows\system32>Get-MailboxStatistics -Server RD85| where { $_.DisconnectDate -ne $null }|format-list

    SO pls help me how can I overcome my issues.

    By

    P Elayaraja


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"

    Thursday, September 13, 2012 1:40 PM

Answers

  • Hi every one. I gto the solutions using slight modification in my code..

    Below the solutions code

    try
    {
     ..........
     ..........
     cmd = new Command("Get-MailboxStatistics);
    cmd.Parameters.Add("-Server", "RD85");
     pipline = runspace.CreatePipeline();
     pipline.Commands.Add(cmd);
     Collection<PSObject> psCol = pipline.Invoke(); 
     foreach (PSObject ps in psCol)
     {
      if (ps.Properties["DisconnectDate"]!= null)
      {
        ....
        ....
      }
    }
     .........
     .........
    }
    catch (RemoteException re)
    {
    strError = re.Message;
    }

    The above code gives the exact answer ..


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"

    • Marked as answer by P Elayaraja Friday, September 14, 2012 1:53 PM
    Friday, September 14, 2012 1:53 PM

All replies

  • The remote session provided by the Exchange server is a constained runspace.

    It only has the Exchange-specific cmdlets (and those aren't really cmdlets, they're actually proxy functions). 

    The cmdlets where-object and format-table do not exist in that runspace. 


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, September 13, 2012 1:53 PM
  • So how can I collect the disabled mailbox details using powershel with .net?

    Pls guide me..


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"

    Thursday, September 13, 2012 1:58 PM
  • Format-Table and of course Where-Object both work pretty well in Exchange Management Shell and even in the remote console running against an Exchange server.

    (I've been using in every day for the last year or two.)

    Thursday, September 13, 2012 2:08 PM
  • Neither of those are using the runspace provided by the Exchange server directly.

      If you want to see what's in that runspace, create a PSSession that connects to that remote runspace, then enter that PSSession and see what's there.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, September 13, 2012 2:14 PM
  • To use a remote PS Session on the Exchange server for management, you start in a local generic PS session, create a new PSSession that connects to the remote Exchange runspace, and then use Import-PSSession to import those proxy functions into your current session.  From there Powershell knows to execute the Exchange cmdlets in the remote session, and return the results back to your local session.  This is called "implicit remoting".

    I'm just an Exchange admin, so I can't tell you how to duplicate that in C#.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, September 13, 2012 2:25 PM
  • Something else you may need to be aware of when using that remote session is that what it returns will not be native Exchange objects, but de-serialized objects.  A consequence of this is that all of the methods that would normally be there may not (the proxy functions usually re-construct some of them, but there's no guarantee you will get all of them). 

    In the case of the object you will get back from get-mailboxstatistics, I can tell you from experience that the item size properties returned using the EMS shell will be [Microsoft.Exchange.Data.ByteQuantifiedSize] objects, and will have a  Value property that has ToBytes(), ToKB(),ToMB(),ToGB(), and ToTB() methods.

    In the object you get back from the remote session, that Value property will be [string], and you'll have to parse the byte count out of that and do the unit conversions yourself.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, September 13, 2012 2:46 PM
  • Hi every one. I gto the solutions using slight modification in my code..

    Below the solutions code

    try
    {
     ..........
     ..........
     cmd = new Command("Get-MailboxStatistics);
    cmd.Parameters.Add("-Server", "RD85");
     pipline = runspace.CreatePipeline();
     pipline.Commands.Add(cmd);
     Collection<PSObject> psCol = pipline.Invoke(); 
     foreach (PSObject ps in psCol)
     {
      if (ps.Properties["DisconnectDate"]!= null)
      {
        ....
        ....
      }
    }
     .........
     .........
    }
    catch (RemoteException re)
    {
    strError = re.Message;
    }

    The above code gives the exact answer ..


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"

    • Marked as answer by P Elayaraja Friday, September 14, 2012 1:53 PM
    Friday, September 14, 2012 1:53 PM