##Declaring Variables as null
$Results = $null
$SendMail = $null
$CmsDown = $null
$Result = $null
#Getting the excution directory of script excution
$Location = Get-Location
#Making Server Parameters file location
$Source_File = Join-Path -Path $Location.Path -ChildPath "\Server_Parameters.txt"
#Fetching Exception List
$Exception_File = Join-Path -Path $Location.Path -ChildPath "\Server_Exception_List.txt"
#Uploading Server Exception List
$Server_Exception_List = Get-Content $Exception_File
#Making Hash Table from User defined variables in Server Parameter File
$Server_Parameter = Get-Content $Source_File | Out-String | ConvertFrom-StringData
#Appending & Making path to ccm executable
$Server_Parameter.BO_INSTALL_FOLDER = Join-Path -Path $Server_Parameter.BO_INSTALL_FOLDER -ChildPath "\win64_x64\"
#Changing Directory to Win64_x64 so we can run ccm.exe
cd $Server_Parameter.BO_INSTALL_FOLDER
$Result= ccm.exe -cms $Server_Parameter.CMS_MACHINE_NAME -display -username $Server_Parameter.CMS_USERNAME -password $Server_Parameter.CMS_PASSWORD -authentication $Server_Parameter.CMS_USER_AUTHENTICATION
##Removing tabs from the lines of results, so everything starts from beginning of line
$Result = $Result.replace("`t","")
##If CMS is down. Dont run rest of the code & send email with custom message
$CmsDown = $Result | Select-String -Pattern 'Unable to logon to CMS'
if($CmsDown -ne $null){$SendMail += 1}
Else{
##Declaring arrays for States
$Stopped = @()
$Initializing = @()
$Starting = @()
$Stopping = @()
$Running =@()
#Declaring array to hold exception
$Exceptioned_Server = @()
#Ensuring that buffer is null for Line Numbers
$StoppedLineNumber = $null
$InitializingLineNumber = $null
$StartingLineNumber = $null
$StoppingLineNumber = $null
$RunningLineNumber = $null
#Declaring Liner Number buffer for exception & making an array out of it
$ExceptionedLineNumber = $null
$ExceptionedLineNumber = @()
#Getting exception Numbers First
Foreach($Exceptioned_Server_Name in $Server_Exception_List){
$ExceptionedLineNumber += $Result | Select-String -Pattern $Exceptioned_Server_Name | Select-Object -ExpandProperty 'LineNumber'
}
#Extracting the Exceptioned Servers in $Exceptioned Array
Foreach ($Number in $ExceptionedLineNumber){
$Exceptioned_Server += [PSCustomObject] @{
ServerName = $Result[$Number - 1].replace("Server Name: ","");
State = $Result[$Number].replace("State: ","");
Status = $Result[$Number+1].replace("Enabled: ","");}
#removing the exceptioned lines from $Result File, as they have been already extraced to Exceptioned Server Array
$Result[$Number - 1] = ''
$Result[$Number] = ''
$Result[$Number + 1] = ''
$Result[$Number + 2] = ''
$Result[$Number + 3] = ''
$Result[$Number + 4] = ''
}
#Find Line number where State is found and add them in $LineNumber variable
$StoppedLineNumber = $Result | Select-String -Pattern 'State: Stopped' | Select-Object -ExpandProperty 'LineNumber'
$InitializingLineNumber = $Result | Select-String -Pattern 'State: Initializing' | Select-Object -ExpandProperty 'LineNumber'
$StartingLineNumber = $Result | Select-String -Pattern 'State: Starting' | Select-Object -ExpandProperty 'LineNumber'
$StoppingLineNumber = $Result | Select-String -Pattern 'State: Stopping' | Select-Object -ExpandProperty 'LineNumber'
$RunningLineNumber = $Result | Select-String -Pattern 'State: Running' | Select-Object -ExpandProperty 'LineNumber'
Foreach ($Number in $StoppedLineNumber){
$Stopped += [PSCustomObject] @{
ServerName = $Result[$Number - 2].replace("Server Name: ","");
State = $Result[$Number - 1].replace("State: ","");
Status = $Result[$Number].replace("Enabled: ","");}
}
Foreach ($Number in $InitializingLineNumber){
$Initializing += [PSCustomObject] @{
ServerName = $Result[$Number - 2].replace("Server Name: ","");
State = $Result[$Number - 1].replace("State: ","");
Status = $Result[$Number].replace("Enabled: ","");}
}
Foreach ($Number in $StartingLineNumber){
$Starting += [PSCustomObject] @{
ServerName = $Result[$Number - 2].replace("Server Name: ","");
State = $Result[$Number - 1].replace("State: ","");
Status = $Result[$Number].replace("Enabled: ","");}
}
Foreach ($Number in $StoppingLineNumber){
$Stopping += [PSCustomObject] @{
ServerName = $Result[$Number - 2].replace("Server Name: ","");
State = $Result[$Number - 1].replace("State: ","");
Status = $Result[$Number].replace("Enabled: ","");}
}
Foreach ($Number in $RunningLineNumber){
$Running += [PSCustomObject] @{
ServerName = $Result[$Number - 2].replace("Server Name: ","");
State = $Result[$Number - 1].replace("State: ","");
Status = $Result[$Number].replace("Enabled: ","");}
}
if($StoppedLineNumber -ne $null)
{
$SendMail += 1
$OutputreportStopped = "<div><H2>[Servers(s) Stopped]</H2><Table border=1 cellpadding=5 cellspacing=0><TR bgcolor=gray align=center><TD><B>Server Name</B></TD><TD><B>State</B></TD><TD><B>Status</B></TD></TR>"
Foreach($Entry in $Stopped)
{
switch ($Entry.State)
{
"Stopped" {$OutputreportStopped += "<TR bgcolor='#FF704D' style='color:#000;'>"}
default {$OutputreportStopped += "<TR bgcolor='#FF704D' style='color:#000;'>"}
}
$OutputreportStopped += "<TD>$($Entry.ServerName)</TD><TD align=center>$($Entry.State)</TD><TD align=center>$($Entry.Status)</TD></TR>"
}
$OutputreportStopped += "</Table></div>"
}
if($StoppingLineNumber -ne $null)
{
$SendMail += 1
$OutputreportStopping = "<div><H2 style='color:black;'>[Server(s) Stopping]</H2><Table border=1 cellpadding=5 cellspacing=0><TR bgcolor=gray align=center><TD><B>Server Name</B></TD><TD><b>State</b></TD><TD><B>Status</B></TD></TR>"
Foreach($Entry in $Stopping)
{
switch ($Entry.State)
{
"Stopping" {$OutputreportStopping += "<TR bgcolor='#FF704D' style='color:#000;'>"}
default {$OutputreportStopping += "<TR bgcolor='#FF704D' style='color:#000;'>"}
}
$OutputreportStopping += "<TD>$($Entry.ServerName)</TD><TD align=center>$($Entry.State)</TD><TD align=center>$($Entry.Status)</TD></TR>"
}
$OutputreportStopping += "</Table></div>"
}
if($StartingLineNumber -ne $null)
{
$SendMail += 1
$OutputreportStarting = "<div><H2 style='color:black;'>[Server(s) Starting]</H2><Table border=1 cellpadding=5 cellspacing=0><TR bgcolor=gray align=center><TD><B>Server Name</B></TD><TD><b>State</b></TD><TD><B>Status</B></TD></TR>"
Foreach($Entry in $Starting)
{
switch ($Entry.State)
{
"Starting" {$OutputreportStarting += "<TR bgcolor='#FF704D' style='color:#000;'>"}
default {$OutputreportStarting += "<TR bgcolor='#FF704D' style='color:#000;'>"}
}
$OutputreportStarting += "<TD>$($Entry.ServerName)</TD><TD align=center>$($Entry.State)</TD><TD align=center>$($Entry.Status)</TD></TR>"
}
$OutputreportStarting += "</Table></div>"
}
if($InitializingLineNumber -ne $null)
{
$SendMail += 1
$OutputreportInitializing = "<div><H2 style='color:black;'>[Server(s) Initializing]</H2><Table border=1 cellpadding=5 cellspacing=0><TR bgcolor=gray align=center><TD><B>Server Name</B></TD><TD><b>State</b></TD><TD><B>Status</B></TD></TR>"
Foreach($Entry in $Initializing)
{
switch ($Entry.State)
{
"Initializing" {$OutputreportInitializing += "<TR bgcolor='#FF704D' style='color:#000;'>"}
default {$OutputreportInitializing += "<TR bgcolor='#FF704D' style='color:#000;'>"}
}
$OutputreportInitializing += "<TD>$($Entry.ServerName)</TD><TD align=center>$($Entry.State)</TD><TD align=center>$($Entry.Status)</TD></TR>"
}
$OutputreportInitializing += "</Table></div>"
}
if($ExceptionedLineNumber -ne $null)
{
# $SendMail += 1
$OutputreportException = "<div><H2 style='color:blue;'>[Server(s) Excluded From This Scan As Per Exception List]</H2><Table border=1 cellpadding=5 cellspacing=0><TR bgcolor=gray align=center><TD><B>Server Name</B></TD><TD><b>State</b></TD><TD><B>Status</B></TD></TR>"
Foreach($Entry in $Exceptioned_Server)
{
switch ($Entry.State)
{
# "Initializing" {$OutputreportInitializing += "<TR bgcolor='#FF704D' style='color:#000;'>"}
default {$OutputreportException += "<TR bgcolor='#D6FFFF' style='color:#000;'>"}
}
$OutputreportException += "<TD>$($Entry.ServerName)</TD><TD align=center>$($Entry.State)</TD><TD align=center>$($Entry.Status)</TD></TR>"
}
$OutputreportException += "</Table></div>"
}
if($Running.Status -eq ('Disabled')){
$SendMail += 1
$OutputreportRunningButDisabled = "<div><H2 style='color:black;'>[Server(s) Running But Disabled]</H2><Table border=1 cellpadding=5 cellspacing=0><TR bgcolor=gray align=center><TD><B>Server Name</B></TD><TD><b>State</b></TD><TD><B>Status</B></TD></TR>"
Foreach($Entry in $Running)
{
if ($Entry.Status.Contains('Disabled')){
# Write-Host $Entry.ServerName
switch ($Entry.State)
{
"Running" {$OutputreportRunningButDisabled += "<TR bgcolor='#FF704D' style='color:#000;'>"}
default {$OutputreportRunningButDisabled += "<TR bgcolor='#FF704D' style='color:#000;'>"}
}
$OutputreportRunningButDisabled += "<TD>$($Entry.ServerName)</TD><TD align=center>$($Entry.State)</TD><TD align=center>$($Entry.Status)</TD></TR>"
}
}
$OutputreportRunningButDisabled += "</Table></div>"
}
$Results = "<div>BO Server Availability Report - " + $Server_Parameter.ENVIRONMENT + " see results below:</div>"
$Results += "<TITLE style='color:grey;'>[Errors] BO Server Availability Report</TITLE><BODY background-color:peachpuff>$OutputreportStopped $OutputreportStopping $OutputreportStarting $OutputreportInitializing $OutputreportRunningButDisabled $OutputreportException</BODY>"
$Date = (Get-Date -format "dd-MM-yyyy_hh-mm-ss")
#$OutputFile = "D:\sarv\scripts\ServerStatusCheck_$Date.htm"
#$Results | out-file $OutputFile
#Write-Host "Mail Count Variable : " $SendMail
}
if ($SendMail -gt 0) {
#Send email with atachment
$EmailFrom = $Server_Parameter.MAIL_FROM
$EmailTo = $Server_Parameter.MAIL_TO
$SMTPServer = $Server_Parameter.SMTP_SERVER
$EmailSubject = $Server_Parameter.MAIL_SUBJECT
$EmailCC = $Server_Parameter.MAIL_CC
#Send mail with output
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($EmailFrom)
$mailmessage.To.add($EmailTo)
$mailmessage.Subject = $EmailSubject
$mailmessage.IsBodyHTML = $true
if ($Server_Parameter.MAIL_CC -ne ''){
$mailmessage.CC.Add($EmailCC)
}
if($CmsDown -eq $null){
$mailmessage.Body = $Results
# Write-Host "NOT WORKING"
}
if($CmsDown -ne $null){
# Write-Host "NOT WORKING"
$mailmessage.Body = $Server_Parameter.MAIL_MESSAGE
# Write-Host $mailmessage.Body
}
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer,$Server_Parameter.SMTP_SERVER_PORT)
if($Server_Parameter.IS_SMTP_SSL -eq 'TRUE'){
$SMTPClient.EnableSsl = $true
}
if($Server_Parameter.USE_SMTP_CREDENTIALS -eq 'TRUE'){
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Server_Parameter.SMTP_USERNAME,$Server_Parameter.SMTP_USER_PASSWORD);
}
$SMTPClient.Send($mailmessage)
}
##############################################################################
## *****ONLY CHANGE THE PARAMETERS BELOW, DO NOT CHANGE VARIABLE NAMES*********
## *****ANY OTHER CHANGE WILL CAUSE THE PROGRAM TO FAIL*********
## *****REFER SAMPLES BELOW FOR GUIDANCE*********
## BO Server Availability Monitoring & Alerting
## Implemented by: Sarvjot Singh Mann
## Date : XX XXX 20XX | Modification Date XX XXX 20XX
## Version : 2.0
## Email:
## Change Log: (1)V2 now allows server exception List, where servers marked as exceptions will not be alerted upon. (2)Mail CC can be allowed as empty
##############################################################################
###Specify what kind of environment is this, as this will be title keyword of the body of the report generated & emailed to you
ENVIRONMENT=BO-PROD (@BOPROD)
###If you have a custom port for CMS, make sure you specify it / Or use cluster name here.
CMS_MACHINE_NAME=Hostname.COM:6400
CMS_USERNAME=Server_Monitoring
CMS_PASSWORD=Password123
###It can be any authentication type, as long as the user has rights to vew server objects, see Read Me First.txt for more details.
CMS_USER_AUTHENTICATION=secEnterprise
#####SPECIFY YOUR BO INSTALL LOCATION WITH DOUBLE \\ ONLY, ELSE THE PROGRAM WILL NOT WORK SINCE \S IS CONSIDERED AS ESCAPE SEQUENCE. DO NOT PUT THE PATH IN QUOTES OR DOUBLE QUOTES. WRITE AS SHOWN BELOW
BO_INSTALL_FOLDER=D:\\SAP BusinessObjects\\SAP BusinessObjects Enterprise XI 4.0\\
##############MAIL SERVER / RECEPIENTS / SENDER DETAILS #####################################################
SMTP_SERVER=smtp.company.com
##Usually the SMTP port is
SMTP_SERVER_PORT= 25
##If your mail Server is running with SSL Mark the 'IS_SMTP_SSL=TRUE' ELSE Mark it as 'IS_SMTP_SSL=FALSE' (Marking SMTP SSL status is MANDATORY).
IS_SMTP_SSL=FALSE
##If your mail server only allows mails to be sent after authentication, set USE_SMTP_CREDENTIALS=TRUE, ELSE set USE_SMTP_CREDENTIALS=FALSE *****THIS FLAG IS MANDATORY else mail will not be sent out
USE_SMTP_CREDENTIALS=FALSE
#### If your mail SMTP requires credentials in then you must fill the SMTP_USERNAME & SMTP_USER_PASSWORD ELSE Leave them as blank
SMTP_USERNAME=
SMTP_USER_PASSWORD=
##Write your mail addresses here, however seperated by comma(,)
MAIL_TO=DL@company.com
MAIL_CC=
MAIL_FROM=Sender@company.com
##Write your custom subject here. NO NEED TO ENCLOSE THEM IN QUOTES OR DOUBLE QUOTES. ENCLOSING THEM IN QUOTES OR DOUBLE QUOTES WILL CAUSE THE MAIL TO FAIL
MAIL_SUBJECT=SERVER-ALERT!!! BOBJ SERVICES DOWN OR NOT WORKING ON BO-PRODUCTION!!!
###################################
## Incase CMS is down or unreachable, write your Custom Mail below
## **Replace text as it is or be sure to apply proper HTML formatting, ensure that all the message is in 1 line assigned to this variable
## **You can give new lines with <br> tag, as shown below, see example below
###################################
MAIL_MESSAGE=Dear Team <br><br>The CMS on this machine is down!!!!. The server status report generated failed with CMS Error: Could not connect to CMS, the CMS is either Down / Disabled . Kindly check and take necessary action if required.<br><br> <br>Regards,<br>BO Admin Alerts
Host.ExplorerMasterServer
Host.ExplorerExplorationServer
Host.ExplorerIndexingServer
Host.ExplorerMasterServer
Host.ExplorerSearchServer
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
8 | |
5 | |
5 | |
4 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |