How to append to a log file in powershell?

Posted by Mark Allison on Stack Overflow See other posts from Stack Overflow or by Mark Allison
Published on 2010-03-16T09:26:06Z Indexed on 2010/03/17 3:31 UTC
Read the original article Hit count: 282

Filed under:
|
|

Hi there,

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.

© Stack Overflow or respective owner

Related posts about powershell

Related posts about sqlcmd