首页 > 解决方案 > PowerShell中的Write-Verbose vs Write-Host

问题描述

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

Write-Verbose和有什么区别Write-Host

标签: powershellpowershell-3.0

解决方案


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

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

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

同样,该Write-Hostcmdlet 使用默认情况下也不可见的信息流 (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


推荐阅读