For one of my applications I have a Windows service (on Windows Server 2012 R2 x64) which role is to execute many jobs on differents schedules and triggers. One of them is to reset a user password on Office 365. The server on which the service runs has the Microsoft Online Services Sign-in Assistant and the Microsoft Azure Active Directory Module for Windows PowerShell installed (MSOnline version 188.8.131.52).
From PowerShell I can successfuly call the following with my user.
$> $cred = Get-Credential $> Connect-MsolService -Credential $cred
If I run PowerShell as the account my service is started with it also runs fine. Running the reset password function from the Windows service fail with the following message :
The term 'Connect-MsolService' 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.
Adding a debug flag to the module import in the code allowed us to drag down the problem to the import-module MSOnline command. We got the error :
The specified module 'MSOnline' was not loaded because no valid module file was found in any module directory.
We already tried to remove and reinstall in x64 version the two tools (Microsoft Online Services Sign-in Assistant and the Microsoft Azure Active Directory Module for Windows PowerShell). System variable "PATH" is correct with : %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft\Web Platform Installer\
Folder C:\Windows\System32\WindowsPowerShell\v1.0\Modules has the MSOL module subfolders MSOnline and MSOnlineExtended
Copying the two folders in C:\Windows\sysWOW64\WindowsPowerShell\v1.0\Modules as reported as solution in many other topics fails here with the following error :
System.Management.Automation.CmdletInvocationException: Could not load file or assembly 'file:///C:\Windows\system32\WindowsPowerShell\v1.0\Modules\MSOnline\Microsoft.Online.Administration.Automation.PSModule.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format. ---> System.BadImageFormatException: Could not load file or assembly
The code used for the Windows service has been run and tested successfuly as a separate tool on the same server and the code has also been run flawlessly on a developer machine.
These investigations leads us to think there is some kind of issue with the service itself but can't figure out what/why.