Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-03-19
2
Medium Priority
?
279 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 41

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

636 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