Advertisement
Guest User

Untitled

a guest
May 21st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $application = "https://secret.coyote.com";
  2. Function Convert-SSToString([System.Security.SecureString]$ObsPass){
  3.     return [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($ObsPass))
  4. }
  5. Function Get-Token{
  6.     [CmdletBinding()]
  7.     Param(
  8.         [Switch] $UseTwoFactor
  9.     )
  10.  
  11. # Define the user credentials
  12.  
  13. $cred = Get-Credential -credential "$env:userdomain\$env:username"
  14.  
  15.     $creds = @{
  16.         username = $cred.UserName
  17.         password = (Convert-SSToString $cred.Password)
  18.         grant_type = "password"
  19.     };
  20.  
  21.     $headers = $null
  22.     If ($UseTwoFactor) {
  23.         $headers = @{
  24.             "OTP" = (Read-Host -Prompt "Enter your OTP for 2FA: ")
  25.         }
  26.     }
  27.  
  28.     try
  29.     {
  30.         $response = Invoke-RestMethod "$application/oauth2/token" -Method Post -Body $creds -Headers $headers;
  31.         $token = $response.access_token;
  32.         return $token;
  33.     }
  34.     catch
  35.     {
  36.         $result = $_.Exception.Response.GetResponseStream();
  37.         $reader = New-Object System.IO.StreamReader($result);
  38.         $reader.BaseStream.Position = 0;
  39.         $reader.DiscardBufferedData();
  40.         $responseBody = $reader.ReadToEnd() | ConvertFrom-Json
  41.         Write-Host "ERROR: $($responseBody.error)"
  42.         return;
  43.     }
  44. }
  45.  
  46. $token = Get-Token -UseTwoFactor
  47. $api = "https://secret.coyote.com/api/v1"
  48. $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  49. $headers.Add("Authorization", "Bearer $token")
  50.  
  51. #pull down live working dependency
  52. $livestub = Invoke-RestMethod "$api/secret-dependencies/8" -Method get -Headers $headers
  53.  
  54. #generalize settings for dependency
  55. $livestub.id = 0
  56. $livestub.groupId = 0
  57. $livestub.secretId = 3288
  58. $livestub.sortOrder = 0
  59.  
  60. $body = $livestub | ConvertTo-Json
  61.  
  62. Invoke-RestMethod "$api/secret-dependencies" -Method Post -Body $body -Headers $headers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement