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…
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)?
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:
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:
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?
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.