Commands not being imported with PSSession

Hi,

 

I have created a custom import module “import-exchange” which imports all exchange commands and connects to our exchange but even though the PSSession is being created, it is not importing the commands.

The code I am using is:

<pre.
function import-exchange {
[CmdletBinding()]
param (

)
begin {

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://emailserver.domainname.co.uk/PowerShell -Authentication Kerberos -Credential Get-Credential

}
process {
Import-PSSession $Session -DisableNameChecking -AllowClobber
}
end {
}
}

I run Import-Exchange and this creates the PSSession, but if I run Get-Mailbox, it doesn’t recognize the command.

Any ideas?

The connectionuri has fake server and domains in, but the real script has the correct ones.

Why are you doing this?
There is already a prescribed way to do this and well documented for many years now.
Don’t reinvent the wheel and stress yourself out over it. There are prebuilt scripts on the MS powershellgallery.com specially for this as well as ones all over the web, for Exchange on-prem and online.

There are PowerShell modules for it as well…

Find-Module -Name '*exchange*' | Format-Table -AutoSize

Examples:

https://www.powershellgallery.com/packages?q=exchange

CreateExoPsSession 0.11
https://www.powershellgallery.com/packages/CreateExoPsSession/0.11/Content/CreateExoPSSession.ps1

Exchange.Connection.Manager 2.0.1
Are you an Exchange Admin? Are you managing multiple Exchange OnPrem and Online environments? Tired of switching between multiple shells and sessions, updating and entering credentials? Check out the PowerShell Exchange Connection Manager module.
https://www.powershellgallery.com/packages/Exchange.Connection.Manager/2.0.1

Connect to Exchange servers using remote PowerShell
https://docs.microsoft.com/en-us/powershell/exchange/exchange-server/connect-to-exchange-servers-using-remote-powershell?view=exchange-ps

Connect to Exchange Online PowerShell
https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/connect-to-exchange-online-powershell?view=exchange-ps

Run PowerShell Against Both On-Premises and Cloud Mailboxes
https://blogs.technet.microsoft.com/jbecks/2017/08/05/powershell-against-onprem-and-cloud-mailboxes

Connect to Office 365 Services using PowerShell
PowerShell script with helper functions to connect to Office 365 or Exchange On-Premises. Makes it easy to connect to different Office 365 services using the same credentials (non-MFA). No more needing to remember various ways to connect to each workload.
https://gallery.technet.microsoft.com/office/Connect-to-Office-53f6eb07

… and there are many more.