我想要停止服务/重新启动时的时间戳,然后输出输出。该文件将变为附件并发送给支持。
我无法输出文件,因为我的以下查询似乎有错误。
$hostname = $env:computername
$smtpServer = 'smtpServer'
$from = "from"
$recipients = 'recipients'
$Subject = "Services Restarted Successfully on $hostname $ipv4"
$body = "This mail confirms that the service on $hostname $ipv4 is now running."
$ipv4 = (Test-Connection -ComputerName $env:computername -count 1).ipv4address.IPAddressToString
$natip = Invoke-WebRequest ifconfig.me
$timestamp = (Get-Date)
$output = D:TestingRestart.txt
$attachment = $output
$service = 'Apache'
停止服务
Stop-Service -name $service -Verbose
do {
Start-sleep -s 5 | Write-Output "$timestamp Services is stopped" | Out-file $output
}
until ((get-service $service).Status -eq 'Stopped')
开始服务
start-Service -name $service -Verbose
do {
Start-sleep -s 5 | Write-Output "$timestamp Services is restarted" | Out-file $output
}
until ((get-service $service).Status -eq 'Running')
发送确认服务已成功重新启动
Start-Sleep -s 5
Send-MailMessage -To $recipients -Subject $Subject -Body $body ((gsv Apache) | out-string) -From $from -SmtpServer $smtpServer -Attachments $attachment
如上所述,将代码更改为
Stop-Service -name $service -Verbose
do {
Start-sleep -s 5
Write-Output "$timestamp Services is stopped" | Out-file $output
} until ((get-service $service).Status -eq 'Stopped')
实际上,您的Start-Sleep
CMLEDT调用输出已发送到管道(Start-sleep - s 5 |...
(。我的猜测是Start-sleep
没有返回任何内容,因此没有发送到管道。基于该Write-Output
未调用。
Antoher Guess:$output
分配失败,因为您的路径不是字符串,PowerShell可以在命令模式下解释分配。将其更改为:
$output = "D:TestingRestart.txt"