Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sauceaaron/d7e1df2fefd479524ad875f71582aa94 to your computer and use it in GitHub Desktop.
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
$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