Home > Software design >  How to use a TimeStamp in a file name using PowerShell in a docker container?
How to use a TimeStamp in a file name using PowerShell in a docker container?

Time:05-11

I have a PowerShell script that receives live log data and writes it into a file which contains a timestamp in the filename. Locally my script is running fine but when I run it in a docker container I get the following error message:

Out-File: /app/myscript.ps1:13

Could not find a part of the path
     | '/app/myapp-logs/live-logfile-10/05/2022-14/12.txt'. 

Here the relevant line of my code:

cf logs myapp > ".\myapp-logs\live-logfile-$(Get-Date -Format "dd/MM/yyyy-HH/mm").txt"

I'm using the latest ubuntu base image and the Powershell Version: 7.2.3-1.deb.

CodePudding user response:

The error you receive is because you're trying to write to a file in a subdirectory underneath /app/myapp-logs/live-logfile-10/ - and the directory live-logfile-10 doesn't exist.

Use something that isn't a slash in the datetime format used and the problem will go away:

cf logs myapp > ".\myapp-logs\live-logfile-$(Get-Date -Format "dd_MM_yyyy-HH_mm").txt"  

That being said, I'd recommend using a big-endian datetime format - this will make it much easier to sort the log files by date:

cf logs myapp > ".\myapp-logs\live-logfile-$(Get-Date -Format "yyyyMMdd-HHmm").txt"
  • Related