We manage a handful of clients in office 365 in our tenant. If I cannot to the MSOline service by entering this code 1 line at a time, it works every time. If i use it as part of an actual code block, if works about 50% of the time. Half the time it errors out Stating that “This cmndlet can only be run by partners”, like the first connection line never actually happened.
$UserCredential = Get-Credential
$connectMsol = {
Connect-MSOLservice -Credential $UserCredential
$MSOLTenantid = (get-msolpartnercontract -DomainName domain.onmicrosoft.com).tenantid.guid
Connect-MSOLservice -Credential $UserCredential
}