Module to Connect to Remote PS Enviroments

So I created a Module to connect to my Office 365 Remote PS, and two on-perm PS environments.

I have loaded the Module and ran the three cmdlets that I use to run out of my powershell profile. However now cmdlets like Get-Mailbox are showing up after I connect to the remote PS Environment.

Here is part of the module.

Function Connect-Office365
[Parameter(Mandatory = $False)]
[System.Management.Automation.PsCredential]$Credential = $Host.UI.PromptForCredential("Enter MSOL Admin Credential",
"Enter the username and password of an MSOnline Administrator account.","","userCreds"),
[Parameter(Mandatory = $False)]
[System.Uri]$Uri = ""
connect-msolservice -credential $credential
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $Uri -Credential $Credential -Authentication "Basic" -AllowRedirection
Import-PSSession $session -prefix EXO -AllowClobber

Return $session

I’m confused - you don’t want Get-Mailbox to show up?

No they are not showing up. Whenever I try to run something that I normally would have ran before when I just had this as a profile script I get an error telling me it’s not recognized.

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.

You applied a prefix to the imported commands. It’d be Get-ExoMailbox.

I still get the error when I try it with the prefix. The Moduletype is a Script. I’m not sure if this would matter?

No. Imported modules are always scripts - it’s just a set of proxy functions, not the “real” functions.

So here’s the thing I think you’re running into: scope. Because you’re creating the PSSession within a function, isn’t it likely that the PSSession ceases to exist after the function runs? You can test that by running the function and then running Get-PSSession to see what sessions are available and what their status is. if the session isn’t opened and available, then the proxy module can’t do its job.

I was wondering about that myself but I do see the PSconnection.

PS H:\IT-Share> Get-PSSession | fl

ComputerName           :
ConfigurationName      : Microsoft.Exchange
InstanceId             : daf95a44-9417-46e7-8b37-b76363d8fe57
Id                     : 3
Name                   : Session3
Availability           : Available
ApplicationPrivateData : {SupportedVersions, ImplicitRemoting, PSVersionTable}
Runspace               : System.Management.Automation.RemoteRunspace
State                  : Opened
IdleTimeout            : 900000
OutputBufferingMode    : None
DisconnectedOn         : 
ExpiresOn              : 

Then… not sure. I’d probably try to manually load the proxy module, and look inside it (in the ISE) to make sure it contained the commands I thought it did.

I also use this script for using Exchange on-premises and Exchange Online command in 1 powershell. This script is also found here

What i would like to add to this script is automatic logging in to the Exchange Online envrionment.

As my powershell scripting knowledge is not very good i am not sure how to add this.

If i where to add to variable as such:
$password=(ConvertTo-SecureString -String “Password” -AsPlainText -Force)

What has to be adjusted to this command to get it to automatically log in:
[System.Management.Automation.PsCredential]$Credential = $Host.UI.PromptForCredential(“Enter MSOL Admin Credential”,
“Enter the username and password of an MSOnline Administrator account.”,“”,“userCreds”),

get-credential | export-clixml ohthreesixtyfive.xml

$creds = import-clixml ohthreesixtyfive.xml

Connect-MsolService -Credential $creds