如何抑制 PowerShell Get-Content 输出 [英] How to suppress PowerShell Get-Content output

查看:37
本文介绍了如何抑制 PowerShell Get-Content 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PS 脚本:

I have a PS script:

script.ps1

[System.Xml.XmlDocument] $Config;

function Get-ScriptDirectory
{
    Split-Path $script:MyInvocation.MyCommand.Path
}

function Load-Config
{
    $configPath = Join-Path (Get-ScriptDirectory) config.xml
    $global:Config = [xml](gc $configPath) 
}

Load-Config

配置文件

<Configuration>
</Configuration>

稍后在脚本中我使用 $Config 变量.当我运行此脚本时,它将输出写入控制台,其中包含 xml 的根元素.类似的东西:

Later in the script I'm working with $Config variable. When I run this script it writes the output to the console, which contains the root element of the xml. Something like:

Configuration
--------------

有没有办法抑制这个输出?

Is there exists any way how to suppress this output?

谢谢.

推荐答案

可能输出不是由赋值语句(voidable 语句)引起的,而是由这一行引起的:

Probably the output isn't caused by the assignment statement (a voidable statement), but by this line:

[System.Xml.XmlDocument] $Config;

在 PowerShell 中,通常所有语句都返回一个值(可撤销语句除外).我认为第一次运行脚本时不会将输出写入控制台.但是在后续运行中 $Config 仍将包含前一次运行的值,并且其值将被写入屏幕.

In PowerShell, typically, all statements return a value (except for voidable statements). I think that the first time you run the script no output will be written to the console. However on subsequent runs $Config will still contain the value of the previous run, and its value will be written to the screen.

  • 管道到 Out-Null cmdlet:[System.Xml.XmlDocument] $Config |Out-Null
  • 强制转换为空:[void][System.Xml.XmlDocument]$Config
  • 赋值给 $null:$null = $Config
  • 或者干脆不声明"$Config 变量

是抑制这种行为的方法.

are ways to suppress this behaviour.

这篇关于如何抑制 PowerShell Get-Content 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆