Last active
August 18, 2017 23:18
-
-
Save sauceaaron/d7e1df2fefd479524ad875f71582aa94 to your computer and use it in GitHub Desktop.
Use Powershell to call the Sauce REST API to shutdown Sauce Connect tunnels using Basic Authentication header
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $username = $(Get-ChildItem Env:SAUCE_USERNAME).value | |
| $accessKey = $(Get-ChildItem Env:SAUCE_ACCESS_KEY).value | |
| $credentials = "$username`:$accessKey" | |
| $encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credentials)) | |
| $headers = @{ Authorization = "Basic $encodedCredentials" } | |
| $sauceRestUrl = "https://saucelabs.com/rest/v1" | |
| $userEndpoint = "$sauceRestUrl/users/$username" | |
| $tunnelsEndpoint = "$sauceRestUrl/$username/tunnels" | |
| $user = Invoke-WebRequest -Uri $userEndpoint -Headers $headers | ConvertFrom-Json | |
| $tunnels = Invoke-WebRequest -Uri $tunnelsEndpoint -Headers $headers | ConvertFrom-Json | |
| ForEach ($tunnelId in $tunnels) | |
| { | |
| $tunnel Invoke-WebRequest -Headers $headers -Uri $tunnelsEndpoint/$tunnelId | ConvertFrom-Json | |
| If ($tunnel.status -eq 'running') | |
| { | |
| echo "shutting down $($tunnel.id) $($tunnel.tunnel_identifier)" | |
| Invoke-WebRequest -Method Delete -Headers $headers -Uri $tunnelsEndpoint/$tunnelId | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment