2015-07-27 11 views
6

Ben Powershell için çok yeni ve öğrenme aşamasında, aşağıdaki görevi otomatikleştirmek için bir komut dosyası oluşturmaya çalıştım. Bu betik beklediğim gibi çalışmıyor. Lütfen gözden geçirebilir ve bana biraz yardım edebilir misiniz? Benim görevdirPowershell Komutu Durdurulduysa Servise Başlamak ve dakika bekleyip bir e-posta bildirimi göndermek için

,

Durdum durumdadır SQL Hizmetleri (birden fazla sunucu birden fazla SQL hizmetlerini) bulmaya çalışıyor ve hizmet başlangıç ​​tamamlamak için bir dakika bekliyorum, bunu başlatmak için çalışıyorum ve servis durumunu tekrar doğrulamak. Hala durdurulursa, bir eylem için insanların kurulumuna bir e-posta göndermeye çalışıyorum.

aşağıdaki kodu inceleyin ve hata düzeltmek misiniz, bunu bulmaya çalıştı ama

#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 $ Sunucular, $ Hizmetleri yapamadığını

burada şeye ihtiyacın olursa bana bildirin benim son

+2

serviceStatus işlevini tanımladınız ancak hiçbir zaman bir çağrı yapmadınız –

+2

Beklediğiniz gibi davranmadığını bize söylemediniz mi? – Nanhydrin

+0

Burada meydana gelen bir hata olup olmadığı hakkında daha fazla bilgiye mi ihtiyacınız var. Ayrıca, zaten mevcut olan bir 'Send-MailMessage' cmdlet'i olduğunda FuncMail işlevinize gerek yoktur. – boeprox

cevap

3

Merhaba, bu en iyi yaklaşım değil ve ben hızlı bir şekilde yapıyorum. note% = foreach-object; ? = Nerede-Nesne.

aksi halde ben de size sunucuları tek bir dosya kaydetmiş olan farz ediyorum read-host -assecurestring | convertfrom-securestring | out-file "C:\Secure\Password.txt"

kullanarak çalıştırın tutmazsa SMTP sunucusu kimlik doğrulaması gerektiriyorsa bir dosya üzerinde şifrenizi kaydetmek gerekir.
Çözümüm, tüm SQL Server hizmetlerini başlatmak için, özel bir isme başlamak istiyorsanız, servis ismini ayrı bir satırda tek bir dosyaya kaydedin.

Aşağıdaki kod yürütme kodu.

#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.: Bu sorunu çözmek için sadece karar iyi yaklaşım değil. İsterseniz daha sonra bir işlev oluşturabiliriz, sadece önce test edin.

+0

Kesinlikle Powershell'de birisine kısaltma kullanmamanızı veya bir satırda komutları bir araya getirmenizi tavsiye ederim – Taegost

+0

Size @Taegost ile katılıyorum. Bu yüzden cevabımın üstüne kısaltma ile ilgili kısa bir açıklama yaptım. Önerinize göre düzelteceğim. BU YAKLAŞIMI KABUL ETMEK İSTEDİĞİNİZ HER TÜRLÜ ÇOCUKLARA ÇABAĞINIZ. BAZI GHOST START TYPING MY CODE'I DÜŞÜNÜYORUM. :) –

+0

@Taegost İnşallah şimdiden öncekilerden biraz daha iyileşmişim, ipuçları için teşekkürler Umarız bu çözüm okunabilir ve kullanıcı dostu oldu –

İlgili konular