Мне нужно отправить оповещение по электронной почте из скрипта PowerShell, но когда я запускаю скрипт, я сталкиваюсь с ошибкой, я пытался по-разному, но не помогал для моего скрипта.
Я пыталсяСценарий PowerShell, как показано ниже:
#AUTHOR: Kevin Olson
#DATE: 4/29/2011
#Machine to be monitored
$Computer = "Chandu"
#Create an array of all services running
$GetService = get-service -ComputerName $Computer
#Create a subset of the previous array for services you want to monitor
$ServiceArray = "RemoteRegistry"
#Find any iWFM service that is stopped
foreach ($Service in $GetService)
{
foreach ($srv in $ServiceArray)
{
if ($Service.name -eq $srv)
{
#check if a service is hung
if ($Service.status -eq "StopPending")
{
#email to notify if a service is down
Send-Mailmessage -to abc@example.com -Subject "$srv is hung on $Computer" -from abc@example.com -Body "The $srv service was found hung" -SmtpServer smtp.office365.com
$servicePID = (gwmi win32_Service | where { $_.Name -eq $srv}).ProcessID
Stop-Process $ServicePID
Start-Service -InputObject (get-Service -ComputerName $Computer -Name $srv)
}
# check if a service is stopped '
elseif ($Service.status -eq "Stopped")
{
#email to notify if a service is dow
Send-Mailmessage -to abc@example.com -Subject "$srv is stopped on $Computer" -from abc@example.com -Body "The $srv service was found stopped" -SmtpServer smtp.office365.com
#automatically restart the service.
Start-Service -InputObject (get-Service -ComputerName $Computer -Name $srv)
}
}
}
}
При запуске сценария я столкнулся с приведенной ниже ошибкой:
Send-Mailmessage: SMTP-сервер требует безопасного соединения, или клиент не былпроверку подлинности. Ответ сервера был: 5.7.57 SMTP;Клиент не прошел аутентификацию для отправки анонимной почты во время MAIL FROM [LO2P265CA0403.GBRP265.PROD.OUTLOOK.COM] В строке: 35 символов: 13 + Send-Mailmessage -to abc@example.com - ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo: InvalidOperation: (System.Net.Mail.SmtpClient: SmtpClient) [Se nd-MailMessage], SmtpException + FullyQualifiedErrorId: SmtpException, Microsoft.PowerShell.Commands.SendMailMessage
* 1010