Skip to content

Instantly share code, notes, and snippets.

@pstephenson02
Last active Feb 19, 2021
Embed
What would you like to do?
$octopusUrl = 'https://samples.octopus.app'
$username = 'admin'
$password = 'your-password'
$loginPath = "$octopusUrl/api/users/login"
$response = Invoke-WebRequest -Method Post `
-Uri $loginPath `
-Body (@{'Username' = $username;'Password' = $password} | ConvertTo-Json) `
-UseBasicParsing `
-SessionVariable 'session'
$userId = ($response.Content | ConvertFrom-Json).Id
$csrfToken = $session.Cookies.GetCookies($loginPath) `
| Where-Object { $_.Name.StartsWith('Octopus-Csrf-Token') }
$sessionToken = $session.Cookies.GetCookies($loginPath) `
| Where-Object { $_.Name.StartsWith('OctopusIdentificationToken') }
$headers = @{
'Cookie' = $sessionToken.Name + '=' + $sessionToken.Value
'X-Octopus-Csrf-Token' = $csrfToken.Value
}
Invoke-RestMethod -Method Post `
-Uri "$octopusUrl/api/users/$userId/apikeys" `
-Headers $headers `
-Body (@{'Purpose'='Automation'} | ConvertTo-Json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment