New-object on remote session failing

trying to create an object on a remote session but is failing. Any idea ?

PS> $sess = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “http://server/Power
Shell” -Authentication Kerberos -Credential $cred -ErrorAction Stop
PS> Enter-PSSession $sess
[server]: PS>new-object

writeErrorStream : True
Exception : System.Management.Automation.RemoteException: The term ‘New-Object’ is not recognized as the
name of a cmdlet, function, script file, or operable program. Check the spelling of the nam
e, or if a path was included, verify that the path is correct and try again.
at System.Management.Automation.PowerShell.CoreInvokeRemoteHelper[TInput,TOutput](PSDataC
ollection1 input, PSDataCollection1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataC
ollection`1 output, PSInvocationSettings settings)
at System.Management.Automation.RemotePipeline.Invoke(IEnumerable input)
at Microsoft.PowerShell.Executor.ExecuteCommandHelper(Pipeline tempPipeline,

The session is constrained and I don’t think it has new-object available. Rather than entering the session - use import-possession to make proxy functions of the Exchange cmdlets and work with them that way

how can i create an unconstrained session ?

With very great difficulty

The Exchange team designed their PowerShell implementation to be deliberately constrained when accessed remotely. As I stated earlier the best way is to create the session and then use import-pssession.

That is the way it is designed to work and that is the way that works best. I have spent a lot of time accessing Exchange remotely and think I’ve tried most options. Importing the session is the cleanest way to work

Any idea what changes can i make to the below code to make it work ?

$Mbxservers=Get-MailboxServer name* Foreach ($server in $mbxservers) { $sess = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “http://$server/PowerShell” -Authentication Kerberos -Credential $cred -ErrorAction Stop $arrayJobs += Invoke-Command {

$serverName=[System.Net.Dns]::GetHostName()
$report = @()
$mailboxes = @(Get-Mailbox -Filter ‘RecipientTypeDetails -eq “UserMailbox”’ -server $serverName -ResultSize unlimited)
foreach ($mb in $mailboxes)
{
$stats = $mb | Get-MailboxStatistics | Select-Object TotalItemSize,TotalDeletedItemSize,ItemCount,LastLogonTime,LastLoggedOnUserAccount
$userObj = New-Object PSObject
$userObj | Add-Member NoteProperty -Name “DisplayName” -Value $mb.DisplayName
$userObj | Add-Member NoteProperty -Name “Last Logon By” -Value $stats.LastLoggedOnUserAccount
$userObj | Add-Member NoteProperty -Name “Item Size (Mb)” -Value $stats.TotalItemSize.Value.ToMB()
$userObj | Add-Member NoteProperty -Name “Deleted Item Size (Mb)” -Value $stats.TotalDeletedItemSize.Value.ToMB()
$userObj | Add-Member NoteProperty -Name “Items” -Value $stats.ItemCount
$userObj | Add-Member NoteProperty -Name “Type” -Value $mb.RecipientTypeDetails
$userObj | Add-Member NoteProperty -Name “Server” -Value $mb.ServerName
$userObj | Add-Member NoteProperty -Name “Database” -Value $mb.Database
$report = $report += $userObj
}
$report
} -asjob -Session $sess
}

I’d look at something like this. can’t guarantee its 100% as I don’t have an Exchange server handy to test

$Mbxservers=Get-MailboxServer name*
Foreach ($server in $mbxservers)
{
$sess = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “http://$server/PowerShell” -Authentication Kerberos -Credential $cred -ErrorAction Stop
import-pssession $sess
$serverName=[System.Net.Dns]::GetHostName()
$report = @()
$mb = Get-Mailbox -Filter ‘RecipientTypeDetails -eq “UserMailbox”’ -server $serverName -ResultSize unlimited
$mb | Get-MailboxStatistics |
Select-Object @{N=“DisplayName”; E={$mb.DisplayName}},
@{N=“Item Size (Mb)”; E={$.TotalItemSize.Value.ToMB()}},
@{N=“Deleted Item Size (Mb)”; E={$
.TotalDeletedItemSize.Value.ToMB()}},
ItemCount,LastLogonTime,LastLoggedOnUserAccount,
@{N=“Type”; E={$mb.RecipientTypeDetails}},
@{N=“Server”; E={$mb.ServerName}},
@{N=“Database”; E={$mb.Database}}
}