Suppressing undesired output that appears in generated EXE

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
andycarpenter@westat.com
Posts: 18
Last visit: Wed Jan 17, 2024 9:56 am

Suppressing undesired output that appears in generated EXE

Post by andycarpenter@westat.com »

I'm using PowerShell Studio to build an EXE of a large PS1 that needs a clean but basic user interface on the console. I find that when the generated EXE runs there are cmdlets that generate undesirable informative messages. These don't show up when running in PowerShell. I can't seem to find a way to eliminate them (e.g., preferences).

Here's some code snipets:
  1. $VerbosePreference="SilentlyContinue"
  2. $WarningPreference="SilentlyContinue"
  3.  
  4. Write-Host "`nBefore Invoke-WebRequest"
  5. $URL="http://www.timeanddate.com/worldclock/fullscreen.html?n=263"
  6. $URL_Contents=Invoke-WebRequest -URI $URL -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
  7.  
  8. Write-Host "`nBefore Get-NetConnectionProfile"
  9. $SSIDName=(Get-NetConnectionProfile).Name
Here's the output from PowerShell:
Before Invoke-WebRequest

Before Get-NetConnectionProfile

Here's the output from the EXE:
Before Invoke-WebRequest
Reading web response completed. (Number of bytes read: 3824)

Before Get-NetConnectionProfile

1/1 completed

Any ideas how to make this console output in italics NOT appear? I'd appreciate your assistance.

Thanks,
Andy
User avatar
OldLost
Posts: 55
Last visit: Wed Feb 21, 2018 8:00 am

Re: Suppressing undesired output that appears in generated EXE

Post by OldLost »

You could try adding *>sometempfile to the end of the commands you wish all output suppressed from, e.g.,
  1. $URL_Contents=Invoke-WebRequest -URI $URL -ErrorAction SilentlyContinue -WarningAction SilentlyContinue *>sometempfile
then delete the temp file when you're done.

Or use a subset of handle numbers if it suppresses too much. Type help about_Redirection for more info.

HTH
User avatar
juneblender
Posts: 93
Last visit: Thu Mar 30, 2017 8:54 am

Re: Suppressing undesired output that appears in generated EXE

Post by juneblender »

The best practice is to pipe the selected output to Out-Null or assign it to the $null variable.
... | Out-Null
$null = ...

But, I bet you can be more selective. If these are truly informational messages, i.e. in the Information stream, you can use the InformationAction common parameter to suppress them. For info, see https://blogs.technet.microsoft.com/hey ... on-stream/

To tell which stream the messages are coming from, try to save them in a variable or, as @OldLost suggests, redirect them. In the meantime, I'll try to spin up a script that tells which stream a message is coming from.

-- juneb

June Blender
Technology Evangelist
SAPIEN Technologies, Inc.
User avatar
andycarpenter@westat.com
Posts: 18
Last visit: Wed Jan 17, 2024 9:56 am

Re: Suppressing undesired output that appears in generated EXE

Post by andycarpenter@westat.com »

Thanks for your suggestions. I think piping the output and Out-Null were both things that I had tried without success (i.e., the messages still went to the console), but I'll give them a try and let you know.

Thanks

EDIT: Just tried both piping and Out-Null. No help in the EXE. The unwanted output still appears.

Any other ideas? Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Suppressing undesired output that appears in generated EXE

Post by jvierra »

Some exe outputs cannot be suppressed.

The messages you are getting are not normal for the commands. I suspect there may be a configuration error. I have used those commands many times and have never seen spurious output.

For info: Assigning the command to avraiable performs the same function as [I[ing to Ou-Null.

I use this because it is more explicit and easier to see in a script:

[void]Invoke-WebRequest -URI $URL

Of course it makes little sense in this scenario.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Suppressing undesired output that appears in generated EXE

Post by jvierra »

If I run this from an EXE I get the same issues. Using Ou-Null or any other method has no effect.
User avatar
andycarpenter@westat.com
Posts: 18
Last visit: Wed Jan 17, 2024 9:56 am

Re: Suppressing undesired output that appears in generated EXE

Post by andycarpenter@westat.com »

Thanks.

When you say some EXE outputs can't be suppressed, do you mean that console output from some cmdlets can't be suppressed in a generated EXE?

When you say a configuration error, can you please be more specific? The output doesn't show in PowerShell or PowerShell ISE, so is it some kind of configuration error in building the EXE? It really is 100% defaults. Really, the code above will demonstrate the error when built into an EXE with the default settings.

Thanks for your ideas. It's seems really odd that it's impossible to suppress these, but only when running as an EXE.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Suppressing undesired output that appears in generated EXE

Post by jvierra »

I am going to vote on a bug/feature in the Sapien Host. I bet running this under the VS debugger will find the issue. I suspect there is some odd API debug code laying around in the API that is called by these CmdLets.

bottom line it cannot be a bug in the script as written.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Suppressing undesired output that appears in generated EXE

Post by jvierra »

AndyCarpenter@westat.com wrote:Thanks.

When you say some EXE outputs can't be suppressed, do you mean that console output from some cmdlets can't be suppressed in a generated EXE?

When you say a configuration error, can you please be more specific? The output doesn't show in PowerShell or PowerShell ISE, so is it some kind of configuration error in building the EXE? It really is 100% defaults. Really, the code above will demonstrate the error when built into an EXE with the default settings.

Thanks for your ideas. It's seems really odd that it's impossible to suppress these, but only when running as an EXE.
Andy see my last post. It is not a config error.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Suppressing undesired output that appears in generated EXE

Post by jvierra »

Doing this at a command prompt suppresses the output.

Test-EXE.exe > nul:

From a PowersShell prompt:
.\Test-EXE.exe | Out-Null
.\Test-EXE.exe > $null


The output is coming from code not in the CmdLet. Alex?
This topic is 7 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked