PowerShell Remoting w/ Exchange 2010: Value cannot be null

I'm having difficulty running Exchange 2010 cmdlets through remote PowerShell sessions.

I start my local PowerShell session as Administrator and issue the following commands --

PS C:\Windows\system32> $mailcred = Get-Credential
PS C:\Windows\system32> $mailSession = New-PSSession -ComputerName MAILSRV -Credential $mailcred
PS C:\Windows\system32> Enter-PSSession $mailSession
[MAILSRV]: PS C:\Users\jdoe\Documents> Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
[MAILSRV]: PS C:\Users\jdoe\Documents> hostname
MAILSRV
[MAILSRV]: PS C:\Users\jdoe\Documents> Get-ExchangeServer
Value cannot be null.    
Parameter name: serverSettings
    + CategoryInfo          : 
    + FullyQualifiedErrorId : System.ArgumentNullException,Microsoft.Exchange.Management.SystemConfigurationTasks.GetExchangeServer

[MAILSRV]: PS C:\Users\jdoe\Documents> get-mailbox
Value cannot be null.    
Parameter name: serverSettings
    + CategoryInfo          : 
    + FullyQualifiedErrorId : System.ArgumentNullException,Microsoft.Exchange.Management.RecipientTasks.GetMailbox

As you can see, none of the Exchange cmdlets are working. What could be the issue?


Solution 1:

You're trying to connect to the default remoting endpoint on the Exchange Server and add the ps snapins from there. This is wrong. Replace your first 3 lines with this:

$mailcred = Get-Credential
$mailSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://MAILSRV/PowerShell/ -Credential $mailcred
Import-PsSession $mailSession

You don't have to enter the session, import it into the local session instead. From here on, you can use the Exchange-specific CmdLets locally. Also, some of the Exchange .Net types are installed with the Exchange Management Console, so this needs to be installed on your local computer if you want to do stuff with mailbox sizes (Exchange uses it's own types for size objects)

Solution 2:

You can do it like this, with import-pssesion and specifing the connectionURI & ConfigurationName.

PS U:\> $cred = Get-Credential
PS U:\> $session = New-PSSession -ConnectionUri http://Exchange01/powershell -ConfigurationName Microsoft.Exchange -Credential $cred
PS U:\> Import-PSSession $session
PS U:\> Get-Mailbox marius.davidsen

Name                      Alias                ServerName       ProhibitSendQuota                                                                                        
----                      -----                ----------       -----------------                                                                                        
Marius Davidsen           Marius.Davidsen      Exchange01       unlimited             

This requires that you do allow tcp port 80 in your firewall.

Using that session, you can also enter it like you wanted:

PS U:\> Enter-PSSession $session
[Exchange01]: PS> get-mailbox
[Exchange01]: PS> get-mailbox marius.davidsen

Name                      Alias                ServerName       ProhibitSendQuota                                                                                        
----                      -----                ----------       -----------------                                                                                        
Marius Davidsen           Marius.Davidsen      Exchange01       unlimited