Tuesday, October 18, 2016

Powershell Script to retrieve process details from UNIX machines

<#  
.SYNOPSIS  
Script to retrieve process details from UNIX machines. Read the credentials file for the UNIX box which are separated by # symbol as root#password

.FILE NAME

getUNIXStatus.ps1
#>


# Import the SSH libraries which is one time task as below
#iex (New-Object Net.WebClient).DownloadString("https://gist.github.com/darkoperator/6152630/raw/c67de4f7cd780ba367cccbc2593f38d18ce6df89/instposhsshdev")

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$CredPath = $scriptDir + "\securestring.txt"

$list = @()
$flag = ""

$Credential = Get-Content $CredPath
$User = $Credential.Split("#")[0]
$Password = $Credential.Split("#")[1] | ConvertTo-SecureString -AsPlainText -Force

$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $User,$Password

New-SSHSession -ComputerName "machine1" -Credential $creds
$session = Invoke-SSHCommand -Index 0 -Command "/app/oracle/opmn/bin/opmnctl status | egrep 'process0|process1|process2|process3|process4|process5'"

#Since there are 24 values in the session object for loop has been iterated till 24

for($i=0; $i -lt 24)
{
    $Obj=New-Object -TypeName PSObject -Property @{
    Name = $session.Output.Split('|')[$i+1]
    Pid = $session.Output.Split('|')[$i+2]
    Status = $session.Output.Split('|')[$i+3]
    } | Select Name, Pid, Status

    $list += $Obj

    $i = $i+4
}

# Disconnect the UNIX session
Remove-SSHSession -Index 0 -Verbose

$Output = "<HTML><TITLE>UNIX Machine Details</TITLE><BODY><font color =""orange"" face=""Comic Sans MS""><H2 align=left>UNIX Machine Details</H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=black align=center><TD><font color=""white"" face=""Comic Sans MS"">Name</font></TD><TD><font color=""white"" face=""Comic Sans MS"">Process ID</font></TD><TD><font color=""white"" face=""Comic Sans MS"">Status</font></TD></TR>"
            foreach ($listobj in $list)
            {
                if($listobj.Status -eq "Down")
                {
                    $Output += "<TR bgcolor=red>"
                    $flag = "Red"
                }
               else
                { 
                    $Output += "<TR>" 
                } 
            
                $Output += "<TD>$($listobj.Name)</TD><TD>$($listobj.Pid)</TD><TD align=center>$($listobj.Status)</TD></TR>"
            }
$Output += "</Table></BODY></HTML>"

No comments:

Post a Comment