Advertisement
Guest User

Untitled

a guest
Mar 9th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  2.  
  3. # In case you don't have a valid certitificate this is needed to supress any SSL errors
  4. Add-Type @"
  5.    using System;
  6.    using System.Net;
  7.    using System.Net.Security;
  8.    using System.Security.Cryptography.X509Certificates;
  9.    public class ServerCertificateValidationCallback
  10.    {
  11.        public static void Ignore()
  12.        {
  13.            ServicePointManager.ServerCertificateValidationCallback +=
  14.                delegate
  15.                (
  16.                    Object obj,
  17.                    X509Certificate certificate,
  18.                    X509Chain chain,
  19.                    SslPolicyErrors errors
  20.                )
  21.                {
  22.                    return true;
  23.                };
  24.        }
  25.    }
  26. "@
  27. [ServerCertificateValidationCallback]::Ignore();
  28.  
  29. Function Get-AuthToken {
  30.  
  31.     Param($LoadBalancer, $User, $Password)
  32.  
  33.  
  34.     #Create the string that is converted to Base64
  35.     $Credentials = $User + ":" + $Password
  36.      
  37.     #Encode the string to base64
  38.     $EncodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($Credentials))
  39.      
  40.     #Add the "Basic prefix"
  41.     $BasicAuthValue = "Basic $EncodedCreds"
  42.      
  43.     #Prepare the headers
  44.     $Headers = @{
  45.         "Authorization" = $BasicAuthValue
  46.         "Content-Type" = "application/json"
  47.     }
  48.  
  49.     #Create the body of the post
  50.     $Body = @{"username" = $User; "password" = $Password; "loginProviderName" = "tmos" }
  51.      
  52.     #Convert the body to Json
  53.     $Body = $Body | ConvertTo-Json
  54.      
  55.     $Response  = Invoke-WebRequest -Method "POST" -Headers $Headers -Body $Body -Uri "https://$LoadBalancer/mgmt/shared/authn/login"
  56.      
  57.     #Extract the token from the response
  58.     $Token = ($Response.content | ConvertFrom-Json).Token.token
  59.    
  60.     Return $Token
  61. }
  62.  
  63. # Creedentials used to login to the LBs
  64. $User = "admin"
  65. $Password = "admin"
  66. # A list of the load balancers
  67. $Loadbalancers = @("192.168.1.10")
  68.  
  69. # The user you want to create
  70. $Payload = @{}
  71. $Payload.description = "BigIPReportUser"
  72. $Payload.password = "password"
  73. $Payload.shell = "none"
  74. $Payload.name = "bigipreportuser"
  75. $Payload.role = "guest"
  76. $Payload.partitionaccess = @()
  77. $Payload.partitionaccess += @{ "role" = "guest"; "name" = "all-partitions" }
  78.  
  79. $JSONPayload = $Payload | ConvertTo-Json -Compress
  80.  
  81. # For every LB, log in and create the user{}
  82. Foreach($LB in $Loadbalancers){
  83.     $AuthToken = Get-AuthToken -Loadbalancer $LB -User $User -Password $Password
  84.     $Headers = @{}
  85.     $Headers["X-F5-Auth-Token"] = $AuthToken;
  86.     $Headers["Content-Type"] = "application/json"
  87.  
  88.     $Response = Invoke-WebRequest -Method "POST" -Body $JSONPayload -Headers $Headers -Uri "https://$LB/mgmt/tm/auth/user"
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement