Solved

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

Posted on 2014-03-19
2
264 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
2 Comments
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
A procedure for exporting installed hotfix details of remote computers using powershell
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now