简体   繁体   中英

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 “abc@gmail.com” -From “abc@gmail.com” -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

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" 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="user@domain.com"
         $subject="$($_.MachineName) $($_.Name)  fails to Start, Take immediate Action to avoid Impact"
         $From="ServiceStatus@domain.com"
         $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
     }
}

PS: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM