【问题标题】:Prevent Add-Content From Displaying String防止添加内容显示字符串
【发布时间】:2020-08-06 21:10:36
【问题描述】:

执行这个

$String = "Testing123"    
$String | Add-Content -PassThru $Env:WinDir\System32\Test\Test.txt
Pause

执行时返回this

Testing123
Press Enter to continue...:

如何防止在运行此脚本时出现“Testing123”?我知道暂停功能,删除它并不能解决我的困境

感谢您的宝贵时间。

【问题讨论】:

  • 不要使用-PassThru 来抑制来自Add-Content 的输出。如果您想要输出但希望稍后再看到,请使用$later = $String | Add-Content -Passthru ...。然后在将来的某个时间致电$later
  • @AdminOfThings - 就是这样!感谢您的精彩解释 - 如果您想将您的评论设置为答案 - 我会将其标记为已回答!

标签: powershell powershell-ise


【解决方案1】:

默认情况下,Add-Content 不会向成功流生成任何输出。当添加参数-PassThru 时,成功流(在您的情况下为控制台输出)也接收添加的内容。

要防止来自Add-Content 的控制台输出,请不要使用-PassThru

$String = "Testing123"    
$String | Add-Content $Env:WinDir\System32\Test\Test.txt
Pause

要捕获添加的内容以在脚本的未来部分显示,请执行以下操作:

$String = "Testing123"    
$later = $String | Add-Content -PassThru $Env:WinDir\System32\Test\Test.txt
Pause
# Other code runs here
$later # Outputting added content here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2015-08-28
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 2010-12-27
    • 2018-04-23
    相关资源
    最近更新 更多