PowerShell中的Write-Verbose与Write-Host

问题描述 投票:1回答:4

当我在powershell命令窗口中使用Write-Verbose时,我在控制台中没有得到任何东西。然而,我的团队中的devops工程师使用它进行持续集成,构建脚本。

Write-VerboseWrite-Host有什么区别?

powershell
4个回答
5
投票

cmdlet之间的差异(从开始)是用于显示信息的流。默认情况下,除非您指定-Verbose,使用-Verbose自动字典添加$PSDefaultParameterValues以将开关附加到所有或特定cmdlet,或设置$VerbosePreference自动变量,否则不会显示详细流(4)。

您可以这样观察此流行为:

PS ~/> Write-Verbose -Message 'Just testing' -Verbose 4>$null
PS ~/> Write-Verbose -Message 'Just testing' -Verbose
VERBOSE: Just testing

同样,Write-Host cmdlet使用信息流(6),默认情况下也不可见,但Write-Host基本上成为了一个包装器

Write-Information -InformationAction Continue

该流具有与Verbose流相同的要求,其中偏好变量为$InformationPreference

您还可以通过分配输出来观察这些对象:

PS ~/> $output = Write-Verbose -Message test -Verbose 4>&1
PS ~/> $output | Get-Member

   TypeName: System.Management.Automation.VerboseRecord
Name                  MemberType   Definition
----                  ----------   ----------
Equals                Method       bool Equals(System.Object obj)
GetHashCode           Method       int GetHashCode()
GetType               Method       type GetType()
ToString              Method       string ToString()
WriteVerboseStream    NoteProperty bool WriteVerboseStream=True
InvocationInfo        Property     System.Management.Automation.InvocationInfo InvocationInfo {get;}
Message               Property     string Message {get;set;}
PipelineIterationInfo Property     System.Collections.ObjectModel.ReadOnlyCollection[int] PipelineIterationInfo

PS ~/> $output = Write-Host -Object test 6>&1
PS ~/> $output | Get-Member

   TypeName: System.Management.Automation.InformationRecord
Name                   MemberType   Definition
----                   ----------   ----------
Equals                 Method       bool Equals(System.Object obj)
GetHashCode            Method       int GetHashCode()
GetType                Method       type GetType()
ToString               Method       string ToString()
WriteInformationStream NoteProperty bool WriteInformationStream=True
Computer               Property     string Computer {get;set;}
ManagedThreadId        Property     uint ManagedThreadId {get;set;}
MessageData            Property     System.Object MessageData {get;}
NativeThreadId         Property     uint NativeThreadId {get;set;}
ProcessId              Property     uint ProcessId {get;set;}
Source                 Property     string Source {get;set;}
Tags                   Property     System.Collections.Generic.List[string] Tags {get;}
TimeGenerated          Property     datetime TimeGenerated {get;set;}
User                   Property     string User {get;set;}

首选项变量的有效值如下:

[System.Management.Automation.ActionPreference].GetEnumValues()

about_Redirection


1
投票

看看定义

Write-Verbose将文本写入详细消息流。

Write-Host将自定义输出写入主机。

我认为你的devops工程师会在运行脚本之前设置$VerbosePreference = "Continue",因为Verbose日志也会输出到控制台。

我们来看一个例子吧

PS > Write-Verbose "hello"
> NO OUTPUT
PS > Write-Host "hello"
hello
PS >  $VerbosePreference = "Continue"
PS > Write-Verbose "hello"
VERBOSE: hello

需要记住的重要一点是像Write-VerboseWrite-Error等cmdlet用于提供不同级别的日志记录,即在监视日志并希望按日志级别进行过滤时非常有用。这回答了诸如“我们得到了多少错误?”(Write-Error),“这个函数被调用了吗?”之类的问题。 (Write-Debug

相比之下,Write-Host通常可以在cmdlet的进度上向用户显示“输出”,要求输入等。

参考文献:

  1. https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host?view=powershell-6
  2. https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-verbose?view=powershell-6

0
投票

Write-Verbose开关传递给cmdlet时,-Verbose只是“活动” - 否则,假设您不希望看到否则将生成的消息。

Write-Host无条件地输出数据,并绕过PowerShell管道。


0
投票

Write-Verbose仅在使用-Verbose参数时写入控制台。

Write-Host无论如何写到控制台......

您需要在[CmdletBinding()]部分之前将Param添加到文件中以启用-Verbose参数...

见例子:

[CmdletBinding()]
Param(
)
Write-Verbose "Verbose"
Write-Host "Host"

PS C:\> .\test.ps1
Host
PS C:\> .\test.ps1 -Verbose
VERBOSE: Verbose
Host
© www.soinside.com 2019 - 2024. All rights reserved.