|
|
发表于 2025-10-27 19:07:24
|
显示全部楼层
输出到管道不同,Write-Output是管道1,Write-Host是管道6;Write-Output相当于cmd的echo,但是Write-Output通常省略,例如下面的结果是一样的:- Write-Output "hello"
- "hello"
复制代码 Write-Host直接输出到屏幕,而管道1的对象可以赋值给变量,故Write-Output可以赋值给变量;- $a=1+2|Write-Output
- $b=1+2|Write-Host
复制代码- public System.Management.Automation.Runspaces.CommandParameterCollection Parameters { get; }
- 方法:
- public void MergeMyResults (System.Management.Automation.Runspaces.PipelineResultTypes myResult, System.Management.Automation.Runspaces.PipelineResultTypes toResult);
- 这个方法等同于 管道重定向符号 ">&" .
- PowerShell supports the following output streams.
- Stream # Description Introduced in Write Cmdlet StreamType
- 1 Success stream PowerShell 2.0 Write-Output Object
- 2 Error stream PowerShell 2.0 Write-Error System.Management.Automation.ErrorRecord
- 3 Warning stream PowerShell 3.0 Write-Warning System.Management.Automation.WarningRecord
- 4 Verbose stream PowerShell 3.0 Write-Verbose System.Management.Automation.VerboseRecord
- 5 Debug stream PowerShell 3.0 Write-Debug System.Management.Automation.DebugRecord
- 6 Information stream PowerShell 5.0 Write-Information System.Management.Automation.InformationRecord
- n/a Progress stream PowerShell 3.0 Write-Progress System.Management.Automation.ProgressRecord
- * All streams PowerShell 3.0
- • The Progress stream does not support redirection.
- • Write-Host 也是在 stream 6, streamType是 一种特殊的System.Management.Automation.InformationRecord,该实例的属性MessageData类型是System.Management.Automation.HostInformationMessage
- $Command.MergeMyResults("Error","Output") 等同于 2>&1
- $Command.MergeMyResults("Information","Output") 等同于 6>&1
- 发现PipelineResultTypes的 value__正好是各自的Stream number.
- PS> Get-EnumValues ([System.Management.Automation.Runspaces.PipelineResultTypes])
- None : 0 00000000 00000000 00000000
- Output : 1 00000001 00000000 00000001
- Error : 2 00000002 00000000 00000010
- Warning : 3 00000003 00000000 00000011
- Verbose : 4 00000004 00000000 00000100
- Debug : 5 00000005 00000000 00000101
- Information : 6 00000006 00000000 00000110
- All : 7 00000007 00000000 00000111
- Null : 8 00000008 00000000 00001000
复制代码 |
|