Author Posts

December 14, 2017 at 1:13 pm

Which one of you clever folks can help me out here? I think this is just a lack of knowledge on my part as to how PowerShell deals with importing modules.

If I run the following code in ISE then everything connects fine and the commands import okay with the EOL prefix.

function Connect-EOL()
    {
    if((get-pssession | where{$_.name -eq "EOL"}).count -lt 1)
        {
        $O365Cred = Get-Credential -Message "Please enter admin credentials to connect to Exchange Online"
        $proxysettings = New-PSSessionOption -ProxyAccessType IEConfig
        $365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $O365Cred -Authentication Basic -AllowRedirection -Name EOL -SessionOption $proxysettings
        Import-PSSession $365Session  -Prefix "EOL"
        }
    }

Then if I save that code as C:\Windows\System32\WindowsPowerShell\v1.0\Modules\My-PSModule\My-PSModule.psm1 and close and open ISE then it loads up the module which visually looks okay. Intellisense can auto-comlete the Connect-EOL command, it connected to our tenancy and displays the exported commands with the EOL prefix but if I try to use any of the commands they aren't available.

So, my two questions are; why aren't the commands available for me to use and how can I make the commands available?

Sorry if this is a bit of a numpty question but it's my first go at creating a module and I think maybe I haven't quite got it right.

Many thanks,
Baz

December 14, 2017 at 6:05 pm

Can you give us an example of some of the commands you are expecting to get but aren't working?

December 14, 2017 at 10:04 pm

I do this sort of this as well every day for all my O365/Azure target resources.

Two things though.

One, you still should get in the habit of login to MSOnline first via the MSOnline sign-in assistant before establishing a session to an O365/Azure resource.

In the ISE, when you load these session, to make these cmdlets appear in the 'Commands Tab', you need to hit the 'Refresh' button on the 'Commands' tab. However, if you don't do the 'Refresh' thing, you can still use them from the ISE console pane.
At least that's how things have occurred in my use case.

What I do is have this in my AzureAdmin profile on my admin workstation.

$CredsCloudAdmin = Get-Credential -Credential "YourGlobalAdminAccountName@$env:USERDOMAIN.onmicrosoft.com"

# Environment setup
# Sign in to MS Online
Connect-MsolService -Credential $CredsCloudAdmin
# Import-Module -Name MSOnline
Import-Module -Name MSOnlineExtended

# Connect to AzureAD
Connect-AzureAD -Credential $CredsCloudAdmin
# Import-Module -Name AzureAD
Import-Module -Name AzureADPreview

# Connect to Online resources
## Session Options
$so = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck

## Create session for Exchange
$ExoSession = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri 'https://outlook.office365.com/powershell-liveid/' `
-Credential $CredsCloudAdmin -Authentication Basic -AllowRedirection
Import-PSSession $ExoSession -Prefix 'EXO'

Start-Sleep -Seconds 3
$psISE.CurrentPowerShellTab.VerticalAddOnTools[4].Refresh

December 14, 2017 at 10:48 pm

Similar to above I have this in my Powershell profile, I do not ever do anything with -Prefix though

function Connect-ExchangeOnline
{
Set-ExecutionPolicy RemoteSigned

$usercredential = Get-Credential

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/?proxymethod=rps -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
Connect-MsolService -Credential $usercredential
}

December 14, 2017 at 11:13 pm

I got into the habit of using the -prefix thing a while back, specifically when I am loading on-prem Exchange cmdlets and Exchange online cmdlets simultaneously, in order to work with both environments as needed, with using the 'New PowerShell Tab' or 'New Remote PowerShell Tab' approach.

I still use the aforementioned remote tab approach, in other scenarios though.

For Admins who don't need all this open at the same time or whom have multiple Exchange environments. I recommended they take this approach.
'eightwone.com/2012/10/25/adding-exchange-shell-items-to-powershell-ise'

Well, he updated this, this year.
'eightwone.com/2012/10/25/adding-exchange-shell-items-to-powershell-ise'

Or the more fully automated way of setup here:
'powershellgallery.com/packages/O365_Installs_Connections_1.1/1.1/Content/O365_Installs_Connections_1.1.ps1'

December 15, 2017 at 9:28 am

Thanks guys,

Jon – Any commands to do with Exchange Online so anything starting verb-EOLxxxxxxx (get-EOLMailbox, get-EOLAcceptedDomain, add-EOLMailboxLocation....) You mentioned you put your script in your profile

PostanoteOne, you still should get in the habit of login to MSOnline first via the MSOnline sign-in assistant before establishing a session to an O365/Azure resource.
– Is there a reason why you say this? I'm only working on Exchange so have no reason to access licensing and user accounts are synced so no reason to access user accounts.
– For the command list refresh, that's an interesting little quirk although I don't use the command list. I ddid give it a try though to see if it makes the commands available but no luck and they don't appear in the command list either. I have something like the script you have and it works fine if I just run it or if I create a function for it and run that, it only stops working when I place that function into a module.

Thanks for the advice but I think this is more to do with how a function in a module differs from a function in a profile or just running it directly in the console.

December 15, 2017 at 12:36 pm

Are you connecting to your on prem exchange server AND the o365 exchange server at the same time? i.e. hybrid setup or still have legacy exchange for some reason

I do not use prefix at all in my function and unless I am also connected to my on prem exchange server (we are in a hybrid setup) there is never a confusion in commands. Do you really *need* the prefix? When I need to connect to the on prem server, I just open a new tab (I use https://conemu.github.io/)

In regards to postanote's comment about connecting to MSOL first, as you can see in my function I do not, and I have not run into any issues. If you are a pure Exchange admin, I would think you probably do not need to connect to MSOL regularly. I use it for licensing and checking on provisioning of accounts, but I am in a small org and manage more than a pure email admin would in a large org.

December 15, 2017 at 1:19 pm

Hi Jon,
Yes I do need the prefix as it's a hybrid environment. The prefix isn't the issue, it's that it doesn't import the commands when running from a module.

There's a similar thread here https://social.technet.microsoft.com/Forums/windows/en-US/61cd013a-3207-4672-b806-cc359d3c477c/start-remote-powershell-from-a-module?forum=winserverpowershell

None of those options worked for me either so I've just settled for having the function in the profile but it's not really ideal for what I want.

December 15, 2017 at 2:06 pm

Ahh ok, I see what you're saying.

As a work around, if you really want to use the modules you could run two separate shells one for O365 and one for on on prem management. Since powershell is open sourced now, it might be worth putting it as a bug on Github.

December 15, 2017 at 2:15 pm

That still wouldn't really solve the issue that I wanted a module so that I can distribute it to the rest of the team rather than adding the function to their profile they could just install the module and then just type Connect-EOL to connect to Exchange Online with commands using the EOL prefix or Connect-OnPrem to connect to the on-premises Exchange server with an OnPrem prefix.

Oh well, it would have been nice but it just seems overly complicated to import a pssession from a module. 🙁