header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Get-Mailboxstatistics provides different output in different shells
Last Post 23 Aug 2011 03:30 PM by Elliot Gingold. 7 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Elliot GingoldUser is Offline
New Member
New Member
Posts:5
Avatar

--
18 Aug 2011 11:16 PM
    nAlthough I have had some programming experience, I am pretty new to Powershell. So I may have missed something obvious!

    I have written a script to dump some basic mailbox information into a csv file. The script works fine when run from a EMS shell. But I want to run it as a scheduled job, so I added the necessary New-PSSession, Import=PSSessions commands at the beginning and at the end. These were borrowed from another script and seemed to work. And indeed the script continued to work fine when run from the EMS shell. But when run from a non-Exchange shell (as it would be if it was a scheduled job) it failed.

    The errors pointed to a line which used the expression (Get-MailboxStatistics $_.name ).TotalItemSize.Value.ToMB(). To isolate the problem I set up a simple test script as follows:

    $Session = New-PSSession -ConfigurationName Microsoft.Exchange –ConnectionUri [path to Exchange Server PS directory] -Authentication Kerberos
    Import-PSSession $Session
    (Get-MailboxStatistics elliotg ).TotalItemSize.Value.ToMB()
    Remove-PSSession $Session

    When run from the EWS shell this gave the size of my mailbox. When run from a non-Exchange shell, it returned the following error:

    You cannot call a method on a null-valued expression. At C:\sourcefiles\egscripts\test2.ps1:4 char:58
    + (Get-MailboxStatistics elliotg ).TotalItemSize.Value.ToMB <<<< ()
    + CategoryInfo          : InvalidOperation: (ToMB:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    By breaking down the core command in my test script I found that (Get-MailboxStatistics elliotg ).TotalItemSize.Value did indeed return a null result in the non-Exchange PS shell, while in the EWS shell it returned 1.725 GB (1,851,897,391 bytes).

    Stripping the command even more I found that (Get-MailboxStatistics elliotg ).TotalItemSize returned in the EWS shell -
     
    IsUnlimited Value
     ----------- -----
          False 1.725 GB (1,851,897,391 bytes)

    while in the non Exchange shell  simply False 1.725 GB (1,851,897,391 bytes). Which makes it pretty clear why .value returned null in this case!

    All of this was run on a manangment server, but just to cover all bases I repeated it on one of our CAS servers and got the same result.

    I cannot understand why this cmdlet gives different output in these two situations. I hope someone can point the way! It would be useful to be able to run the original script as a scheduled job.

    Elliot
    Marco ShawUser is Offline
    Veteran Member
    Veteran Member
    Posts:1684
    Avatar

    --
    19 Aug 2011 03:28 AM
    I can't test right now, but Exchange 2007 or 2010? It is quite possible that EMS loads a special ETS, so if one looks at the the EMS shortcut properties, one will see that not only the Exchange snapin is loaded, but also functions and possibly even some special formatting files also.
    Elliot GingoldUser is Offline
    New Member
    New Member
    Posts:5
    Avatar

    --
    19 Aug 2011 03:31 AM
    Yes I should have said we are using Exchange 2010 (SP1).

    Elliot
    Karl MitschkeUser is Offline
    Basic Member
    Basic Member
    Posts:457
    Avatar

    --
    19 Aug 2011 06:00 AM
    Elliot;

    You have run into the problems running the Exchange Management Shell cmdlets from a remote session within PowerShell.

    http://technet.microsoft.com/en-us/...97939.aspx says "Without the Exchange management tools installed, you need to connect to the remote Exchange 2010 server manually, and you don't have access to the additional capabilities that the Exchange management tools provide."

    I asked "What "additional capabilities" do the Exchange management tools provide over a remote shell?" on TechNet:
    http://social.technet.microsoft.com...297f7bdbbb

    I reccomend you look at that last link, but in a nutshell the Exchange Management Shell returns rich Exchange Objects, whoile a remote PowerShell session returns PowerShell objects.

    I also cover this in my book (shameless plug) ;)

    Karl
    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Elliot GingoldUser is Offline
    New Member
    New Member
    Posts:5
    Avatar

    --
    19 Aug 2011 02:51 PM
    Thanks for this clear explanation. I guess if I want to use the script as a scheduled job I will just have to keep it simple! I don't know if it is possible to run scheduled jobs using the EMS.

    Elliot
    Elliot GingoldUser is Offline
    New Member
    New Member
    Posts:5
    Avatar

    --
    19 Aug 2011 04:15 PM
    I think I have found a solution to the scheduling problem via my old friend Google. I came upon this

    The scheduling works if you use something of this form when setting up the task:
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; c:\Scripts\MyScript.ps1"

    This ran the script and produced the expected result.

    My source was http://www.mikepfeiffer.net/2010/02...l-scripts/

    Elliot
    Karl MitschkeUser is Offline
    Basic Member
    Basic Member
    Posts:457
    Avatar

    --
    23 Aug 2011 11:30 AM
    Elliot;

    You may also want to check out Shay's post"Get Full Control over your Exchange remote PowerShell session"
    http://preview.tinyurl.com/3jd28j3
    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Elliot GingoldUser is Offline
    New Member
    New Member
    Posts:5
    Avatar

    --
    23 Aug 2011 03:30 PM
    Thanks for this, I found it very interesting! For the issue I was trying to solve getting the Schedduled task to start in EMS works well, but understanding the background is always heplful!

    Elliot
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer