[英]Print Timestamp of Stop and Start Services into .txt file
我想要服务停止/重新启动时的时间戳,然后输出到文件。 该文件将成为附件并发送给支持人员。
我无法输出到文件,因为我的以下查询似乎出错了。
$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 -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
不被调用。
另一个猜测:由于您的路径不是字符串,因此分配给$output
失败,Powershell 可能会在命令模式下解释分配。 将其更改为:
$output = "D:\Testing\Restart.txt"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.