隐藏写入主机输出 [英] Hide Write-Host outputs
问题描述
我正在编辑一个需要转换为 exe 的脚本.它有几个 Write-Host
输出脚本运行时的状态.这会导致脚本的 exe 向用户显示对话框,迫使他们在脚本完成之前单击 3 或 4 次.
I am editing a script which I need to convert to exe. It has a few Write-Host
outputs of the status of the script when running. This causing the exe of the script to show up dialog boxes to the user forcing them to click OK 3 or 4 times before the script finishes.
我想保留 Write-Host
输出但只是在最终用户从 exe 执行它时隐藏它们(对话框).
I'd like to keep the Write-Host
output but just hide them (the dialog boxes) when the end user executes it from the exe.
这可能吗?我查看了代码不喜欢的 [void]
.我可以开始工作的唯一其他方法就是用 #
注释掉它,但我相信有更好的方法.
Is this possible? I have looked into [void]
which the code doesn't like. The only other way I can get to work is just commenting it out with #
, but I'm sure there's a better way.
我想要隐藏/抑制的示例:
Example of what I want hidden/suppressed:
Write-Host "Current Status = " $status
推荐答案
根据评论你应该使用 Write-Verbose
而不是 Write-Host
因为这会给你只需很少的努力即可获得您想要的功能.但是,要使用 Write-Verbose
,您还需要进行一些其他更改:
Per the comments you should use Write-Verbose
instead of Write-Host
as this will give you the functionality you want with very little effort. However to use Write-Verbose
there's a couple of other changes you'll need to make:
首先,您需要将其添加到脚本的顶部:
First you'll need to add this to the top of your script:
[cmdletbinding()]
Param()
这为您的脚本提供了一组默认参数,其中一个包括 -Verbose
,它可以在使用时显示任何 Write-Verbose
消息.
This gives your script a set of default parameters one of which includes -Verbose
which enables any Write-Verbose
messages to be displayed when used.
其次(基于您提供的示例)您可能需要稍微重写一些(现在)Write-Verbose
字符串语句.例如:
Secondly (based on the example you gave) you might need to slightly rewrite some of your (now) Write-Verbose
string statements. For example:
write-host "Current Status = " $status
与 Write-Host
一起使用,因为它需要一个字符串数组作为输入.Write-Verbose
不一样,它只需要一个字符串,所以上面的例子需要改为:
Works with Write-Host
because it takes an array of strings as input. The same is not true of Write-Verbose
, it only takes a single string, so the above example would need to be changed to:
Write-Verbose "Current Status = $status"
请注意,通过使用双引号字符串,变量仍将被扩展.
Note that by using double quote strings the variable will still be expanded.
这篇关于隐藏写入主机输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!