将停止和启动服务的时间戳打印到 .txt 文件中
Print Timestamp of Stop and Start Services into .txt file
我想要服务为 stopped/restarted 时的时间戳,然后输出到文件。该文件将成为附件并发送给支持人员。
我无法输出到文件,因为我的以下查询似乎出错了。
$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
没有 returns 任何东西,所以没有任何东西被发送到管道。基于此 Write-Output
未被调用。
Antoher 猜测:由于您的路径不是字符串,对 $output
的赋值失败,Powershell 可能会在命令模式下解释赋值。将其更改为:
$output = "D:\Testing\Restart.txt"
我想要服务为 stopped/restarted 时的时间戳,然后输出到文件。该文件将成为附件并发送给支持人员。
我无法输出到文件,因为我的以下查询似乎出错了。
$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
没有 returns 任何东西,所以没有任何东西被发送到管道。基于此 Write-Output
未被调用。
Antoher 猜测:由于您的路径不是字符串,对 $output
的赋值失败,Powershell 可能会在命令模式下解释赋值。将其更改为:
$output = "D:\Testing\Restart.txt"