Office 365

Office 365 – Using dynamic variable as prefix for Office 365 commands

One of the nice features of an Import-PSSession is Prefix parameter. If you have never used one let me try to show you an example that should clear things up for you. Let's assume You want to get mailbox from Office 365 and Exchange On-Premises during the same session. Usually, you would do something like

# Connect to Exchange Online
$Mailbox2 = Get-Mailbox -Identity 'MyMailbox1'
# Disconnect from Exchange Online
# Connect to Exchange On-Premises
$Mailbox2 = Get-Mailbox -Identity 'MyMailbox2'
# Disconnect from Exchange On-Premises

While this code would most likely work it's pretty inefficient and in more complicated scenarios not really useful. That's why Import-PSSession has a parameter called Prefix. This gives you a way to connect to Office 365 and Exchange Online at the same time!

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -Prefix O365

# Get-Mailbox from Exchange On-Premises
Get-Mailbox -Identity 'MyMailbox1'

# Get-Mailbox from Exchange Online
Get-O365Mailbox -Identity 'MyMailbox2'

Isn't that cool? It is. But this is pretty common so there's probably 50 pages with information on how to use this. What I actually wanted to write about is the ability to dynamically define that prefix for a command.

So what about that Prefix?

What if one wanted to have a function that you can allow the user to define prefix?

function Connect-WinExchange {
    [CmdletBinding()]
    param(
        [string] $SessionName = 'Exchange',
        [string] $ConnectionURI = 'http://ex2013x3.ad.evotec.xyz/Powershell', # https://outlook.office365.com/powershell-liveid/
        [ValidateSet("Basic", "Kerberos")][String] $Authentication = 'Kerberos',
        [string] $Username,
        [string] $Password,
        [switch] $AsSecure,
        [switch] $FromFile,
        [string] $Prefix,
        [switch] $Output
    )
   # Some Code
    if ($Prefix) {
        #Write-Verbose "Prefix used $Prefix"
        Import-Module (Import-PSSession -Session $Session -AllowClobber -DisableNameChecking -Prefix $Prefix -Verbose:$false) -Global -Prefix $Prefix
    } else {
        #Write-Verbose "Prefix used - None"
        Import-Module (Import-PSSession -Session $Session -AllowClobber -DisableNameChecking -Verbose:$false) -Global
    }
}

Now notice how Prefix in the code above is available as a parameter and how I had to use Import-Module to be able to use Import-PSSession in a separate module. Because of that Prefix has to be also defined for the Import-Module. Otherwise, Prefix within Import-Module gets lost, and all commands work as if there was no prefix at all. But back to the topic. Now that we passed a Prefix to Connect-WinExchange we can try to use it in code.

# Connect to Exchange and set dynamic prefix
Connect-WinExchange -Prefix 'Something'

# After we're connected we now needs to use
Get-SomethingMailbox 

Is this a problem? No, not really. But what if there is another function taking this as a parameter? Things get complicated.

function Get-WinExchangeMailbox {
    [CmdletBinding()]
    param(
        [string] $UserName,
        [string] $Prefix
    )
   # Normally you would do it like 
   Get-SomeMailbox

   # But how to use prefix?
   Get-$PrefixMailbox 
  
   # Or maybe?
   Get-$($Prefix)Mailbox 
}

Well, the code above won't work. I did try it. If only it were that simple right?

I want my answer now

The only way that worked for me (maybe not the only one out there thou) was to use it with Call operator

& "get-$($prefix)mailbox"

Call operator allows you to execute string (among other things) which is what I wanted in this case.

This post was last modified on 3 listopada, 2018 01:30

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

2 tygodnie ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

5 miesięcy ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

7 miesięcy ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

8 miesięcy ago

How to Efficiently Remove Comments from Your PowerShell Script

As part of my daily development, I create lots of code that I subsequently comment…

9 miesięcy ago

Unlocking PowerShell Magic: Different Approach to Creating ‘Empty’ PSCustomObjects

Today I saw an article from Christian Ritter, "PowerShell: Creating an "empty" PSCustomObject" on X…

9 miesięcy ago