繁体   English   中英

powershell 将文本保存在输出文件中

[英]powershell Save text in out-file

$timeFromFile = Get-Content 'D:\script2\IIS\timeln.txt'

$diff = [datetime]::Now - [datetime]$timeFromFile

[System.Math]::Round($diff.TotalSeconds)

if ($diff.TotalSeconds -gt 900){
Write-Host "Time difference is greater than 900 seconds"
}   
$diff.TotalSeconds | Out-File 'D:\script2\IIS\EndTime.txt'

执行上述脚本时,计算时间的结果值保存为 EndTime.txt。 但是,当 900 秒过去了,消息并没有保存在 EndTime.txt 文件中,只有时间值是 output。 怎么修?

基于评论“执行上述脚本时,结果只是数字 ex) 39568.2052459 不要记录“时间差大于 900 秒”我终于可能知道你在做什么了:)

您所做的是将秒数通过管道传输到文件( $diff.TotalSeconds | Out-File 'D:\script2\IIS\EndTime.txt' ),并且您需要 pipe 消息本身。 我在消息中添加了秒值。

$timeFromFile = [datetime]::Now

$diff = [datetime]::Now - [datetime]$timeFromFile

$diffSecs = [System.Math]::Round($diff.TotalSeconds)

#Initialize empty string variable for message
$msg = ""
if ($diffSecs -gt 900) {
    #Setting the message if greater than 900
    $msg = "Time difference is greater than 900 seconds ($diffSecs)"
}
else {
    #If lower or equal
    $msg = "Time difference IS NOT greater than 900 seconds ($diffSecs)"
}

#Setting the content of file to the $msg variable value
$msg | Out-File C:\Temp\asd.txt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM