Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Script to Start Service if it is Stopped and wait for minute and send an email notification

I am very new to Powershell and in learning stage, I have tried to create an script to do automate below task. This script is not working as i am expected. Could you please review it and give me some help on it.

My task is,

I am trying to find out SQL Services (more than one SQL services in multiple servers) which are in stopped state and trying to start it, Waiting for an minute to complete the service start and verifying the service status again. If still it is stopped state i am trying to sending an email to setup of people for an action.

Could you please review the below code and correct the mistake, i tried to find it but unable to do

#Define servers & Services Variables
$Servers = GC "E:\Bhanu\SQLServer.txt"
$Services = GC "E:\Bhanu\SQLService.txt"

#Function Call
Function ServiceStatus ($Servers, $Services)
{
    foreach ($Server in $Servers)
    {
        foreach ($Service in $Services)
        {
            $Servicestatus = get-service -ComputerName $Server -Name $Service
            if ($Servicestatus.Status -eq "Stopped")
            {
                Start-service $Service          
                Start-Sleep -Seconds 60            
                $ServiceStatus1 = Get-Service -ComputerName $Server -Name $Service
                if ($Servicestatus1.Status -eq "Stopped")
                {            
                FuncMail -To “[email protected]” -From “[email protected]” -Subject $Server + $Service "fails to Start, Take immediate Action to avoid Impact” -Body $ServiceName "Service fails to Start, Take immediate Action to avoid Impact” -smtpServer “servername”
                }
            }
        }
    }
}


function FuncMail 
{
    #param($strTo, $strFrom, $strSubject, $strBody, $smtpServer)
    param($To, $From, $Subject, $Body, $smtpServer)
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg.From = $From
    $msg.To.Add($To)
    $msg.Subject = $Subject
    $msg.IsBodyHtml = 1
    $msg.Body = $Body
    $smtp.Send($msg)
}

servicestatus $Servers $Services

Please let me know if you need anything here from my end

like image 203
bhanu prakash Avatar asked Jul 27 '15 11:07

bhanu prakash


People also ask

How do I wait for a service to stop in PowerShell?

The PowerShell cmdlets Stop-Service and Start-Service will wait until the services are fully stopped and started respectively. You can use the -Force switch for Stop-Service to make it also stop services that depend on the service you are trying to stop.

How do I start a service from a PowerShell script?

You cannot start the services that have a start type of Disabled. If a Start-Service command fails with the message Cannot start service \<service-name\> on computer , use Get-CimInstance to find the start type of the service and, if you have to, use the Set-Service cmdlet to change the start type of the service.

How do I know if a PowerShell service is running?

Use PowerShell to Check Service Status on a Remote Computer You can use the Get-Service cmdlet to get the status of services not only on the local but also on remote computers. To do this, use the –ComputerName parameter. You can use the NetBIOS, FQDN name, or an IP address as a computer name.

How do I check services in PowerShell?

To find the service name and display name of each service on your system, type Get-Service . The service names appear in the Name column, and the display names appear in the DisplayName column.


1 Answers

Hi this isn't the best approach and i'm doing it in quick way. note %=foreach-object; ?=Where-Object.

You have to save your password on one file if your smtp-server require authentication otherwise don't run it using read-host -assecurestring | convertfrom-securestring | out-file "C:\Secure\Password.txt"

I'm also assuming you have your servers saved on one file.
My solution is to start all sql server service if you want to start specific just save the service name on one file on separate line.

The code to execute bellow.

#Loading Server and service details
$Services=Get-content C:\PS\Service.txt
$servidores=get-content C:\PS\Servers\Servers.txt

#Loading Mail credential
$Mailpasswordpath="C:\PS\Securestring.txt"
$Mailusername="DOmain\User"
$password=cat $Mailpasswordpath |ConvertTo-Securestring
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Mailusername,$password


$servidores|Foreach-Object{Get-Service -ComputerName $_ -Name $Services }|  #Get the services running on all servers
Where-Object{$_.Status -eq "Stopped"}| #Which status is equal stopped
Foreach-Object{
     $_.Start(); #try to start
     Start-Sleep -Seconds 60;  #wait one minute
     $_.Refresh();  #refresh then service to update status

     #validate is status still stopped
     if($_.Status -eq "Stopped") 
     {
         #LOADING Mail details
         $To="[email protected]"
         $subject="$($_.MachineName) $($_.Name)  fails to Start, Take immediate Action to avoid Impact"
         $From="[email protected]"
         $smtp="Server.domain.com"
         $body="$($_.Name) Service fails to Start, Take immediate Action to avoid Impact"

         #Sending email to notify
         Send-MailMessage -To $To -Subject $subject  -From $From  -SmtpServer $smtp  -Body $body  -Credential $Cred
     }
}

P.S: It's not the best approach I only decide to solve this problem. if you want we can create a function together later just test it first.

like image 154
Felicio Balane Avatar answered Nov 03 '22 04:11

Felicio Balane