# How to test if I have a an active SpoService connection before starting a new 1

**URL:** <https://forums.powershell.org/t/how-to-test-if-i-have-a-an-active-sposervice-connection-before-starting-a-new-1/15365>\
**Category:** PowerShell Help\
**Created:** [December 9, 2020, 2:17pm UTC](https://forums.powershell.org/t/how-to-test-if-i-have-a-an-active-sposervice-connection-before-starting-a-new-1/15365 "2020-12-09T14:17:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawsonweb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dawsonweb/32/22_2.png) [@dawsonweb](https://forums.powershell.org/u/dawsonweb)\
**Post date:** [December 9, 2020, 2:17pm UTC](https://forums.powershell.org/t/how-to-test-if-i-have-a-an-active-sposervice-connection-before-starting-a-new-1/15365/1 "2020-12-09T14:17:37Z")

</div>

Hi,

I’m writing a bit of code that tests if an existing SpoService exists by watching for failure of a Get-SpoSite:

`try`  
`{`  
`$SPOSessions = Get-SpoSite $OneDriveAdminURL | Select-Object -Property Url`  
`$SPOSessionExists = (@($SPOSessions) -like “@{Url=$($OneDriveAdminURL)”).Count -gt 0`  
`$SPOSessionExists = (Get-PnpConnection | Where-Object Url -eq $OneDriveAdminURL | Select-Object -Property Url | Measure-Object).Count -gt 0`  
`}`  
`catch`  
`{`  
`$SPOSessionExists = $false`  
`}`

It works, but I always get an error in the error stack:

`Exception Details:`  
`Message : No connection available. Use Connect-SPOService before running this CmdLet.`

I thought putting in in a try/catch would avoid the error? Anyone know how I could test without generating an error?

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [December 10, 2020, 3:12am UTC](https://forums.powershell.org/t/how-to-test-if-i-have-a-an-active-sposervice-connection-before-starting-a-new-1/15365/2 "2020-12-10T03:12:02Z")

</div>

By default the catch statement will only handle terminating errors. This is not a terminating error. To properly handle this exception either make the command causing the error create terminating errors only with the common parameter/value of -ErrorAction Stop. In you post I don’t see the full error message so I’m not sure which command is causing it.

---

<div class="post-metadata">

**Author:** ![dawsonweb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dawsonweb/32/22_2.png) [@dawsonweb](https://forums.powershell.org/u/dawsonweb)\
**Post date:** [December 10, 2020, 6:44am UTC](https://forums.powershell.org/t/how-to-test-if-i-have-a-an-active-sposervice-connection-before-starting-a-new-1/15365/3 "2020-12-10T06:44:18Z")

</div>

Thanks Mike R.

I did this and it worked:

` try`  
`{`  
`$SPOSessionExists = (Get-SPOSite | Measure-Object).Count -gt 0`  
`}`  
`catch [System.InvalidOperationException]`  
`{`  
`$SPOSessionExists = $false`  
`$Error.Remove($error[$Error.Count - 1]) # removes the error that has been trapped so the script can close cleanly`  
`}`  
`catch`  
`{`  
`$SPOSessionExists = $false`  
`}`

Thanks,

Michael

&nbsp;

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 10, 2020, 7:49am UTC](https://forums.powershell.org/t/how-to-test-if-i-have-a-an-active-sposervice-connection-before-starting-a-new-1/15365/4 "2020-12-10T07:49:44Z")

</div>

The cmdlet throws an error that explains that you need to use Connect-SPOSite again. The only reason you would catch that specific exception is if you put code in there to do something like, Connect-SPOSite. Using the try\catch you can add some details to the error message if you want, otherwise, you are just setting a flag for any error in both cases. Try something like this:

```
$site = 'https://contoso.sharepoint.com'

try {
    $spoSite = Get-SPOSite -Identity $site -ErrorAction Stop
}
catch {
    $msg = 'Error connecting to Share Point site {0}. {1}' -f $site, $_
    Throw $msg
}

if ($spoSite) {
    #Do stuff
}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/how-to-test-if-i-have-a-an-active-sposervice-connection-before-starting-a-new-1/15365/5 "2024-05-16T20:28:10Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


