1

I have a powershell line that works great:

Start-Job { plink.exe -telnet 000.000.000.000 -P 1000 > LogFile.txt }

Job runs as expected in the background, text file is created.

When I throw TimeStamp in there than run the job, Get-Job shows it completed, and the file is not created. Not sure if it makes the connection or not in this case.

Start-Job { plink.exe -telnet 000.000.000.000 -P 1000 | TimeStamp > LogFile.txt }

When I run the above without Start-Job and with TimeStamp it works fine. Obviously it is finishing the job at TimeStamp. Any ideas for a workaround or another way to get the timestamp in the log file. Running it as a job is the way i want to go mainly So I can stop it at a specific time, parse the logs than restart with a new file name. Here the rest of the script:

filter timestamp {"$(Get-Date -Format MM/dd/yyyy_HH:mm:ss) $_"} 

Start-Job -Name Logging { c:\plink.exe -telnet 000.000.000.000 -P 10000 | TimeStamp  
> c:\users\User\"LogFile_$(get-date -f MM-dd-yyyy).txt" }

[datetime]$JobEndTime = '23:50' 
$AddDaysWhenInPast = 1  
[datetime]$CurrentTime = Get-Date
If ($JobEndTime -lt $CurrentTime) { $JobEndTime = 
$JobEndTime.AddDays($AddDaysWhenInPast) }
[int]$WaitSeconds = ( $JobEndTime - $CurrentTime ).TotalSeconds

#Debug
#write-Host $JobEndTime
#Write-Host $AddDaysWhenInPast
#Write-Host $CurrentTime
#Write-Host $WaitSeconds
#Write-Host $AddDaysWhenInPast
#Pause

Stop-Job logging

Set-Content -Path C:\users\User\"LogFile_$(get-date -f MM-dd-yyyy).txt" -Value (get- 
content -Path C:\users\users\User\"LogFile_$(get-date -f MM-dd-yyyy).txt" | Where- 
Object { $i % 2 -eq 0; $i++ })

Set-Content -Path C:\users\User\"LogFile_$(get-date -f MM-dd-yyyy).txt" -Value (get- 
content -Path C:\users\User\"LogFile_$(get-date -f MM-dd-yyyy).txt" | Select-String - 
Pattern '00000' -NotMatch)

(loops back to top)
Retrotube
  • 35
  • 5
  • your `TimeStamp` filter doesn't exist in the scope of your Job, you need to define it there. Different cmdlet but the concept is exactly the same https://stackoverflow.com/a/61273544/15339544 – Santiago Squarzon May 04 '22 at 02:11
  • 1
    @SantiagoSquarzon Nice, didn't even think of that, guessing that may be why the same problem line didn't work when I had it in a function. I'll update If that does it, sounds like the right track. Thanks! – Retrotube May 04 '22 at 02:18
  • @SantiagoSquarzon I ran in to one more issue with the same line when I changed plink.exe to $var\plink.exe. Aside from this, I have all the other issues worked out. Any chance you could help with this one: https://stackoverflow.com/questions/72118674/how-to-pass-arguments-to-program-when-using-variable-as-path – Retrotube May 04 '22 at 21:50

0 Answers0