Solved

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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...
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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