EMC Isilon SSH based Command execution Write Action

EMC.Isilon.SSHCommandCaller.WA (WriteActionModuleType)

EMC Isilon Write Action to execute a command via SSH

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
InputTypeSystem.BaseData
OutputTypeMicrosoft.Windows.SerializedObjectData

Member Modules:

ID Module Type TypeId RunAs 
CallPS WriteAction Microsoft.Windows.PowerShellWriteAction Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
Portint$Config/Port$Port number
TimeoutSecondsint$Config/TimeoutSeconds$Timeout in Seconds

Source Code:

<WriteActionModuleType ID="EMC.Isilon.SSHCommandCaller.WA" Accessibility="Public">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Host" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Port" type="xsd:integer" minOccurs="0"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="UserName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Password" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Command" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TimeoutSeconds" type="xsd:integer" minOccurs="0"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="Port" ParameterType="int" Selector="$Config/Port$"/>
<OverrideableParameter ID="TimeoutSeconds" ParameterType="int" Selector="$Config/TimeoutSeconds$"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<WriteAction ID="CallPS" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>Infront.EMC.Isilon.SSHCommandCaller.ps1</ScriptName>
<ScriptBody><Script>Param(
[String] $DeviceIP,
[String] $DevicePort,
[String] $DeviceUserName,
[String] $DeviceUserPassword,
[String] $CommandText
)

$api = New-Object -comObject 'MOM.ScriptAPI'

$EventIDNUmber = 16033
$EventScriptName = "Infront.EMC.Isilon.SSHCommandCaller.ps1"

$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Starting Script")

$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Checking if NuGet Package Provider is Installed")
$NUGetPackge = Get-PackageProvider -Name NuGet -ForceBootstrap

if($NUGetPackge -eq $null)
{
$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"NuGet Package Provider is Not Installed")
$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Installing NuGet Package Provider")
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Confirm:$False -Force -Scope AllUsers
}
else
{
$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"NuGet Package Provider is Installed")
}

$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Checking if Posh-SSH Module is Installed")

$SSHModule = Get-InstalledModule -Name Posh-SSH -ErrorAction SilentlyContinue

if($SSHModule -eq $null)
{
$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Posh-SSH Module is Not Installed")
$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Installing Posh-SSH Module")
Install-module Posh-SSH -Scope AllUsers -Confirm:$False -Force
}
else
{
$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Posh-SSH Module is Installed")
}

$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Importing Posh-SSH Module")
Import-Module Posh-SSH;

$ErrorDetails = ""
$ExecutionResult = $false

#Device Credential
$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Setting Credential Object")

$DeviceUserName = $DeviceUserName.Replace("&lt;SCXUser&gt;&lt;UserId&gt;","")
$DeviceUserName = $DeviceUserName.Replace("&lt;/UserId&gt;&lt;Elev&gt;&lt;/Elev&gt;&lt;/SCXUser&gt;","")

if($DeviceUserPassword -ne "")
{
$DeviceUserPasswordOK = $true
}
else
{
$DeviceUserPasswordOK = $false
}

$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"User Name: $DeviceUserName")
$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"User Password OK: $DeviceUserPasswordOK")

$DeviceSecPasswd = ConvertTo-SecureString $DeviceUserPassword -AsPlainText -Force
$DeviceCred = New-Object System.Management.Automation.PSCredential ($DeviceUserName, $DeviceSecPasswd)
$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Credential Object Created")

#Creating new SSH Session
$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Creating new SSH Session")

$SSHSession = New-SSHSession -ComputerName $DeviceIP -Port $DevicePort -Credential $DeviceCred -AcceptKey -ErrorAction SilentlyContinue -ErrorVariable SSHSessionConnError

if ($SSHSessionConnError.Count -gt 0)
{
$SSHConnectionStatus = "Failed"
$ErrorDetails = $SSHSessionConnError[0].Exception.Message
$SHHCommandOutput = $ErrorDetails

$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"New SSH Session creating $SSHConnectionStatus with Error: $SHHCommandOutput")
}
else
{
$SSHConnectionStatus = "Succeed"
$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"New SSH Session creating $SSHConnectionStatus")

$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Invoking SSH Command")
$CommandExecution = Invoke-SSHCommand -Index 0 -Command $CommandText -ErrorAction SilentlyContinue -ErrorVariable CommandExecutionError

if ($CommandExecutionError.Count -gt 0)
{
$SSHCommandExecutionStatus = "Failed"
$ErrorDetails = $CommandExecutionError[0].Exception.Message
$SHHCommandOutput = $CommandExecution.Error
$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Invoking SSH Command $SSHCommandExecutionStatus with Error: $SHHCommandOutput")
}
else
{
$SSHCommandExecutionStatus = "Succeed"
$SHHCommandOutput = $CommandExecution.Output

$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"Invoking SSH Command $SSHCommandExecutionStatus")
$api.LogScriptEvent($EventScriptName,$EventIDNUmber,0,"SSH Command ouput returned")
}
}

# Clean up
if($SSHSession -ne $null)
{
Remove-SSHSession -SSHSession $SSHSession
Remove-variable SSHSession
}

return $SHHCommandOutput</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>DeviceIP</Name>
<Value>$Config/Host$</Value>
</Parameter>
<Parameter>
<Name>DevicePort</Name>
<Value>$Config/Port$</Value>
</Parameter>
<Parameter>
<Name>DeviceUserName</Name>
<Value>$Config/UserName$</Value>
</Parameter>
<Parameter>
<Name>DeviceUserPassword</Name>
<Value>$Config/Password$</Value>
</Parameter>
<Parameter>
<Name>CommandText</Name>
<Value>$Config/Command$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="CallPS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>Windows!Microsoft.Windows.SerializedObjectData</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>