2014-12-08 8 views
9

In che modo è possibile aggiungere timestamp a ciascuna riga di un output generato dall'operatore & PowerShell?Come aggiungere timestamp alle singole righe di PowerShell e output?

Esempio:

PS H:\> $result = & ping 192.168.1.1 
PS H:\> echo $result 

Pinging 192.168.1.1 with 32 bytes of data: 
Reply from 192.168.1.1: bytes=32 time=104ms TTL=250 
Reply from 192.168.1.1: bytes=32 time=106ms TTL=250 
Reply from 192.168.1.1: bytes=32 time=102ms TTL=250 
Reply from 192.168.1.1: bytes=32 time=102ms TTL=250 
Ping statistics for 192.168.1.1: 
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 
Approximate round trip times in milli-seconds: 
Minimum = 102ms, Maximum = 106ms, Average = 103ms 

risultato desiderato:

PS H:\> echo $result 

2014-12-08T14:45:48.8898125+00:00:Pinging 192.168.1.1 with 32 bytes of data: 
2014-12-08T14:45:48.8932661+00:00:Reply from 192.168.1.1: bytes=32 time=104ms TTL=250 
2014-12-08T14:45:48.9233451+00:00:Reply from 192.168.1.1: bytes=32 time=106ms TTL=250 
2014-12-08T14:45:48.9765438+00:00:Reply from 192.168.1.1: bytes=32 time=102ms TTL=250 
2014-12-08T14:45:49.0233105+00:00:Reply from 192.168.1.1: bytes=32 time=102ms TTL=250 
2014-12-08T14:45:49.0233201+00:00: 
2014-12-08T14:45:49.0238753+00:00:Ping statistics for 192.168.1.1: 
2014-12-08T14:45:49.0239210+00:00: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 
2014-12-08T14:45:49.0233318+00:00:Approximate round trip times in milli-seconds: 
2014-12-08T14:45:49.0237209+00:00: Minimum = 102ms, Maximum = 106ms, Average = 103ms 

so come dividere/partecipare a una serie di PowerShell, ma questo può avvenire solo dopo che l'operatore & completa. Sto cercando una soluzione più in tempo reale, in cui i timestamp vengono aggiunti all'output mentre l'operatore & è in esecuzione.

Tra l'altro, il timestamp stesso è $($(Get-Date -Format o) + ":")

risposta

33

Si potrebbe utilizzare un filtro:

filter timestamp {"$(Get-Date -Format o): $_"} 
$result = & ping 192.168.1.1 | timestamp 

output prodotto da $result:

2014-12-08T11:42:59.2827202-05:00: 
2014-12-08T11:42:59.2857205-05:00: Pinging 192.168.1.1 with 32 bytes of data: 
2014-12-08T11:43:03.1241043-05:00: Request timed out. 
2014-12-08T11:43:08.1236042-05:00: Request timed out. 
2014-12-08T11:43:13.1241042-05:00: Request timed out. 
2014-12-08T11:43:18.1246042-05:00: Request timed out. 
2014-12-08T11:43:18.1246042-05:00: 
2014-12-08T11:43:18.1246042-05:00: Ping statistics for 192.168.1.1: 
2014-12-08T11:43:18.1246042-05:00:  Packets: Sent = 4, Received = 0, Lost = 4 (100% loss), 
+0

La mia domanda era su come aggiungere timestamp per l'uscita nel primo posizionare, non come filtrarli in (o out) successivamente. –

+2

Ecco cosa sta facendo il filtro. Un filtro PowerShell è un blocco di script che accetta l'input dalla pipeline e viene eseguito una volta per ogni oggetto che ottiene dalla pipeline. Funzionalmente è uguale a una funzione con solo un blocco di processo. Questo filtro aggiungerà il timestamp a ciascun output di riga dal tuo eseguibile. – mjolinor

+0

Ah, in effetti, funziona esattamente come hai descritto. Grazie mille! Problema risolto! –

3

È possibile utilizzare il cmdlet Start-Transcript in combinazione con un prompt personalizzato nel tuo profilo:

# Configure PS prompt to show date and time 
function prompt 
{ 
    $promptStringStart = "PS:" + (Get-Date -format MM/dd/yy` hh:mm:ss) 
    $promptStringEnd += ">" 
    Write-Host $promptStringStart -NoNewline -ForegroundColor Yellow 
    Write-Host $promptStringEnd -NoNewline -ForegroundColor Yellow 
    return " " 
} 

Questo funziona perfettamente sulla mia workstation Windows 7. Tuttavia, in alcune installazioni di Server 2012 R2 più recenti, Start-Transcript sembra leggermente danneggiato. Questo risolve parte di esso: https://support.microsoft.com/en-us/help/3014136/powershell-transcript-file-doesn-t-contain-the-correct-information-in-windows-server-2012-r2

... ma non risolve il problema con il prompt personalizzato, ancora.

Per esempio, io vedo questo sulla console:

PS:02/14/17 08:28:20> hostname 
server463 

e questo è ciò che è scritto nel registro:

PS:02/14/17 08:28:20 
> 

PS>hostname 
server463 
+0

Grazie, questo è un approccio abbastanza interessante e sono sicuro che è utile in alcuni scenari. Mi sto attenendo all'approccio del filtro timestamp anche se secondo la risposta di mjolinor sopra. Si adatta meglio alle mie esigenze –

Problemi correlati