PowerShell Management Folder
Problem this snippet solves:
This example illustrates how to use the Folder methods to build a console shell allowing you to navigate and manage system Folders on BIG-IP v11 and above.
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-2011
# 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 (
$bigip = $null,
$uid = $null,
$pwd = $null
)
Set-PSDebug -strict;
# Global Script variables
$script:DEBUG = $false;
$script:FOLDER = $null;
$script:RECURSE = "STATE_DISABLED";
#-------------------------------------------------------------------------
# function usage
#-------------------------------------------------------------------------
function usage()
{
Write-Host "Usage: ManagementFolder.ps1 host uid pwd";
exit;
}
#----------------------------------------------------------------------------
function Debug-Message()
#----------------------------------------------------------------------------
{
param($msg);
if ( $script:DEBUG )
{
Write-Host "DEBUG > $msg";
}
}
#----------------------------------------------------------------------------
function Get-CurrentFolder()
#----------------------------------------------------------------------------
{
param([switch]$Force = $false);
if ( ($($script:FOLDER) -eq $null) -or ($Force) )
{
$folder = (Get-F5.iControl).SystemSession.get_active_folder();
}
else
{
$folder = $script:FOLDER;
}
$folder;
}
#----------------------------------------------------------------------------
function Get-ChildFolders()
#----------------------------------------------------------------------------
{
param([switch]$Recurse = $false, [switch]$Long = $false);
$Long = $true;
if ( $Recurse )
{
$oldstate = (Get-F5.iControl).SystemSession.get_recursive_query_state();
(Get-F5.iControl).SystemSession.set_recursive_query_state("STATE_ENABLED");
}
$folders = (Get-F5.iControl).ManagementFolder.get_list();
if ( $Recurse -and ($oldstate -ne "STATE_ENABLED") )
{
(Get-F5.iControl).SystemSession.set_recursive_query_state($oldstate);
}
$descriptions = (Get-F5.iControl).ManagementFolder.get_description($folders);
$groups = (Get-F5.iControl).ManagementFolder.get_device_group($folders);
$curfolder = Get-CurrentFolder;
if ( $curfolder -eq "/" ) { $curfolder = "ZZZZZZZZ"; }
for($i=0;$i-lt$folders.length;$i++)
{
if ( $Long )
{
$o = 1 | select "Folder", "Description", "DeviceGroup";
$o.Folder = $folders[$i].Replace($curfolder, "");
$o.Description = $descriptions[$i];
$o.DeviceGroup = $groups[$i];
}
else
{
$o = 1 | select "Folder";
$o.Folder = $folders[$i].Replace($curfolder, "");
}
$o;
}
}
#----------------------------------------------------------------------------
function Change-Folder()
#----------------------------------------------------------------------------
{
param($folder);
Debug-Message "Setting active folder to '$folder'";
if ( $folder -eq ".." )
{
Move-Up;
}
else
{
(Get-F5.iControl).SystemSession.set_active_folder($folder);
}
$f = Get-CurrentFolder -Force;
}
#----------------------------------------------------------------------------
function Move-Up()
#----------------------------------------------------------------------------
{
$folder = Get-CurrentFolder;
$parent = (Split-Path $folder).Replace('\', '/');
if ( $parent.Length -gt 0 )
{
Debug-Message "Setting active folder to '$parent'";
(Get-F5.iControl).SystemSession.set_active_folder($parent);
}
$f = Get-CurrentFolder -Force;
}
#----------------------------------------------------------------------------
function Create-Folder()
#----------------------------------------------------------------------------
{
param($folder);
Debug-Message "Creating folder '$folder'";
(Get-F5.iControl).ManagementFolder.create($folder);
Write-Host "Folder '$folder' successfully created!";
}
#----------------------------------------------------------------------------
function Remove-Folder()
#----------------------------------------------------------------------------
{
param($folder);
Debug-Message "Removing folder '$folder'";
(Get-F5.iControl).ManagementFolder.delete_folder($folder);
Write-Host "Folder '$folder' successfully removed!";
}
#----------------------------------------------------------------------------
function Set-FolderDescription()
#----------------------------------------------------------------------------
{
param($cmd);
$tokens = $cmd.Split(" ");
if ( $tokens.Length -eq 1 )
{
$f = $cmd;
$d = "";
Debug-Message "Setting folder '$folder' description to '$d'";
(Get-F5.iControl).ManagementFolder.set_description($f, $d);
Get-FolderDescription $f;
}
elseif ( $tokens.Length -gt 1 )
{
# folder description goes here
$f = $tokens[0];
$d = $tokens[1];
for($i=2; $i-lt$tokens.Length; $i++)
{
$d += " ";
$d += $tokens[$i];
}
Debug-Message "Setting folder '$f' description to '$d'";
(Get-F5.iControl).ManagementFolder.set_description($f, $d);
Get-FolderDescription $f;
}
else
{
Show-Help;
}
}
#----------------------------------------------------------------------------
function Get-FolderDescription()
#----------------------------------------------------------------------------
{
param($folder);
Debug-Message "Retrieving folder description for '$folder'";
$descriptions = (Get-F5.iControl).ManagementFolder.get_description($folder);
$descriptions[0];
}
#----------------------------------------------------------------------------
function Get-RecursiveState()
#----------------------------------------------------------------------------
{
$oldState = (Get-F5.iControl).SystemSession.get_recursive_query_state();
$script:RECURSE = $oldState;
$oldState;
}
#----------------------------------------------------------------------------
function Set-RecursiveState()
#----------------------------------------------------------------------------
{
param($state = $null);
$newState = "STATE_DISABLED";
if ( $state -eq $null )
{
# toggle
$oldState = (Get-F5.iControl).SystemSession.get_recursive_query_state();
if ( $oldState -eq "STATE_DISABLED" )
{
$newState = "STATE_ENABLED";
}
}
else
{
# set
if ( $state.ToLower().Contains("enable") )
{
$newState = "STATE_ENABLED";
}
}
$script:RECURSE = $newState;
(Get-F5.iControl).SystemSession.set_recursive_query_state($newState);
Write-Host "Recursive State set to '$newState'";
}
#----------------------------------------------------------------------------
function Execute-Command()
#----------------------------------------------------------------------------
{
param($cmd);
$fullcmd = $cmd;
if ( -not $fullcmd.ToLower().StartsWith("(get-f5.icontrol).") )
{
$fullcmd = "(Get-F5.iControl).$cmd";
}
Debug-Message "Executing command '$fullcmd'";
Invoke-Expression $fullcmd;
}
#----------------------------------------------------------------------------
function Show-Help()
#----------------------------------------------------------------------------
{
$indent = " "*5;
Write-Host @"
$indent ====================================================================
$indent iControl Folder Shell
$indent ====================================================================
$indent Commands
$indent --------
$indent cd folder - change active folder
$indent d - toggle script debugging
$indent dir,ls - List child folders
$indent gd folder - Get folder description
$indent h - Show this help
$indent ls -l - List child folders and their properties
$indent ls -r - List recursive listing of child folders
$indent ls -lr - List recursive listing of child folders
$indent and their properties
$indent md,mkdir - create folder
$indent pwd - Get current folder
$indent r enable|disable - Set the recursive state. Omitting a
$indent state value will toggle it.
$indent rd,rmdir - remove folder
$indent sd folder desc - Set folder description
$indent q - quit the shell
$indent ! command - execute the iControl command
$indent $INTERFACE.call - execute the iControl command
$indent .. - Move to parent folder
"@;
}
#----------------------------------------------------------------------------
function Get-Args()
#----------------------------------------------------------------------------
{
param($cmd);
$tokens = $cmd.Trim().Split(" ");
$cmd = $tokens[0];
$args = $tokens[1];
for($i=2; $i-lt$tokens.Length;$i++)
{
$args += " ";
$args += $tokens[$i];
}
$args;
}
#----------------------------------------------------------------------------
function Process-Input()
#----------------------------------------------------------------------------
{
param($i);
Debug-Message "< $($MyInvocation.MyCommand.Name) '$i' >";
if ( $i.Length -gt 0 )
{
Debug-Message "CommandLine: '$i'...";
switch -Wildcard ($i.Trim().ToLower())
{
"" { break; }
"cd *" { Change-Folder (Get-Args $i); }
"cd" { Get-CurrentFolder; }
"d" { $script:DEBUG = -not $script:DEBUG; }
"dir" { Get-ChildFolders | Sort-Object Folder; }
"gd *" { Get-FolderDescription (Get-Args $i); }
"h" { Show-Help; }
"ls" { Get-ChildFolders | Sort-Object Folder; }
"ls -l" { Get-ChildFolders -Long | Sort-Object Folder; }
"ls -lr" { Get-ChildFolders -Recurse -Long | Sort-Object Folder; }
"ls -r" { Get-ChildFolders -Recurse | Sort-Object Folder; }
"md *" { Create-Folder (Get-Args $i); }
"mkdir *" { Create-Folder (Get-Args $i); }
"pwd" { Get-CurrentFolder; }
"r" { Set-RecursiveState; }
"r *" { Set-RecursiveState (Get-Args $i); }
"rd *" { Remove-Folder (Get-Args $i); }
"rmdir *" { Remove-Folder (Get-Args $i); }
"sd *" { Set-FolderDescription (Get-Args $i); }
"q" { exit; }
"! *" { Execute-Command (Get-Args $i); }
"$*" { Execute-Command $i.SubString(1); }
".." { Move-Up; }
default { Show-Help; }
}
}
}
#----------------------------------------------------------------------------
function Get-Prompt()
#----------------------------------------------------------------------------
{
# [(r)/Common]
$prefix = "";
if ( $script:DEBUG -or ($script:RECURSE -eq "STATE_ENABLED") )
{
$prefix = "(";
if ( $script:DEBUG )
{
$prefix += "d";
}
if ( $script:RECURSE -eq "STATE_ENABLED" )
{
if ( $prefix -ne "(" ) { $prefix += ","; }
$prefix += "r";
}
$prefix += ")";
}
"[${prefix}$(Get-CurrentFolder)] ";
#if ( $script:DEBUG ) { $prefix = "D:"; }
#$prompt = "[${prefix}$(Get-CurrentFolder)] ";
}
#----------------------------------------------------------------------------
function Do-Initialize()
#----------------------------------------------------------------------------
{
if ( (Get-PSSnapin | Where-Object { $_.Name -eq "iControlSnapIn"}) -eq $null )
{
Add-PSSnapIn iControlSnapIn
}
$success = Initialize-F5.iControl -HostName $bigip -Username $uid -Password $pwd;
return $success;
}
#-------------------------------------------------------------------------
# Main Application Logic
#-------------------------------------------------------------------------
if ( ($bigip -eq $null) -or ($uid -eq $null) -or ($pwd -eq $null) )
{
usage;
}
if ( Do-Initialize )
{
$s = Get-RecursiveState;
while(1)
{
$prompt = Get-Prompt;
# Not using Read-Host here so we can support responses starting with !
$host.UI.Write($prompt);
$i = $host.UI.ReadLine().Trim();
Process-Input $i;
}
}
else
{
Write-Error "ERROR: iControl subsystem not initialized"
}Published Mar 09, 2015
Version 1.0CodeCentral_194
Cirrostratus
Joined May 05, 2019
CodeCentral_194
Cirrostratus
Joined May 05, 2019
No CommentsBe the first to comment