?
Solved

Can a script determine where "show-output" is going?

Posted on 2014-03-19
2
Medium Priority
?
278 Views
Last Modified: 2014-03-19
Hi

I am running a script, sometimes using "out-file" to catch messages, sometimes not.
In my script is a "write-host" cmdlt so I can see the messages and then I do a write-output cmdlt to ensure it is also in the out-file.

If I have not used an "out-file", I get the information twice on the screen.
To avoid this, can I detect if a running script is using  "out-file"?
Then I could supress "write-output" when there is no out-file.
Thanks.
0
Comment
Question by:dreadman2k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
footech earned 2000 total points
ID: 39941466
Why not just use Write-Output and skip the Write-Host commands?  Then if you if you have the Out-File it will go to a file, and if not then it will display on screen.  Then you're just choosing which you want.

Another option is to create your script using a parameter that you set as desired for whether you want to display output or not.  For example, setting a variable like $display=$false, and checking throughout the script if $display -eq $false then skipping the Write-Host commands.

Third option would be instead of piping to Out-File, pipe to Out-Null instead.

In short, I haven't heard of nor can I think of any way to detect the use of Out-File (and what about any other cmdlets that produce files like Set-Content, Export-CSV, etc.).  A script is processed in a synchronous/serial (i.e. one thing after another) fashion.  A pipeline isn't going to know what is at the end of it until it gets to it.
0
 
LVL 2

Author Closing Comment

by:dreadman2k
ID: 39941548
Thanks footech,

I will go with the parameter & send write-output to out-null if no parameter is specified.
Your last paragraph makes sense - I guess I was approaching this from the wrong direction.
Cheers!
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question