如何在 powershell 中追加到日志文件?
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 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.
sqlcmd
是否可能将其日志输出到 stderr?如果是这样,则在末尾放置一个2>&1
,这使得标准错误流与标准输出流的方式相同。如果您已经在使用 PowerShell,我敢问为什么还要通过
cmd
执行所有这些操作?Start-Process
有什么问题? :-)Does
sqlcmd
maybe output its log to stderr? If so, then put a2>&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 withStart-Process
? :-)