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 domainname.co.uk - This website is for sale! - domainname Resources and Information. -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.

Connect to Exchange servers using remote PowerShell

Connect to Exchange Online PowerShell

Run PowerShell Against Both On-Premises 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.

… and there are many more.