使用PowerShell记录程序运行时



我想记录特定程序的运行时间,并将时间记录在文件中(纯文本或Excel(。

作为输出示例:

01.01.2018 notepad.exe Start: 06:30 End: 12:30 Duration: 6h
01.01.2018 notepad.exe Start: 14:00 End: 15:00 Duration: 1h
05.01.2018 ....

我尝试过各种各样的东西,但我的知识还不够,而且我没有取得任何进展。没有任何级别的日志记录,因为它破坏了我的输出。

我现在拥有的是:

$name = 'openvpn-gui'
$anzahl = $prozesse.Count
$läuft = $anzahl -gt 0
$prozesse = Get-Process -Name $name -ErrorAction SilentlyContinue
$Path ="$env:CLogLog.txt"
if ($läuft) {
New-TimeSpan -Start($prozesse).StartTime
} else {
"$name läuft nicht"
}

背景是我想记录openvpn的使用。该工具每天使用多少小时?

由于我刚刚习惯PowerShell,我想我可以这样做。但如上所述,我很快就达到了极限,没有继续下去的线索。


好!我设法做了一些事情:(

$start = (get-process openvpn-gui).StartTime
wait-process openvpn-gui                           
$end = Get-Date                                
$Runtime = $end - $start
Write-Host "Startzeit $start"
write-host "Endzeit $end"
write-host "$($Runtime.Hours) Stunden, $($Runtime.Minutes) Minuten, $($Runtime.Seconds) Sekunden"

这里仍然存在2个错误:如果在脚本运行时没有执行openvpn gui,则会出现错误消息

我该如何将其记录到文件中?我尝试使用Out File-FilePath。。。导致创建Log.txt,但没有内容

您可以创建一个快捷方式,调用一个记录开始时间的脚本,然后为您加载openvpn,然后您可以在openvpn运行时调用您的脚本。您可能需要引入几秒钟的延迟,以便在调用下面的脚本之前加载openvpn。

$start = (get-process openvpn-gui).StartTime
wait-process openvpn-gui                           
$end = Get-Date                                
$Runtime = $end - $start
Write-Host "Startzeit $start"
write-host "Endzeit $end"
write-host "$($Runtime.Hours) Stunden, $($Runtime.Minutes) Minuten, $($Runtime.Seconds) Sekunden"

你可以像一样将变量写入文件

$StartTime = Get-Date -Format yyyyMMdd-HHmmss
$MyFilenamePath = "c:testOpenVPNStart$StartTime.txt"
$StartTime | sc $MyFilenamePath 

最新更新