我有一个PowerShell脚本,用于监视多个服务器上的Windows服务的状态。有人可以帮助我进行一些修改,其中脚本检查状态并重新启动服务,如果它没有运行?我还想发一封综合电子邮件。
$servers = Get-Content C:\servers.txt
$Servers | ForEach-Object {
invoke-command -ComputerName $_ -Scriptblock {Get-Service W3SVC}
}| Out-File C:\Result.txt
帮助表示赞赏。
答案 0 :(得分:0)
我再次修改了脚本,现在它正在为我工作。
#$Services=Get-content C:\Services.txt
$Servers=get-content C:\servers.txt
$SMPTServer = "smtp.ac.com"
$result = "The following service restarted:`n" # default output
$Servers|Foreach-Object{
$result += Invoke-Command -ComputerName $_ -ScriptBlock { # add output of scriptblock to $result
$CompName = (Get-WmiObject -Class Win32_ComputerSystem).Name
(Get-Service -Name Splunkd)| #Get the services running on all servers
Where-Object{$_.Status -eq "Stopped"}| #Which status is equal stopped
Foreach-Object{
if($_.Status -eq "Stopped")
{
Write-Output "`n -Service:- Splunkd is Down on Server $CompName - Service Splunkd on Server $CompName Restarted Successfully `n" # any action is added to output
$_.(Start-Service Splunkd) #try to start
#Start-Sleep -Seconds 60; #wait one minute
#$_.Refresh(); #refresh then service to update status
}}}}
if ($result -ne "The following service restarted:`n") { # If any action was taken send mail with $result
send-mailmessage -to "xx@xx.com" -from "abc@bc.com" -subject "Splunkd Service Restarted Alert" -Body $result -SmtpServer $SMPTServer
}