If i just run commands interactively, all is working as expected
PS> [string]$URL = $(Get-Content $HOME\.psvariables\exchangeserver)
PS> Import-PSSession -Verbose (New-PSSession -Verbose -Name Exchange2013 -ConfigurationName Microsoft.Exchange -ConnectionUri http://$URL/PowerShell/ -Authentication Kerberos)
WARNING: The names of some imported commands from the module 'tmp_wyqpohdw.ids' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.0 tmp_wyqpohdw.ids {Add-ADPermission, Add-AvailabilityAddressSpace, Add-ContentFilterPhrase, Add-DatabaseAvailabilityGroupServer...}
PS> get-mailbox test
Name Alias ServerName ProhibitSendQuota
---- ----- ---------- -----------------
test test exchange2013 Unlimited
But if i run this function from dynamically loaded module, it’s not working:
PS> Connect-Exchange
WARNING: The names of some imported commands from the module 'tmp_fan0rj3t.nzp' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.0 tmp_fan0rj3t.nzp {Add-ADPermission, Add-AvailabilityAddressSpace, Add-Conte...
PS> get-mailbox test
get-mailbox : The term 'get-mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ get-mailbox test
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (get-mailbox:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
But if i run this function from dynamically loaded module, it's not working:
Did you remove your previous session before trying this again?
You don’t say what OS you are running or what version of PoSH you are on.
If you are doing this in the PoSH ISE or Console Host on the Exchange server instead of in the EMS, then this is overkill.
You can just do this…
Add-PSSnapin -Name '*Exchange*'
… to load up the Exchange cmdlets in to either session. No real need to specify the Exchange version, at least I’ve never had a reason to. I just put it in my admin profile on the Exchange server. No need other code effort needed.
If you are doing this from a remote workstation / server the normal documented ways to do this is here:
Connect to Exchange servers using remote PowerShell
If you don't have the Exchange management tools installed, you can use Windows PowerShell on your local computer to create a remote PowerShell session to an Exchange 2016 server. It's a simple three-step process, where you enter your credentials, provide the required connection settings, and then import the Exchange cmdlets into your local Windows PowerShell session so that you can use them.
'technet.microsoft.com/en-us/library/dd335083(v=exchg.160).aspx'
How To–Load Exchange Management Shell into PowerShell ISE
This post provides you with the method to load the Exchange Management Shell into ISE.
'blogs.technet.microsoft.com/samdrey/2017/12/17/how-to-load-exchange-management-shell-into-powershell-ise-2'
I know the last one says ISE, but its the same for the console host. But in the ISE, when you do this, you have to remember to hit that Refresh button on the commands tab panel before you’ll see the cmdlets added to the listing.
Even in the ISE here is a way to add it as a menu item for multiple Exchange versions on-prem and EXO.
Adding Exchange Shell items to PowerShell ISE
In our example, we'll add three entries:
◾ Implicit Remoting to connect to Exchange using a static FQDN;
◾ Loading the Exchange 2010 Snap-in and connecting to Exchange using Autodiscover (unsupported, will bypass RBAC);
◾ Connecting to Exchange Online.
'eightwone.com/2012/10/25/adding-exchange-shell-items-to-powershell-ise'