Solved

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

Posted on 2014-03-19
2
275 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…

696 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