[英]Print Timestamp of Stop and Start Services into .txt file
I want timestamp when services is stopped/restarted then output to file.我想要服务停止/重新启动时的时间戳,然后输出到文件。 The file will become as attached and send to support.
该文件将成为附件并发送给支持人员。
I cannot output to file as my below query seems got error.我无法输出到文件,因为我的以下查询似乎出错了。
$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:\Testing\Restart.txt
$attachment = $output
$service = 'Apache'
Stop service停止服务
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启动服务
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')
Send confirmation that service has restarted successfully发送确认服务已成功重启
Start-Sleep -s 5
Send-MailMessage -To $recipients -Subject $Subject -Body $body ((gsv Apache) | out-string) -From $from -SmtpServer $smtpServer -Attachments $attachment
As stated in above comments, change your code to如上述评论中所述,将您的代码更改为
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')
Actually your Start-Sleep
cmledt calls output is sent to the pipeline ( Start-sleep - s 5 |...
).实际上,您的
Start-Sleep
cmledt 调用输出被发送到管道( Start-sleep - s 5 |...
)。 My guess is that Start-sleep
doesn't returns anything, so nothing is send to the pipeline.我的猜测是
Start-sleep
不返回任何内容,因此没有任何内容发送到管道。 Based on that Write-Output
is not called.基于该
Write-Output
不被调用。
Antoher guess: Assignment to $output
fails since your path is not a string, Powershell may interpret the assignment in command mode.另一个猜测:由于您的路径不是字符串,因此分配给
$output
失败,Powershell 可能会在命令模式下解释分配。 Change it to:将其更改为:
$output = "D:\Testing\Restart.txt"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.