powershell - PowerShell中的Write-Verbose vs Write-Host
问题描述
当我Write-Verbose
在 powershell 命令窗口中使用时,我在控制台中没有得到任何东西。然而,我团队中的 devops 工程师使用它来进行持续集成、构建脚本。
Write-Verbose
和有什么区别Write-Host
解决方案
cmdlet(从powershell-v5.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-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
推荐阅读
- excel - 具有多个条件的唯一计数
- java - 如何用数学运算绑定两个 SimpleDoubleProperty?
- docker - 简单的烧瓶服务在 docker 容器外不响应
- vim - SPF VIM 语法默认开启
- .net - 浏览按钮存储路径作为变量以在其他地方使用
- bash - 使用 bash 和 awk 删除不包含字符串列表之一的行
- java - 将 Android Studio 项目迁移到桌面应用程序
- twitter-bootstrap - Bootstrap 4 Cards - 如何使卡片高度相同以及添加水平填充/间距
- firebase - Firebase 云功能/通知
- r - 包构建/安装期间“找不到对象”