当我在powershell命令窗口中使用Write-Verbose
时,我在控制台中没有得到任何东西。然而,我的团队中的devops工程师使用它进行持续集成,构建脚本。
Write-Verbose
和Write-Host
有什么区别?
cmdlet之间的差异(从powershell-v5.0开始)是用于显示信息的流。默认情况下,除非您指定-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
看看定义
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-Verbose
,Write-Error
等cmdlet用于提供不同级别的日志记录,即在监视日志并希望按日志级别进行过滤时非常有用。这回答了诸如“我们得到了多少错误?”(Write-Error
),“这个函数被调用了吗?”之类的问题。 (Write-Debug
)
相比之下,Write-Host
通常可以在cmdlet的进度上向用户显示“输出”,要求输入等。
参考文献:
当Write-Verbose
开关传递给cmdlet时,-Verbose
只是“活动” - 否则,假设您不希望看到否则将生成的消息。
Write-Host
无条件地输出数据,并绕过PowerShell管道。
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