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
dbbishopAsked:
Who is Participating?
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.
0
QlemoBatchelor, 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.
0
dbbishopAuthor 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)?
0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

QlemoBatchelor, 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.
0
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"
0
dbbishopAuthor 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
0

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
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
That's correct.
0
dbbishopAuthor Commented:
Should be "... | Tee-Object -File $TranscriptFile" (note -File not -Path).
0
QlemoBatchelor, 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 ;-).
0
dbbishopAuthor Commented:
Doing more research, using tee-object was a more acceptable solution than those provided by other experts.
0
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.