简体   繁体   English

如何创建在后台运行的 powershell 脚本的日志?

[英]How can I create a log of a powershell script running in the background?

I have a powershell script that I am converting from running as a fore-front infinite while-loop to a scheduled task.我有一个 powershell 脚本,我正在将其从作为前端无限循环运行转换为计划任务。 The biggest problem here is that I would still like to maintain a log.这里最大的问题是我仍然想维护一个日志。 Start-Transcript was the bit that did the logging previously, but that doesn't work with the background task. Start-Transcript 是之前进行日志记录的部分,但这不适用于后台任务。

These links ( 1 , 2 ) show similar questions, but they only give the information that start-transcript won't work.这些链接 ( 1 , 2 ) 显示了类似的问题,但它们仅提供 start-transcript 不起作用的信息。 They don't give any indication as to how it could be done.他们不给任何指示,以如何做。

Basically you can do two things:基本上你可以做两件事:

  • Add logging routines to your script (see for instance here ).将日志记录程序添加到您的脚本中(例如参见此处)。

  • Run the script like this:像这样运行脚本:

     powershell.exe -Command "&{your.ps1 *> your.log; exit $LASTEXITCODE}"

Personally I'd prefer the former, but it'd require more changes to your code.我个人更喜欢前者,但它需要对您的代码进行更多更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何读取在后台运行的Powershell脚本的内容? - How can i read the contents of a powershell script running in the background? 如何在 PowerShell 脚本后阻止 Excel 进程在后台运行? - How can I stop Excel processes from running in the background after a PowerShell script? 如何创建powershell脚本以将数千个日志文件从一个位置移动到另一个位置? - How can i create a powershell script to move thousands of log files from one location to another? 如何从后台开始删除此脚本并弄乱 PowerShell? - How can I remove this script starting in the background and messing up with PowerShell? 如何判断 Docker Desktop 是否在 Powershell 脚本中运行? - How can I tell if Docker Desktop is running in a Powershell script? 如何自动运行提示UI选择的PowerShell脚本? - How can I automate running a PowerShell script that prompts UI for choice? 当我关闭会话或注销计算机时,计划任务是在后台继续运行 Powershell 脚本的唯一方法吗? - Is a scheduled task the only way to keep running a Powershell script in the background, when I close my session or log out of the computer? 如何创建一个 powershell 脚本来删除它所在的目录? - How can I create a powershell script that will delete the directory it lives inside of? 如何创建一个 powershell 脚本来连接到网络共享文件夹? - How can I create a powershell script to connect to network shared folders? 如何从另一个 powershell 脚本逐字编写/创建 powershell 脚本? - How can I write/create a powershell script verbatim, from another powershell script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM