Ps System Boot Location

Problem this snippet solves:

This sample will illustrate the usage of the system boot location iControl methods.

The BIG-IP has the ability to partition off different "slots" for multiple software installs. This allows for installing a second version of the software on the system without overwriting the existing configuration to allow for an easy rollback in the case you prefer to roll back to your previous version. The System.SoftwareManagement interface contains the methods to manage the software on the system and hidden in there are the methods for getting and setting the current "boot locations". This article will illustrate the use of those methods.

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-2008 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,
  $g_cmd = $null,
  $g_arg = $null
);

Set-PSDebug -strict;

#-------------------------------------------------------------------------
# function Write-Usage
#-------------------------------------------------------------------------
function Write-Usage()
{
  Write-Host "Usage: BootImage.ps1 host uid pwd [options]";
  Write-Host "      options";
  Write-Host "      -------";
  Write-Host "      get          - Get the name of the active boot location";
  Write-Host "      set location - Set the active boot location";
  Write-Host "      getinfo      - Get the contents of the active boot location";
  exit;
}

#-------------------------------------------------------------------------
# function ParseBootLocationSection
#-------------------------------------------------------------------------
function Parse-BootLocationSection()
{
  param($header, $sr);
  
  $headertxt = $header.Replace("/", '').Trim();
  $tokens = $headertxt.Split( (, ' '));
  $comp = $tokens[0];
  $desc = $headertxt.Replace("$comp ", "");
  
  $hash = @{};
  
  Write-Host "==========================================================="
  Write-Host "Component: $comp"
  Write-Host "     Desc: $desc";
  Write-Host "==========================================================="
  $line = $sr.ReadLine();
  while ($null -ne $line)
  {
    if ( $line.Trim().Length -eq 0 )
    {
      #end of section
      break;
    }
    $tokens = $line.Trim().Split( (, '='));
    $hash.Add($tokens[0], $tokens[1]);
    $line = $sr.ReadLine();
  }
  $hash.GetEnumerator() | Sort-Object -Property Name | Format-Table -autosize;
}

#-------------------------------------------------------------------------
# function Parse-BootLocation
#-------------------------------------------------------------------------
function Parse-BootLocationData()
{
  param($data);
  $sr = New-Object System.IO.StringReader($data);
  $line = $sr.ReadLine();
  while($null -ne $line)
  {
    if ( $line.StartsWith("/") )
    {
      # Beginning of section
      Parse-BootLocationSection $line $sr;
    }
    else
    {
      Write-Host $line;
    }
    $line = $sr.ReadLine();
  }
}

#-------------------------------------------------------------------------
# function Get-BootLocationData
#-------------------------------------------------------------------------
function Get-BootImageInfo()
{
  $rawdata = (Get-F5.iControl).SystemSoftwareManagement.get_boot_image_information($false);
  Write-Host "Boot Information";
  $ASCII = New-Object -TypeName System.Text.ASCIIEncoding
  $formatted = $ASCII.GetString($rawdata)
  
  Parse-BootLocationData $formatted;
}

#-------------------------------------------------------------------------
# function Get-BootLocation
#-------------------------------------------------------------------------
function Get-BootLocation()
{
  $location = (Get-F5.iControl).SystemSoftwareManagement.get_boot_location();
  Write-Host "Boot Location: $location";
}

#-------------------------------------------------------------------------
# function Set-BootLocation
#-------------------------------------------------------------------------
function Set-BootLocation()
{
  param($location);
  (Get-F5.iControl).SystemSoftwareManagement.set_boot_location($location);
  Get-BootLocation;
}

#-------------------------------------------------------------------------
# Do-Initialize
#-------------------------------------------------------------------------
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) -or ($g_cmd -eq $null) )
{
  Write-Usage;
}

if ( Do-Initialize )
{
  switch ($g_cmd.ToLower())
  {
    "getinfo" {
      Get-BootImageInfo;
    }
    "get" {
      Get-BootLocation;
    }
    "set" {
      if ( $g_arg -eq $null )
      {
        Write-Usage;
      }
      else
      {
        Set-BootLocation $g_arg;
      }
    }
    default {
      Write-Usage;
    }
  }
}
else
{
  Write-Error "ERROR: iControl subsystem not initialized"
}
Published Mar 09, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment