views:

255

answers:

3

I have a complex Powershell script that gets run as part of a SQL 2005 Server Agent Job. The script works fine, but it uses the "Start-Transcript $strLogfile -Append" command to log all of it's actions to a transcript file. The problem is that the transcript is always empty. It adds the header and footer to indicate that the transcript is starting and stopping, but it doesn't actually log anything. Example:

**********************
Windows PowerShell Transcript Start
Start time: 20100304173001
Username  : xxxxxxxxxxxx\SYSTEM 
Machine   : xxxxx-xxx (Microsoft Windows NT 5.2.3790 Service Pack 2) 
**********************
**********************
Windows PowerShell Transcript End
End time: 20100304173118
**********************

When I execute the script from a command prompt or start -> run everything works just fine. Here is the command used to run the script (same command used in the Operating system CmdExec step of the SQL Agent Job)

powershell.exe -File "c:\temp\Backup\backup script.ps1"

I first thought it must have something to do with the script running under the System account (default SQL Agent account), but even when I tried changing the SQL Agent to run under my own personal account it still created a blank transcript.

Is there any way to get PowerShell Transcripts to work when executing them as part of a 2005 SQL Server Agent Job?

+1  A: 

If your script uses native commands (console exes), Start-Transript does not log any of that output. This issue has been logged on Connect, you can vote on it. One way to capture all input is to use cmd.exe:

cmd /c powershell.exe -file "C:\temp\backup script.ps1" > backup.log
Keith Hill
The thing is that the transcript works fine when run from cmd.exe or from start -> run. It is using Powershell 2.0 and pipes the output of any executables to out-host.
Greg Bray
+1  A: 

sqlps.exe does not implement certain methods including the method that supports write-host. This may explain why you are not seeing output using Start-Transcript when running sqlps.exe from a SQL Agent Powershell jobstep. See http://blogs.msdn.com/mwories/archive/2009/09/30/the-use-of-write-host-and-sql-server-agent-powershell-job-steps.aspx for more information.

Chad Miller
Good to know, but sqlps.exe is actually a part of SQL 2008. Here I am using SQL 2005 and calling powershell.exe as part of a CmdExec step in the SQL Agent Job. My guess is that it has something to do with the Agent job not running in a console based host.
Greg Bray
A: 

I am still not sure why the Powershell Transcript is empty, but we found a workaround. Under the CmdExec step of the SQL Job there is an advance option to capture the output to a file, which combined with the "Append output to existing file" option and using a Logfile.rtf extension is about the same as the Powershell transcript. This way anything that gets printed to the host from the Powershell script (including native console executables piped to "| out-host") will be captured in the log file.

Greg Bray