Connect-MsolService error after importing MSOnline module

0

I'm receive the following error when I run Connect-MsolService -

Exception of type 'Microsoft.Online.Administration.Automation.MicrosoftOnlineException' was thrown. + CategoryInfo : OperationStopped: (:) [Connect-MsolService], MicrosoftOnlineException + FullyQualifiedErrorId : 0x80090345,Microsoft.Online.Administration.Automation.ConnectMsolService + PSComputerName : xxxxx

Background - I am using powershell to manage Active Directory, Exchange and Sharepoint Online user data. I have an SSIS package that dynamically builds arguments passed to powershell scripts for creating new and updating existing AD user data, adding/updating necessary security and distribution groups for each user, enabling exchange mailboxes, and passing custom attribute data to Sharepoint online user profiles.

In effort to maintain the purity of the SQL server running the SSIS package, dynamic sessions are created to servers having the modules necessary for import. This allows me to do all of the things mentioned above without having to install the different modules on my SQL server.

My final hurdle is automating the Office 365 licensing. To script licensing assignments I create a session to our server having the necessary MSOnline module and import it giving me access to the Cmdlets, like Set-MsolUserLicense. However, before I can use the Cmdlets I have to connect to the MSOLService.

When I run Connect-MsolService, the credentials modal pops as expected, the appropriate credentials are entered (which have been verified a thousand times over), and I receive the error shown above.

These commands all work fine when I run them from the server where the MSOnline cmdlets are installed, but return the error when run post successful import-module on my SQL server.

Here are the commands used for establishing the session and importing the MSOnline module -

$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $Username, $securePassword

$outputSession = $Null
foreach($session in Get-PSSession){
    if(($session.ComputerName -eq $Server) -and ($session.Availability -eq "Available")){
        $outputSession = $session
    }
}
if ($outputSession -eq $Null){
    $outputSession = New-PSSession -ComputerName $Server -Credential $cred -Name 'MSOnline'
    Invoke-Command -Session $outputSession -ScriptBlock {Import-Module MSOnline}
    Import-PSSession -session $outputSession -module MSOnline -AllowClobber
}

Connect-MSOLService is the only Cmdlet giving me an issues using these methods. Any help you can provide will be appreciated.

powershell
ssis
office365
asked on Stack Overflow Jul 18, 2014 by Rocky • edited Jul 18, 2014 by 4444

1 Answer

0

Sigh. This appears to be a bug with single sign on assistant and MSOL module. I have had this issue reproduced reliably and had it reported to Office 365 support. It went nowhere. To state it simply, Connect-MSOnline works only in local sessions.

Mind you, MSOL module is a very strange beast, it is not using PSSession to maintain connection with Azure but uses some internal mechanisms that must rely on single sign-on assistant. This may be why in remote session MSOL commandlet cannot connect to Azure AD.

It was a dead end for me, and the only way to work around it is to write an application that runs on the server using MSOL module and accepts commands from the client or is invoked on server directly via service or scheduled task.

answered on Stack Overflow Jul 18, 2014 by Serjx86

User contributions licensed under CC BY-SA 3.0