如何在 docker 容器中使用 PowerShell 在文件名中使用时间戳?

How to use a TimeStamp in a file name using PowerShell in a docker container?

我有一个 PowerShell 脚本,它接收实时日志数据并将其写入文件名中包含时间戳的文件中。在本地,我的脚本 运行ning 很好,但是当我 运行 它在 docker 容器中时,我收到以下错误消息:

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'. 

这里是我的代码的相关行:

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

我正在使用最新的 ubuntu 基础映像和 Powershell 版本:7.2.3-1.deb。

您收到的错误是因为您试图写入 /app/myapp-logs/live-logfile-10/ 下子目录中的文件 - 而目录 live-logfile-10 不存在。

在所用的日期时间格式中使用不是斜杠的东西,问题就会消失:

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

也就是说,我建议使用 big-endian 日期时间格式 - 这样可以更轻松地按日期对日志文件进行排序:

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