PowerShell Force Output to Transcript File

I have a script I created that contains considerable output to the console using Write-Host. I start a transcript at the beginning of the script and all output from Wrote-Host is contained in the transcript file.
However, yesterday I made some revisions (primarily to not present message boxes) if the account that executed the script is disconnected, and sceduled the script to be executed as a job under SQL Agent. This morning, when I got in and examined the transcript file, it contained the following, with none of the output from Write-Host. I have executed the script multiple times manually, and disconnected from the machine without any loss of output, so not sure what I need to do, but I need to force the output to the transcript file, along with any output that the script might generate. Is there something I need to use instead of Write-Host?
Windows PowerShell transcript start
Start time: 20150311180206
Username  : computername\_sqlagent
Machine	  : computername (Microsoft Windows NT 6.1.7601 Service Pack 1)
Windows PowerShell transcript end
End time: 20150312013540

Open in new window

LVL 15
Doug BishopDatabase DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joshua GrantomSenior Systems AdministratorCommented:
When its run from the sql agent, I believe it's not opening a command window so there is nothing to transcribe. What you may need to do instead is output the results and append to a log file like so

$time = Get-Date -Format "MM-dd-yyyy_HH-mm-ss"
$logfile = "C:\logs\SQL_Log_$($time).log"
My command >> $logfile

Open in new window

or you can use

$time = Get-Date -Format "MM-dd-yyyy_HH-mm-ss"
$logfile = "C:\logs\SQL_Log_$($time).log"
Write-Host "My Stuff" | Out-File $logfile -Append -Force

Open in new window

If you could post what you are doing in the script, we may be able to help out a little more.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
In general, don't use the transcript option. You should use Out-File, Write-Output and the like. Write-Host is only useful for the interactive console window; any other PS host might or might not take output with Write-Host.
Doug BishopDatabase DeveloperAuthor Commented:
Basicly I am using write-host to output data to the console. It may be straight text such as:
write-host "starting the process"
a combination of text and a variable such as:
write-host "executing step $stepname"
or a variable such as:
write-host "$message"

In any case, is there a way to determine if a command window is available (e.g. it was run from SQL Agent)?
SD-WAN: Making It Work for You

As bandwidth requirements and Internet costs grow, businesses naturally want to manage budgets by reducing reliance on their most expensive connection types. Learn more about how to make SD-WAN work for your business in our on-demand webinar!

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I assume looking at $host.ui.rawui will help. It has a window title and position etc., so having those should tell it is a host with window.
Joshua GrantomSenior Systems AdministratorCommented:
a way around it may be using a different option in SQL Agent

Type: Operating System (CmdExec)

and using something like this

powershell -windowstyle minimized -c "powershell -c c:\scripts\sqlscript.ps1 -verbose >> C:\logs\mysqllog.log 2>&1"
Doug BishopDatabase DeveloperAuthor Commented:
Doing some more research, it looks like I can use tee-object to output to both a file and (of present) the console, using the syntax:
$TranscriptFile = "c:\temp\mylog.txt"
$message = "this is a test"
Write-Output $message | Tee-Object -Path $TranscriptFile - Append

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
That's correct.
Doug BishopDatabase DeveloperAuthor Commented:
Should be "... | Tee-Object -File $TranscriptFile" (note -File not -Path).
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
If we get picky, then it should be
   Write-Output $message | Tee-Object -FilePath $TranscriptFile -Append

Open in new window

-File is specific enough to match -FilePath, so that works ;-).
Doug BishopDatabase DeveloperAuthor Commented:
Doing more research, using tee-object was a more acceptable solution than those provided by other experts.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.