Session Exchange online out of scope when in a function?

Welcome Forums General PowerShell Q&A Session Exchange online out of scope when in a function?

This topic contains 1 reply, has 2 voices, and was last updated by

3 months ago.

  • Author
  • #162333

    Topics: 2
    Replies: 0
    Points: 25
    Rank: Member

    Please help me understand something.

    I have this simple code:

    If i use Get-User or Get-MailboxUser does not matter, the error is the same.

    function adb_temp(){
    $365master = $cred = get_local_credential
    New-O365ExchangeSession $365master # call the office365 remote connection function
    $users = Get-User -ResultSize 1000

    When i run this i get this error:

    Get-User : The term 'Get-User' 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.

    If i just run the code inside the function so just highlight it and run it, it works fine.

    I can not find out what is wrong. In both cases the Exchange session is set up correctly.

    I have a function that does this:

    Function New-O365ExchangeSession()
    #close any old remote session
    Get-PSSession | Remove-PSSession -Confirm:$false
    #start a new office 365 remote session
    $cred = get_local_credential
    $ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "" -Credential $cred -Authentication "Basic" -AllowRedirection
    #Import-PSSession $ExchangeSession 
    $office365 = Import-PSSession $ExchangeSession -AllowClobber -DisableNameChecking
    write-host "Exchange Session is connected and Imported"

    Is the function doing something else with my session or what?








  • #162395

    Topics: 2
    Replies: 999
    Points: 1,946
    Helping Hand
    Rank: Community Hero

    You only need to set you EXO session once for the entire time you are using it.

    With what you have here, you seem to be trying to set this up every time you run the function.

    All programming languages have a scope and thus times in scope for one this are not necessarily in scope for another, unless you define it to be.

    See the help files on Scope or other blogs on that topic.

    about_scopes | Microsoft Docs

    How Scopes Affect PowerShell Scripts

    Scopes – PowerShell –

    Video by Don Jones, Author of Powershell in a Month of Lunches.

    Windows PowerShell Fundamentals Chapter 13 – Scope

    There are prebuilt scripts and model via the MS  and that provides what you are trying to do, that you can use as is or tweak as needed.

    Displaying results 1 – 20 of 39 (Page 1 of 2)

    Office 365 – resources for IT professionals

    You also will want to get in the habit of removing your sessions, cleaning up after sessions immediately after your code that is doing the work runs to avoid other potential complications, vs looking for open sessions before you start a new one. In most cases, there is little reason to close a remote session, if you know you are going to use most of your day. Now, sessions to drop for many different reasons, and you will need to reestablish it. So, for the New-O365ExchangeSession function, I'd check for an open session and just use it, vs closing it to start new one. Only start new if you have to.

    Also, get in the habit of using the -Prefix parameter in sessions, so that you know you are using the right cmdlets for what you are working on.  For Example, if you were doing this on an Exchange server, then you'd end up with two sets of the same cmdlets, and thus end up in conflict scenarios. So, for example in , on-prem or ONline session, use:

    -Prefix EXO

    -Prefix EXP

    … then when you call cmdlets, it's Get-ExoMail or Get-ExpMail. This is documented on TechNet

    and the MS Docs sites


The topic ‘Session Exchange online out of scope when in a function?’ is closed to new replies.