2016-09-21 54 views
0

我有一个工具可以将某些数据记录到文件中。我想尾接文件并通过mosquitto_pub发送最后一行数据。 我已经使用powershell“Get-Content”命令而没有成功。 这里是我的命令:Get-Content mosquitto_pub

Get-Content -Path "C:\test.txt" -Wait | .\mosquitto_pub.exe -t "Events"

但没有什么是由mosquitto_pub出版。

如果我使用Get-Content -Path "C:\test.txt" -Wait 我看到stdout中文件的尾部。

我的解决方案有什么问题?

谢谢!

回答

0

阅读本文Q and A

另一种方法

$minsToRunFor = 10 
$secondsToRunFor = $minsToRunFor * 60 

foreach ($second in $secondsToRunFor){ 

    $lastline = Get-Content -Path "C:\test.txt" | Select-Object -last 1 

    # added condition as per VonPryz's good point 
    # (otherwise will add lastline regardless of whether it's new or not) 
    if ($lastline -ne $oldlastline){ 
     .\mosquitto_pub.exe -t "Events" -m "$lastline" 
    } 

    $oldlastline = $lastline 
    Start-Sleep 100 
} 
+0

这是不是保持连连发送的最后一行,即使不添加新的内容? – vonPryz

+0

谢谢,修正答案。 – gms0ulman

+0

...除非文件多次添加相同的内容。或者在睡眠期间多次追加... – vonPryz

相关问题