Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Connecting to Exchange Online using module vs. manually : module wont work

$
0
0

I'm nearly at my wits' end. 

I have installed MSOnline, and Active Directory modules for PS 3.0.

I've created a PS module which contains a function, "connect-exchange" which simply gets my credentials, creates a new session for Microsoft Exchange online, then connects to MSonline and imports the PSsession for exchange. I run PS as administrator (windows 7), set-executionpolicy unsigned, open $profile and add in the modules I'll use most (Active Directory, MS Online, and my custom module). I run PS as my normal user, and get-module reports all three modules are loaded, including my custom one. I run the function "connect-exchange" and it goes as expected. I am redirected to two URI's and a Module name is listed with exported commands pertinent to managing Exchange Online. Great. The problem is thatnone of the cmdlets work, and the Exchange Online tmp module does not show up in get-module.

The kicker is that If I open my custom module with the connect-exchange function, highlight the actual commands in the function and run them manually, everything works properly. 

Letting PS do the module makes it appear to work, except nothing related to my exchange session shows up in get-module, and none of the cmdlets are available. Manually running the exact same code that is in the module makes it work.

This is my custom module, which loads when I start powershell:

Function Connect-Exchange()
{
    $cred = Get-Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $cred -Authentication Basic -AllowRedirection
    Connect-MsolService -Credential $cred
    Import-PSSession $Session
}

Running "connect-exchange" does connect, and this is the output (it is identical to the output as when I run the commands in the function manually, as would normally be expected):

PS H:\> Connect-Exchange
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
WARNING: Your connection has been redirected to the following URI: "https://pod51009psh.outlook.com/powershell-liveid?PSVersion=3.0 "
WARNING: Your connection has been redirected to the following URI: "https://by2prd0412psh.outlook.com/powershell-liveid?PSVersion=3.0 "

ModuleType Name                                ExportedCommands                                                                       
---------- ----                                ----------------                                                                       
Script     tmp_j1a0we4q.sad                    {Add-AvailabilityAddressSpace, Add-DistributionGroupMember, Add-MailboxFolderPermiss...

Get-module doesn't give me this module, tmp_j1a0we4q.sad. No Script modules are listed that give me any exchange exported commands. Get-PSsession shows 1 session, opened with Microsoft.Exchange . No exchange online cmdlets are available to me at this point (including any of the ExportedCommands listed above).

If I remove the session, open the custom module (listed at the top of this message), highlight the code in the connect-exchange function, then F8, I get the exact same output as the 2nd box listed above, but my exchange cmdlets are available and usable.

Any ideas why the function which is part of a module which is imported at the start of each PS session does not seem to work the same as running the commands in the function independently?


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>