Connecting to O365 for automated scripts.

I’m in need of a sanity check. I’ve got several scripts that run that process terminated/inactive users in our environment. I’ve been using this same chunk of code for months.

#Credential setup for O365
Import-Module MSOnlineExtended
$O365User = “user@domain.com
$O365Pass = “pass” | ConvertTo-SecureString -AsPlainText -Force
$O365Creds = New-Object System.Management.Automation.PSCredential ($O365User, $O365Pass)

Function Connect-O365
{
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $O365Creds -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
Import-Module Msonline
Connect-MSOLService -credential $O365Creds
}

Import-Module ActiveDirectory

Connect-O365

but as of some point yesterday I’m getting this error.

Connect-MSOLService : Could not load file or assembly ‘Microsoft.Online.Administration.Automation.PSModule.Resources, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’
or one of its dependencies. The system cannot find the file specified.
At line:12 char:2

  • Connect-MSOLService -credential $O365Creds
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (Microsoft.Onlin…nectMsolService:ConnectMsolService) [Connect-MsolService], FileNotFoundException
    • FullyQualifiedErrorId : System.IO.FileNotFoundException: Could not load file or assembly ‘Microsoft.Online.Administration.Automation.PSModule.Resources, Version=1.0.0.0, Culture=n
      eutral, PublicKeyToken=31bf3856ad364e35’ or one of its dependencies. The system cannot find the file specified.
      File name: ‘Microsoft.Online.Administration.Automation.PSModule.Resources, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’
      at Microsoft.Online.Administration.Automation.ConnectMsolService.MsolCmdletProcessRecord()
      at Microsoft.Online.Administration.Automation.MsolCmdlet.ProcessRecord()
      WRN: Assembly binding logging is turned OFF.
      To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
      Note: There is some performance penalty associated with assembly bind failure logging.
      To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
      ,Microsoft.Online.Administration.Automation.ConnectMsolService

Connect-MSOLService : Could not load file or assembly ‘Microsoft.Online.Administration.Automation.PSModule.Resources, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’
or one of its dependencies. The system cannot find the file specified.
At line:12 char:2

  • Connect-MSOLService -credential $O365Creds
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:slight_smile: [Connect-MsolService], FileNotFoundException
    • FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.Online.Administration.Automation.ConnectMsolService

This happens across multiple machines and multiple different accounts. Any help or recommendations would be greatly appreciated.

I’d day something looks borked. Have you tried re-installing the MSOL cmdlets?

Yeah I’ve done a fresh install of the MSOL/Azure software on a 2012r2 box and get the same error. weird part is I’ve got this code running in a powershell studio built app working fine. and it’s wrapped up with another function

Function New-Connection
{
#credential setup for local exchange
$LocalUser = “user”
$LocalPass = “pass” | ConvertTo-SecureString -AsPlainText -Force
$LoginCreds = New-Object System.Management.Automation.PSCredential ($LocalUser,$LocalPass)

#Credential setup for O365
$O365User = "user@domain.com"
$O365Pass = "pass" | ConvertTo-SecureString -AsPlainText -Force
$O365Creds = New-Object System.Management.Automation.PSCredential ($O365User,$O365Pass)

# O365 Function
Function Connect-O365
	{
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $O365Creds -Authentication Basic -AllowRedirection
		Import-PSSession $Session -AllowClobber
		Import-Module Msonline
		Connect-MSOLService -credential $O365Creds
	}

# Local Exchange Function
Function Connect-LocalExchange
	{
	param($server="cas1.domain.net")
	$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$Server/PowerShell/ -Authentication Kerberos -Credential $logincreds
	Import-PSSession $Session -Prefix "L" -AllowClobber
	}

# Connecting and improrting modules
    Connect-LocalExchange
    Connect-O365
    Import-Module ActiveDirectory
    Import-Module MSOnline

}

The only thing I can think of is that there’s either a 32-bit/64-bit mismatch, or a .NET Framework version mismatch. Something’s changed, if it used to work but doesn’t now. You might hop on SAPIEN’s forums and see if they have any suggestions, since it’s working in Studio.

good idea. I’ll try there. Thanks for the assistance.