开发者

How to append to a log file in powershell?

开发者 https://www.devze.com 2022-12-23 15:23 出处:网络
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 t

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 star开发者_运维知识库t "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.


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.


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? :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消