Link to home
Start Free TrialLog in
Avatar of Kelly Garcia
Kelly GarciaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Remote Exchange powershell returns different results

HI All,

When I run the code below on the exchange management shell on the exchange server it gives me the results I am after:

get-mailbox -ResultSize Unlimited | Get-MailboxStatistics | where {$_.TotalItemSize -ge 4GB} |Sort-Object TotalItemSize -Descending |Select-Object DisplayName,TotalItemSize

Open in new window


however when I run this same code remotely, from my laptop it gives me a different result and not the result I am after, how do I resolve this problem?

below is the code I use to connect remotely to exchange:

$UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ts-mail01.x.com/PowerShell/ -Authentication Kerberos -Credential $UserCredential 

Import-PSSession $Session 

Open in new window


thank you in advance.
Avatar of Kelly Garcia
Kelly Garcia
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I've noticed when I run the code remotely It gives me anything over 4, so it gives me anything over 4 MB, 4GB, 4KB,
Avatar of Qlemo
You mean if running remotely it ignores the GB? Don't think so, otherwise you would get everything ...
Could you be more precise about what the issue is?
What about if you convert it to a script block?

$ScriptblockText = 'get-mailbox -ResultSize Unlimited | Get-MailboxStatistics | where {$_.TotalItemSize -ge 4GB} |Sort-Object TotalItemSize -Descending |Select-Object DisplayName,TotalItemSize'

$ScriptBlock = [scriptblock]::create($ScriptBlockText)

$UserCredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ts-mail01.x.com/PowerShell/ -Authentication Kerberos -Credential $UserCredential 

Invoke-Command -Session $Session -ScriptBlock $ScriptBlock

Open in new window


Coralon
i am getting this error:

Script block literals are not allowed in restricted language mode or a Data section.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : ScriptBlockNotSupportedInDataSection

Open in new window

I see an issue with remote management (no matter if Exchange Shell or imported session) and TotalItemSize. This property is a rich (Exchange) object with methods, but get's converted into a string if used remote. You cannot compare the string reliably, which is something like "1.047 GB (1,124,019,831 bytes)".
In a remote Management Shell, this works:
get-mailbox -ResultSize Unlimited | Get-MailboxStatistics | ? { ([Microsoft.Exchange.Data.ByteQuantifiedSize] $_.TotalItemSize).ToGB() -gt 4 }

Open in new window

but not in an imported session (because that doesn't know about the full Exchange types).
I get this error:

? : Unable to find type [Microsoft.Exchange.Data.ByteQuantifiedSize].
At line:1 char:61
+ ... tatistics | ? { ([Microsoft.Exchange.Data.ByteQuantifiedSize] $_.Tota ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Microsoft.Excha...eQuantifiedSize:TypeName) [Where-Object], RuntimeEx
   ception
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.WhereObjectCommand

Open in new window

but not in an imported session
in the exchange management shell, it works fine without having to enter this code. it recognises the GB.
But only on the Exchange machine (running locally). It does not in my Exchange 2013 remote one.
This seems to work, however the output isn't great:

$GMSize = get-mailbox -ResultSize Unlimited | Get-MailboxStatistics | where {$_.TotalItemSize -ge "4GB"} | Select-Object DisplayName,TotalItemSize, ItemCount, Database, TotalDeletedItemSize,DeletedItemCount, LastLoggedOnUserAccount, LastLogonTime, LastLogoffTime 


$GMSize | Select-String "GB" | Format-List

Open in new window


how do I resolve the output issue.

please see the screendumpUser generated image
Your last line does not make sense. It is a textual parse of the complete object representatition as a string, and it is unnecessary - if the first part works, which does not. Comparing to a string "4GB" does not make it work. You see TotalItemSize to be a complex string like shown in #a42086660, which you can't "string compare" that easily. No way.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, so in this case running the below PowerShell script on all of the Exchange servers is the fix on Kay's issue:

# load the IIS module
Import-Module WebAdministration

# get the path to the exchange server installation directory 
# and create a new folder for the exadmin application
$path = ‘HKLM:\SOFTWARE\Microsoft\ExchangeServer\v14\Setup’
$exbin = Join-Path (Get-ItemProperty $path).MsiInstallPath ClientAccess
$folder = New-Item -Path $exbin\exadmin -ItemType Directory -Force 

# copy the web.config file to the new directory, load it (as xml) and
# change the language mode (from RestrictedLanguage) to FullLanguage
Copy-Item $exbin\PowerShell\web.config $folder.FullName -Force
$wconfig = Get-Content $exbin\exadmin\web.config
$wconfig.configuration.appSettings.add.value = 'FullLanguage'
$wconfig.Save("$exbin\exadmin\web.config")

# Create a new IIS application pool, and start it
$pool = New-WebAppPool -Name exadmin

# Configure the exadmin app pool to run under the LocalSystem account (0)
Set-ItemProperty IIS:\AppPools\exadmin -Name ProcessModel -Value @{identityType=0}

# start app pool
Start-WebAppPool -Name exadmin

# Create a new IIS Web Application.
$application = New-WebApplication -Name exadmin -Site 'Default Web Site' -PhysicalPath "$exbin\exadmin" -ApplicationPool $pool.name

#Set the application SSL settings to accept client certificates (if they are provided)
Set-WebConfigurationProperty -Filter //security/access –Name SslFlags -Value SslNegotiateCert -PSPath IIS:\ -Location 'Default Web Site/exadmin'


# create new end point configuration and allow administrators to remotely run commands
# a dialog is shown with the local administrators group selected, and we can add 
# users/groups we want to have access to the end point
#Get-PSSessionConfiguration exadmin | Unregister-PSSessionConfiguration -Force
Register-PSSessionConfiguration -Name exadmin -Force
Set-PSSessionConfiguration -Name exadmin -ShowSecurityDescriptorUI -Force

# testing the new environment, uncomment and change database identity
# create a fan-in session (notice we are connecting to exadmin) and try to 
# invoke the ToBytes method – it works
#$sb = { (Get-MailboxDatabase -Status -Identity 'Mailbox Database 0311695863').DatabaseSize.ToBytes() }
#$uri = ‘http://dc1.homelab.com/exadmin’
#$session = New-PSSession -ConfigurationName Microsoft.Exchange –ConnectionUri $uri
#Invoke-Command $session –ScriptBlock $sb

Open in new window


Kay, can you confirm if the script above fixed your problem in PS Remoting?