sql server - Powershell Script to Start Service if it is Stopped and wait for minute and send an email notification -
i new powershell , in learning stage, have tried create script automate below task. script not working expected. please review , give me on it.
my task is,
i trying find out sql services (more 1 sql services in multiple servers) in stopped state , trying start it, waiting minute complete service start , verifying service status again. if still stopped state trying sending email setup of people action.
could please review below code , correct mistake, tried find unable 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 start, take immediate action avoid impact” -body $servicename "service fails start, take immediate action 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 need here end
hi isn't best approach , i'm doing in quick way. note %=foreach-object; ?=where-object.
you have save password on 1 file if smtp-server require authentication otherwise don't run using read-host -assecurestring | convertfrom-securestring | out-file "c:\secure\password.txt"
i'm assuming have servers saved on 1 file.
solution start sql server service if want start specific save service name on 1 file on separate line.
the code execute bellow.
#loading server , 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 services running on servers where-object{$_.status -eq "stopped"}| #which status equal stopped foreach-object{ $_.start(); #try start start-sleep -seconds 60; #wait 1 minute $_.refresh(); #refresh service update status #validate status still stopped if($_.status -eq "stopped") { #loading mail details $to="user@domain.com" $subject="$($_.machinename) $($_.name) fails start, take immediate action avoid impact" $from="servicestatus@domain.com" $smtp="server.domain.com" $body="$($_.name) service fails start, take immediate action avoid impact" #sending email notify send-mailmessage -to $to -subject $subject -from $from -smtpserver $smtp -body $body -credential $cred } }
p.s: it's not best approach decide solve problem. if want can create function later test first.
Comments
Post a Comment