ClusterShell to PowerShell Command Translator

The following PowerShell function, takes as input NetApp Clustered ONTAP ClusterShell commands, and converts them into the equivalent PowerShell command(s). It requires an existing connection to a Clustered ONTAP Cluster (was created and tested against 8.2.1 clusters.)

Note: There is also an offline version, but it’s too big to post here.

Examples

First, copy the script below into a text editor, save as say cs2ps.ps1, and place in your PowerShell working directory (which is C:\Users\USER in the examples below), then invoke the function in PowerShell with:

PS C:\Users\USER> . .\cs2ps.ps1

(That is: dot space dot backslash cs2ps.ps1)

PS C:\Users\USER> cs2ps "event log show"
Name                      Category    Family      Api
----                      --------    ------      ---
Get-NcEmsMessage          ems         {cluster}   {ems-message-get-iter}

PS C:\Users\USER> cs2ps "cluster show"
Name                      Category    Family      Api
----                      --------    ------      ---
Get-NcClusterNode         cluster     {cluster}   {cluster-node-get-iter}
Get-NcFlashDevice         flash       {cluster}   {flash-device-get-iter}
Get-NcFlashThreshold      flash       {cluster}   {flash-thresholds-get-iter}

PS C:\Users\USER> cs2ps "storage failover show"
Name                      Category   Family      Api
----                      --------   ------      ---
Get-NcClusterHaGiveback   cf         {cluster}   {cf-aggregate-giveback-status}
Get-NcClusterHaPartner    cf         {cluster}   {cf-get-partner}
Get-NcClusterHa           cf         {cluster}   {cf-status}
Get-NcClusterHaTakeover   cf         {cluster}   {cf-takeover-status}

The Script

Note: This script has been formatted to fit blogger. If you have display issues in IE, try Google Chrome.

#################################################
# Clustershell to PowerShell Command Translator #
#           Author = vCosonok @ www.cosonok.com #
#################################################

$ErrorActionPreference = 'SilentlyContinue'

If(!(Get-Module DataOntap)){
try{Import-Module DataOntap
}catch{Write-Error "DataOntap module is not installed"}}

#############################
# HELP/INSTRUCTIONAL OUTPUT #
#############################

function cs2psTranslator {cs2psHelp}
function cs2psHelp {""
"ClusterShell to PowerShell Command Translator"
"---------------------------------------------"
"This script consists of 2 functions each of which take arguments:"
""
"1> api2ps {API}"
"2> cs2ps {ClusterShell command}"
""
"It's intended usage is for converting ClusterShell commands into PowerShell commands using the function cs2ps - for example:"
""
"cs2ps " + [char]34 + "volume create" + [char]34
"cs2ps " + [char]34 + "system node show" + [char]34
""
"cs2ps uses an available connection to a CDOT Cluster, invoke-ncssh, and the ClusterShell command:"
"::> security login role show-ontapi"
""
"Note: If you have a space in the command, the command must be contained in quotation marks."
""}

##################################################
# This function takes an API as the argument and #
#  converts into the PowerShell command.         #
##################################################

function api2ps {

$query = $args[0]
if ($query -eq $null) {cs2psHelp;return $null}
if ($query -eq "?")   {cs2psHelp;return $null}
if ($query -eq "help"){cs2psHelp;return $null}

$ncHelpItems = Get-NcHelp

foreach ($item in $ncHelpItems){
if ($item.api -contains $query){$item}}
return $null}

##################################################
# This function converts the command to API      #
# using an existing connection to a CDOT cluster #
# over SSH (via PowerShell's invoke-ncssh) .     #
# Then api2ps converts the api to PowerShell.    #
##################################################
      
function cs2ps {

$query = $args[0]
if ($query -eq $null) {cs2psHelp;return $null}
if ($query -eq "?")   {cs2psHelp;return $null}
if ($query -eq "help"){cs2psHelp;return $null}
      
$quote = '"'
$qQq = $quote + $query + $quote
$cotAPI = invoke-ncssh "security login role show-ontapi -command $qQq*"

# The string $cotAPI is not fantastically usable.
# Below we take its value, remove carriage returns,
# then run through the entire string recording between spaces
# and checking the recorded string must have at least one "-" but not too many
# and, if it passes, putting it in the function api2ps
      
$cotAPIvalue = $cotAPI.value
$cotAPIvalue = $cotAPIvalue -replace "`t|`n|`r"," "
$cotAPIlength = $cotAPIvalue.length
$i = 0
[string]$extractedAPI = ""
$dashCount = 0
      
do {

if ($cotAPIvalue[$i] -ne " "){
$extractedAPI += $cotAPIvalue[$i]}

if ($cotAPIvalue[$i] -eq "-"){
$dashCount++}

if (($cotAPIvalue[$i] -eq " ") -and ($dashCount -ge 1) -and ($dashCount -le 4)){
api2ps $extractedAPI
[string]$extractedAPI = ""
$dashCount = 0}

if (($cotAPIvalue[$i] -eq " ")){
[string]$extractedAPI = ""
$dashCount = 0}
$i++

} while ($i -le $cotAPIlength)}

###########
# THE END #
###########

Comments