简体   繁体   English

Powershell脚本启动服务如果它已停止并等待一分钟并发送电子邮件通知

[英]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. 我是Powershell的新手,在学习阶段,我尝试创建一个脚本来完成自动化任务。 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. 我试图找出处于停止状态并尝试启动它的SQL服务(多个服务器中的多个SQL服务),等待一分钟完成服务启动并再次验证服务状态。 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 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; 注意%= 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" 如果smtp-server需要身份验证,则必须将密码保存在一个文件中,否则不要使用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. 我的解决方案是启动所有sql server服务,如果你想要开始具体只需将服务名称保存在单独一行的一个文件上。

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. PS:这不是我决定解决这个问题的最佳方法。 if you want we can create a function together later just test it first. 如果你想我们可以一起创建一个函数,那么先测试它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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