Connect-MsolService -EA stop

Tagged: , ,

This topic contains 7 replies, has 4 voices, and was last updated by Profile photo of Dan Potter Dan Potter 10 months, 3 weeks ago.

  • Author
    Posts
  • #33539
    Profile photo of Dan Davis
    Dan Davis
    Participant

    I am trying to stop my script if my connection to O365 fails with -EA Stop but not having any luck so far.

    my code

    Try {
                    Write-Verbose 'Connecting to $MSOLServer'
                    # Create the session to MSOLServer
                    $S = New-PSSession -ComputerName $MSOLServer -Credential $MSOLServerCredImport -EA Stop
                    # Import the O365 cmdlet on the $MSOLServer
                    Invoke-Command -Session $S {Import-Module MSOnline} -EA stop
                    # Export the $MSOLServer cmdlet from remote to local powershell instance
                    Import-PSSession -Session $S -Module MSOnline -AllowClobber -EA Stop
                    # Connect to MSOnline
                    Connect-MsolService -Credential $MSOLOnlineCredImport -EA stop
                } Catch {
                    Write-Verbose 'Cant connect to O365 server or O365'
                    # If we are logging write it to the log
                    if ($Log) {
                        'Cant connect to O365 server or O365' | Out-File $ErrorLogFile_FP -Append
                        # Pull back some of the detaill PS errors for the logs
                        "$_" | Out-File $ErrorLogFile_FP -Append
                    }
                    # If the connection fails there is no point in continuing
                    Write-Verbose 'Cant continue without a session so bye bye'
                    If ($log) {
                        'Cant continue without a session so bye bye' | Out-File $ErrorLogFile_FP -Append
                    }
                    Exit
                }   
    

    The -EA Stop works on all lines apart from

    Connect-MsolService -Credential $MSOLOnlineCredImport -EA stop
    

    If I pass the wrong credentials I get the error

    Unable to authenticate your credentials. Make sure that your user name is in the format: @. If this issue persists, contact Support.
    + CategoryInfo : OperationStopped: (:) [Connect-MsolService], MicrosoftOnlineException
    + FullyQualifiedErrorId : 0x80048862,Microsoft.Online.Administration.Automation.ConnectMsolService

    But the script continues rather than dropping into the catch and then exiting the script

    anyone else have the same problem?

  • #33544
    Profile photo of Don Jones
    Don Jones
    Keymaster

    It's possible the cmdlet isn't throwing a terminating exception that PowerShell can actually catch. That's unfortunately a cmdlet decision decision and not necessarily something you can work around, unfortunately. I'm a bit surprised that New-PSSession isn't barfing when you pass a bad credential.

    Are you sure New-PSSession works with an incorrect credential, but doesn't toss an exception?

  • #33546
    Profile photo of Dan Potter
    Dan Potter
    Participant

    try catch works for me. It's really simple to not execute following code in powershell by setting a random variable and testing the condition.

    Try{

    Connect-MsolService -Credential $credentials}

    Catch{
    $e -eq $true}

    if($e -ne $true){'rest of code'}

  • #33547
    Profile photo of Dan Potter
    Dan Potter
    Participant

    This is exactly how I do it although I don't worry about executing the rest of the code as it won't do anything anyways.

    Try{

    Connect-MsolService -Credential $credentials}

    Catch{

    write-eventlog -LogName "O365 User Script" -Source "O365 User Script" -EventId 55559 -EntryType Error -Category 0 -Message "Could not connect to MSOLService"}

  • #33548
    Profile photo of Dan Davis
    Dan Davis
    Participant

    Don,

    -EA stop works for the lines

    $S = New-PSSession -ComputerName $MSOLServer -Credential $MSOLServerCredImport -EA Stop
    Invoke-Command -Session $S {Import-Module MSOnline} -EA Stop
    Import-PSSession -Session $S -Module MSOnline -AllowClobber -EA Stop
    

    its only the connect-msolservice i am having problems with

    Connect-MsolService -Credential $MSOLOnlineCredImport -EA Stop
    

    perhaps, as you say, that the error is not catch-able. But if others can get it to work I will keep looking.

    Dan,
    since my connect-msol line never seems to drop into the catch loop I cant change a value there.

    Cheers.

  • #33549
    Profile photo of Dan Potter
    Dan Potter
    Participant

    sorry I used -eq when it should have been =, I always do that:-)

    Try{

    Connect-MsolService -Credential $credentials -ea 'stop'}

    Catch{
    $e = $true}

  • #33873
    Profile photo of Dan Davis
    Dan Davis
    Participant

    In case anyone else comes across this, I never go it working so I am doing this as a work around. Connect up to O365 then,

    # Because -EA stop is not working. Try a MSOL command if it fails assume there has been a problem and exit
            $CheckO365 = Get-MsolDomain -EA SilentlyContinue
            if ($CheckO365) {
                # Connected to O365
                Write-Verbose 'Connected to O365'
            } else {
                Write-Verbose 'Cant connect to O365 server or O365'
                # If we are logging write it to the log
                # If the connection fails there is no point in continuing
                Write-Verbose 'Cant continue without a session so bye bye'
                Exit
            }
    
  • #33877
    Profile photo of Dan Potter
    Dan Potter
    Participant

    I don't know why you're not catching an error. This is what I get when I enter a bad password.

    PS H:\> Connect-MsolService -Credential $credentials -ea 'stop'
    Connect-MsolService : Exception of type 'Microsoft.Online.Administration.Automation.MicrosoftOnlineException' was
    thrown.
    At line:1 char:1
    + Connect-MsolService -Credential $credentials -ea 'stop'
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OperationStopped: (:) [Connect-MsolService], MicrosoftOnlineException
    + FullyQualifiedErrorId : 0x800488F2,Microsoft.Online.Administration.Automation.ConnectMsolService

    PS H:\> $error[0]
    The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to
    Stop: Exception of type 'Microsoft.Online.Administration.Automation.MicrosoftOnlineException' was thrown.

You must be logged in to reply to this topic.