O365 script not working in module

This topic contains 7 replies, has 4 voices, and was last updated by Profile photo of sponsen sponsen 2 years, 2 months ago.

  • Author
    Posts
  • #19310
    Profile photo of sponsen
    sponsen
    Participant

    Hi,

    found this rather strange issue today while connecting to O365.

    I have this basic script:

    $LiveCred = Get-Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
    Import-PSSession $Session -allowclobber
    Connect-MsolService -Credential $LiveCred

    If I just copy-paste it into console, everything works.
    If I make a module of it like the one you see below, Get-OrganizationalInfo (and the other Get-Organization cmdlets) dont work!
    I am able to run Get-MsolUser and Get-Mailbox etc, tho..

    function Connect-O365 {
    Process{
    $LiveCred = Get-Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
    Import-PSSession $Session -allowclobber
    Connect-MsolService -Credential $LiveCred
    }
    }
  • #19312
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Everything else is exactly the same? You're running this on the same computer? When pasting this into the console, it works when pasted into a brand-new console window, where you'v run no other commands (including having anything in your profile)?

    • #19468
      Profile photo of sponsen
      sponsen
      Participant

      Everything else is exactly the same? You're running this on the same computer? When pasting this into the console, it works when pasted into a brand-new console window, where you'v run no other commands (including having anything in your profile)?

      Hi,

      I am running this on one computer. Win 8.1.

      I went back and tried again now to be sure. So first of, I removed everything in my profile and removed the modules. Then I opened a new console (run as admin; as my domain admin account, not the one I'm logged in as (logged in as only domain user)).
      Then i copy-pasted each line separately into the console. Get-Org..... cmdlets works.

      Then I only put Import-Module "O365" in my Microsoft.PowerShell_profile.ps1 file (under my domain admin account profile). After that i created the path: domain.admin\Documents\WindowsPowerShell\Modules\O365\O365.psm1. In that file I put:

      function O365 {
      Process{
      $LiveCred = Get-Credential
      $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
      Import-PSSession $Session -allowclobber
      Connect-MsolService -Credential $LiveCred
      }
      }

      Then restarted the console and ran the command "O365". Now Get-Org.... cmdlets don't work.

      Hi Sponsen. Is it your formatting for this post, or are you actually passing html code as a connection uri?

      You're right! Formatting is messing it up! 🙂 Tried to fix it in edit but it did not work.

      Thanks to both of you for responding!

  • #19343
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hi Sponsen. Is it your formatting for this post, or are you actually passing html code as a connection uri?

  • #19472
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    Sponsen,

    I ran into this same problem when I started creating an Office 365 module for functions I use regularly. I think this is because O365 is using implicit remoting so you need to import the Exchange module from the session. Try this:

    function Connect-O365Exchange
    {
        param
        (
            [System.Management.Automation.Credential()]
            $Credential
        )
        Connect-MsolService -Credential $Credential
        $params =   @{
            ConfigurationName = 'microsoft.exchange'
            ConnectionUri = 'https://ps.outlook.com/powershell'
            Credential = $Credential
            Authentication = 'Basic'
            AllowRedirection = $True
        }
        $ExchSession = New-PSSession @params
        Import-Module -ModuleInfo (Import-PSSession -Session $ExchSession -AllowClobber -WarningAction SilentlyContinue) -Global -DisableNameChecking
    }
    

    Also I call Connect-MsolService first. I don't know if this is required but that is the order that the MS documentation uses.

  • #19506
    Profile photo of sponsen
    sponsen
    Participant

    Thanks a lot Matt!

    That works. I also tried to only add your Import-Module line into my function, and that worked as well.
    Funny thing tho – I got my script directly from MS a time ago.. 🙂

    But I'm still not comfortable with this situation; why isn't functions working as console one-liners?
    Why do we have to import that module in functions when we don't have to in one-liners?

  • #19509
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    I can't take credit for this as I stumbled on the answer a while ago here:

    http://social.technet.microsoft.com/Forums/en-US/529bd0ef-5e88-4808-a5ac-dc07ca8660f3/importpssession-is-not-importing-cmdlets-when-used-in-a-custom-module?forum=winserverpowershell

    I think the issue is caused by the way modules and implicit remoting work. When you import the commands from a remote session this is called implicit remoting and this is how the Exchange Online cmdlets work. So when you call these from your powershell session the commands are imported into the global scope. However, modules have their own private scope as well and the implicit commands get imported into that. Any commands that live in that module will have access to the implicit commands but they won't be available on the command line since you will only see what is in the global scope.

    I wonder if creating a module manifest with the appropriate nested modules entry might work here too, but the problem is that the Exchange Online Module is imported using a dynamically generated name each time you import the implicit commands so there is no way I know of to pin this down.

  • #19535
    Profile photo of sponsen
    sponsen
    Participant

    I see. Thanks again 🙂 🙂

    Have a nice day!

You must be logged in to reply to this topic.