Skip to content

Instantly share code, notes, and snippets.

@jasonrahm
Created July 20, 2016 16:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonrahm/2bc6958926a8c3ffcebefd0270cbbfae to your computer and use it in GitHub Desktop.
Save jasonrahm/2bc6958926a8c3ffcebefd0270cbbfae to your computer and use it in GitHub Desktop.
Get F5 auth token in powershell
#----------------------------------------------------------------------------
# The contents of this file are subject to the "END USER LICENSE AGREEMENT
# FOR F5 Software Development Kit for iControl"; you may not use this file
# except in compliance with the License. The License is included in the
# iControl Software Development Kit.
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is iControl Code and related documentation
# distributed by F5.
#
# The Initial Developer of the Original Code is F5 Networks,
# Inc. Seattle, WA, USA. Portions created by F5 are Copyright (C) 1996-2009
# F5 Networks, Inc. All Rights Reserved. iControl (TM) is a registered
# trademark of F5 Networks, Inc.
#
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public License (the "GPL"), in which case the
# provisions of GPL are applicable instead of those above. If you wish
# to allow use of your version of this file only under the terms of the
# GPL and not to allow others to use your version of this file under the
# License, indicate your decision by deleting the provisions above and
# replace them with the notice and other provisions required by the GPL.
# If you do not delete the provisions above, a recipient may use your
# version of this file under either the License or the GPL.
#----------------------------------------------------------------------------
param(
[string]$Bigip = "",
[string]$User = "",
[string]$Pass = "",
[string]$Type = "",
[string]$Name = ""
);
$AUTH_TOKEN = $null;
function Get-AuthToken()
{
$token = $null;
if ( $null -ne $AUTH_TOKEN ) {
$token = $AUTH_TOKEN;
} else {
$uri = "/mgmt/shared/authn/login";
$link = "https://$Bigip$uri";
$headers = @{};
$body = "{'username':'$User','password':'$Pass','loginProviderName':'tmos'}";
# Call token API
$obj = Invoke-RestMethod -Method POST -Headers $headers -Uri $link -Body $body
$token = $obj.token.token;
}
Write-Host "TOKEN: $token"
return $token;
}
#----------------------------------------------------------------------------
function Get-VirtualList()
#
# Description:
# This function lists all virtual servers.
#
# Parameters:
# None
#----------------------------------------------------------------------------
{
$uri = "/mgmt/tm/ltm/virtual";
$link = "https://$Bigip$uri";
$headers = @{};
$headers.Add("X-F5-Auth-Token", $(Get-AuthToken));
$obj = Invoke-RestMethod -Method GET -Headers $headers -Uri $link
$items = $obj.items;
Write-Host "Virtual NAMES";
Write-Host "----------";
for($i=0; $i -lt $items.length; $i++) {
$name = $items[$i].fullPath;
Write-Host " $name";
}
}
#============================================================================
# Main application logic
#============================================================================
Get-VirtualList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment