GlobalLB DNS create and state examples using PowerShell
Problem this snippet solves:
This PowerShell example illustrates how to perform the following GTM tasks using the iControl Cmdlets for PowerShell.
- GTM Data Center (Create, Enable, Disable)
- GTM Server (Create)
- GTM Virtual Server (Create, Enable, Disable)
- GTM Pool (Create)
- GTM Wide IP (Create)
How to use this snippet:
The script includes sample usages in the main script logic. You will have to change the input parameters to account for your specific configurations.
- Save script as GlobalLBCreate.ps1
- Edit main script logic to perform the tasks you are interested in.
- $ .\GlobalLBCreate.ps1 bigip_addr bigip_user bigip_pass
Code :
#----------------------------------------------------------------------------
# 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-2016 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 (
$g_bigip = $null,
$g_uid = $null,
$g_pwd = $null
);
Set-PSDebug -strict;
#-------------------------------------------------------------------------
function Write-Usage()
#-------------------------------------------------------------------------
{
Write-Host "Usage: GlobalLBCreate.ps1 host uid pwd";
exit;
}
#-------------------------------------------------------------------------
function Create-GTMDataCenter()
#-------------------------------------------------------------------------
{
param(
[string]$name = $null,
[string]$location = $null,
[string]$contact = $null
);
if ( $name -and $location -and $contact ) {
$data_center = New-Object -TypeName iControl.GlobalLBDataCenterDataCenterAttribute;
$data_center.name = $name;
$data_center.location = $location;
$data_center.contact = $contact;
$data_centerA = (, $data_center);
(Get-F5.iControl).GlobalLBDataCenter.create($data_centerA);
} else {
Write-Usage;
}
}
#-------------------------------------------------------------------------
function Set-GTMDataCenterState()
#-------------------------------------------------------------------------
{
param(
[string]$name = $null,
[string]$state = $null
);
if ( $name -and $state ) {
$data_center = $name;
$data_centerA = (, $name);
$dc_state = $state;
$dc_stateA = (, $dc_state);
(Get-F5.iControl).GlobalLBDataCenter.set_enabled_state(
$data_centerA,
$dc_stateA
);
} else {
Write-Usage;
}
}
#-------------------------------------------------------------------------
function Create-GTMServer()
#-------------------------------------------------------------------------
{
param(
[string]$server = $null,
[string]$server_ip_device = $null,
[string]$server_ip_addresses_on_device = $null,
[string]$type = $null,
[string]$data_center = $null
);
if ( $server -and $server_ip_device -and $server_ip_addresses_on_device -and $type -and $data_center ) {
$serverA = (, $server);
$server_ips = New-Object -TypeName iControl.GlobalLBServerServerIPs;
$server_ips.device = $server_ip_device;
$server_ips.addresses_on_device = $server_ip_addresses_on_device.Split(",");
$server_ipsA = (, $server_ips);
$server_ipsAofA = (, $server_ipsA);
$typeA = (, $type);
$data_centerA = (, $data_center);
(Get-F5.iControl).GlobalLBServer.create_v2(
$serverA,
$server_ipsAofA,
$typeA,
$data_centerA
);
} else {
Write-Usage;
}
}
#-------------------------------------------------------------------------
function Create-GTMVirtualServer()
#-------------------------------------------------------------------------
{
param(
[string]$vs_name = $null,
[string]$vs_server = $null,
[string]$vs_address = $null,
[string]$vs_port = $null
);
if ( $vs_name -and $vs_server -and $vs_address -and $vs_port) {
$virtual_server = New-Object -TypeName iControl.GlobalLBVirtualServerID;
$virtual_server.name = $vs_name;
$virtual_server.server = $vs_server;
$virtual_serverA = (, $virtual_server);
$address = New-Object -TypeName iControl.CommonIPPortDefinition;
$address.address = $vs_address;
$address.port = $vs_port;
$addressA = (, $address);
(Get-F5.iControl).GlobalLBVirtualServerV2.create(
$virtual_serverA,
$addressA
);
} else {
Write-Usage;
}
}
#-------------------------------------------------------------------------
function Set-GTMVirtualServerState()
#-------------------------------------------------------------------------
{
param(
[string]$vs_name = $null,
[string]$vs_server = $null,
[string]$vs_state = $null
);
if ( $vs_name -and $vs_server -and $vs_state ) {
$virtual_server = New-Object -TypeName iControl.GlobalLBVirtualServerID;
$virtual_server.name = $vs_name;
$virtual_server.server = $vs_server;
$virtual_serverA = (, $virtual_server);
$state = $vs_state;
$stateA = (, $state);
(Get-F5.iControl).GlobalLBVirtualServerV2.set_enabled_state(
$virtual_serverA,
$stateA
);
} else {
Write-Usage;
}
}
#-------------------------------------------------------------------------
function Create-GTMPool()
#-------------------------------------------------------------------------
{
param(
[string]$name = $null,
[string]$lb_method = $null,
[string]$vs_name = $null,
[string]$vs_server = $null,
[long]$order = $null
);
if ( $name -and $lb_method -and $vs_name -and $vs_server -and $order) {
$pool_nameA = (, $name);
$lb_methodA = (, $lb_method);
$member = New-Object -TypeName iControl.GlobalLBVirtualServerID;
$member.name = $vs_name;
$member.server = $vs_server;
# For greater than one member, create muliple members add add them to $memberA
$memberA = (, $member);
$memberAofA = (, $memberA);
$orderA = (, $order);
$orderAofA = (, $orderA);
(Get-F5.iControl).GlobalLBPool.create_v2(
$pool_nameA,
$lb_methodA,
$memberAofA,
$orderAofA
);
} else {
Write-Usage;
}
}
#-------------------------------------------------------------------------
function Create-GTMWideIp()
#-------------------------------------------------------------------------
{
param(
[string]$name = $null,
[string]$lb_method = $null,
[string]$pool_name = $null,
[long]$pool_order = $null,
[long]$pool_ratio = $null,
[string]$rule_name = $null,
[long]$rule_priority = $null
);
if ( $name -and $lb_method -and $pool_name -and $pool_order -and $pool_ratio -and $rule_name -and $rule_priority ) {
$wide_ipA = (, $name);
$lb_methodA = (, $lb_method);
$wideip_pool = New-Object -TypeName iControl.GlobalLBWideIPWideIPPool;
$wideip_pool.pool_name = $pool_name;
$wideip_pool.order = $pool_order;
$wideip_pool.ratio = $pool_ratio;
$wideip_poolA = (, $wideip_pool);
$wideip_poolAofA = (, $wideip_poolA);
$wideip_rule = New-Object -TypeName iControl.GlobalLBWideIPWideIPRule;
$wideip_rule.rule_name = $rule_name;
$wideip_rule.priority = $rule_priority;
$wideip_ruleA = (, $wideip_rule);
$wideip_ruleAofA = (, $wideip_ruleA);
(Get-F5.iControl).GlobalLBWideIP.create(
$wide_ipA,
$lb_methodA,
$wideip_poolAofA,
$wideip_ruleAofA
);
} else {
Write-Usage;
}
}
#-------------------------------------------------------------------------
function Do-Initialize()
#-------------------------------------------------------------------------
{
if ( (Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn"}) -eq $null )
{
Add-PSSnapIn iControlSnapIn
}
$success = Initialize-F5.iControl -HostName $g_bigip -Username $g_uid -Password $g_pwd;
return $success;
}
#-------------------------------------------------------------------------
# Main Application Logic
#-------------------------------------------------------------------------
if ( ($g_bigip -eq $null) -or ($g_uid -eq $null) -or ($g_pwd -eq $null) )
{
Write-Usage;
}
if ( Do-Initialize )
{
# ---------------------------------------
# Example Usages
# ---------------------------------------
#Create-GTMDataCenter -name "test_dc" -location "test_location" -contact "Joe"
# Set-GTMDataCenterState -name "test_dc" -state "STATE_DISABLED";
# Set-GTMDataCenterState -name "test_dc" -state "STATE_ENABLED";
#Create-GTMServer -server "test_server" -server_ip_device "test_device" -server_ip_addresses_on_device "10.10.10.10,10.10.10.20" -type "SERVER_TYPE_GENERIC_LOAD_BALANCER" -data_center "test_dc"
#Create-GTMVirtualServer -vs_name "test_vs" -vs_server "test_server" -vs_address "10.10.10.10" -vs_port "80"
# Set-GTMVirtualServerState -vs_name "test_vs" -vs_server "test_server" -vs_state "STATE_DISABLED";
# Set-GTMVirtualServerState -vs_name "test_vs" -vs_server "test_server" -vs_state "STATE_ENABLED";
#Create-GTMPool -name "test_pool" -lb_method "LB_METHOD_ROUND_ROBIN" -vs_name "test_vs" -vs_server "test_server" -order 1
#Create-GTMWideIp -name "testwip.foo.com" -lb_method "LB_METHOD_ROUND_ROBIN" -pool_name "test_pool" -pool_order 1 -pool_ratio 1 -rule_name "test_rule" -rule_priority 1
}
else
{
Write-Error "ERROR: iControl subsystem not initialized"
}Tested this on version:
11.5Published Sep 07, 2016
Version 1.0No CommentsBe the first to comment