Powershell中的Write-Verbose与Write-Host

Nit*_*ran 3 powershell

当我Write-Verbose在Powershell命令窗口中使用时,控制台中没有任何显示。但是,我团队中的devops工程师使用它进行持续集成,构建脚本。

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

Avs*_*lom 6

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
Run Code Online (Sandbox Code Playgroud)


The*_*le1 6

cmdlet之间的区别(从)是它们用来显示信息的流。默认情况下,除非您指定-Verbose否则将不显示Verbose流(4),请-Verbose使用$PSDefaultParameterValues自动词典添加,以将开关附加到所有或特定cmdlet,或设置$VerbosePreference自动变量。

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

PS ~/> Write-Verbose -Message 'Just testing' -Verbose 4>$null
PS ~/> Write-Verbose -Message 'Just testing' -Verbose
VERBOSE: Just testing
Run Code Online (Sandbox Code Playgroud)

同样,该Write-Hostcmdlet使用信息流(6),该信息流在默认情况下也不可见,但Write-Host实际上已成为

Write-Information -InformationAction Continue
Run Code Online (Sandbox Code Playgroud)

此流具有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;}
Run Code Online (Sandbox Code Playgroud)

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

[System.Management.Automation.ActionPreference].GetEnumValues()
Run Code Online (Sandbox Code Playgroud)

about_Redirection