Skip to content

Instantly share code, notes, and snippets.

@pstephenson02
Last active February 19, 2021 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pstephenson02/3cf2dc3b9d68db28722ad568c9eb49eb to your computer and use it in GitHub Desktop.
Save pstephenson02/3cf2dc3b9d68db28722ad568c9eb49eb to your computer and use it in GitHub Desktop.
$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