Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
- # In case you don't have a valid certitificate this is needed to supress any SSL errors
- Add-Type @"
- using System;
- using System.Net;
- using System.Net.Security;
- using System.Security.Cryptography.X509Certificates;
- public class ServerCertificateValidationCallback
- {
- public static void Ignore()
- {
- ServicePointManager.ServerCertificateValidationCallback +=
- delegate
- (
- Object obj,
- X509Certificate certificate,
- X509Chain chain,
- SslPolicyErrors errors
- )
- {
- return true;
- };
- }
- }
- "@
- [ServerCertificateValidationCallback]::Ignore();
- Function Get-AuthToken {
- Param($LoadBalancer, $User, $Password)
- #Create the string that is converted to Base64
- $Credentials = $User + ":" + $Password
- #Encode the string to base64
- $EncodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($Credentials))
- #Add the "Basic prefix"
- $BasicAuthValue = "Basic $EncodedCreds"
- #Prepare the headers
- $Headers = @{
- "Authorization" = $BasicAuthValue
- "Content-Type" = "application/json"
- }
- #Create the body of the post
- $Body = @{"username" = $User; "password" = $Password; "loginProviderName" = "tmos" }
- #Convert the body to Json
- $Body = $Body | ConvertTo-Json
- $Response = Invoke-WebRequest -Method "POST" -Headers $Headers -Body $Body -Uri "https://$LoadBalancer/mgmt/shared/authn/login"
- #Extract the token from the response
- $Token = ($Response.content | ConvertFrom-Json).Token.token
- Return $Token
- }
- # Creedentials used to login to the LBs
- $User = "admin"
- $Password = "admin"
- # A list of the load balancers
- $Loadbalancers = @("192.168.1.10")
- # The user you want to create
- $Payload = @{}
- $Payload.description = "BigIPReportUser"
- $Payload.password = "password"
- $Payload.shell = "none"
- $Payload.name = "bigipreportuser"
- $Payload.role = "guest"
- $Payload.partitionaccess = @()
- $Payload.partitionaccess += @{ "role" = "guest"; "name" = "all-partitions" }
- $JSONPayload = $Payload | ConvertTo-Json -Compress
- # For every LB, log in and create the user{}
- Foreach($LB in $Loadbalancers){
- $AuthToken = Get-AuthToken -Loadbalancer $LB -User $User -Password $Password
- $Headers = @{}
- $Headers["X-F5-Auth-Token"] = $AuthToken;
- $Headers["Content-Type"] = "application/json"
- $Response = Invoke-WebRequest -Method "POST" -Body $JSONPayload -Headers $Headers -Uri "https://$LB/mgmt/tm/auth/user"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement