如何在 powershell 中追加到日志文件?

发布于 2024-08-25 20:00:10 字数 846 浏览 5 评论 0原文

我正在 powershell 中进行一些并行 SQL Server 2005 数据库恢复。我这样做的方法是使用 cmd.exe 并启动,这样 powershell 就不会等待它完成。我需要做的是将输出通过管道附加到日志文件中。如果我使用 Add-Content,那么 powershell 会等待,这不是我想要的。

我的代码片段是

foreach ($line in $database_list)
{
<snip>
    # Create logins
    sqlcmd.exe -S $instance -E -d master -i $loginsFile -o $logFile

    # Read commands from a temp file and execute them in parallel with sqlcmd.exe
    cmd.exe /c start "Restoring $database" /D"$pwd" sqlcmd.exe -S $instance -E -d master -i $tempSQLFile -t 0 -o $logFile

    [void]$logFiles.Add($logFile)
}

问题是 sqlcmd.exe -o 覆盖。我尝试这样做来附加:

    cmd.exe /c start "Restoring $database" /D"$pwd" sqlcmd.exe -S $instance -E -d master -i $tempSQLFile -t 0 >> $logFile

但它不起作用,因为输出保留在 SQLCMD 窗口中并且不会转到文件中。有什么建议吗?

谢谢, 标记。

I am doing some parallel SQL Server 2005 database restores in powershell. The way I have done it is to use cmd.exe and start so that powershell doesn't wait for it to complete. What I need to do is to pipe the output into a log file with append. If I use Add-Content, then powershell waits, which is not what I want.

My code snippet is

foreach ($line in $database_list)
{
<snip>
    # Create logins
    sqlcmd.exe -S $instance -E -d master -i $loginsFile -o $logFile

    # Read commands from a temp file and execute them in parallel with sqlcmd.exe
    cmd.exe /c start "Restoring $database" /D"$pwd" sqlcmd.exe -S $instance -E -d master -i $tempSQLFile -t 0 -o $logFile

    [void]$logFiles.Add($logFile)
}

The problem is that sqlcmd.exe -o overwrites. I've tried doing this to append:

    cmd.exe /c start "Restoring $database" /D"$pwd" sqlcmd.exe -S $instance -E -d master -i $tempSQLFile -t 0 >> $logFile

But it doesn't work because the output stays in the SQLCMD window and doesn't go to the file. Any suggestions?

Thanks,
Mark.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巡山小妖精 2024-09-01 20:00:10

如果 sqlcmd.exe 的 -o 有效但被覆盖,我将为每次恢复创建一个日志文件,然后将每个日志文件的内容通过管道返回到一个主日志文件中。然后您可以根据需要清理中间文件。

If -o for the sqlcmd.exe works but overwrites, I would create a log file for each restore and then have all have return piped the contents of each log file into one master log file. Then you can clean up the intermediate files if you choose.

谁与争疯 2024-09-01 20:00:10

sqlcmd 是否可能将其日志输出到 stderr?如果是这样,则在末尾放置一个 2>&1 ,这使得标准错误流与标准输出流的方式相同。

如果您已经在使用 PowerShell,我敢问为什么还要通过 cmd 执行所有这些操作? Start-Process 有什么问题? :-)

Does sqlcmd maybe output its log to stderr? If so, then put a 2>&1 at the end which makes the standard error stream go the same way as the standard output stream.

And dare I ask why you are doing all that through cmd if you're using PowerShell already? What's wrong with Start-Process? :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文