找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 1069|回复: 3

[技术讨论] PowerShell脚本Write-Output和Write-Host异同?

[复制链接]
发表于 2025-10-27 17:02:16 | 显示全部楼层 |阅读模式
]PowerShell脚本Write-Output和Write-Host异同
发表于 2025-10-27 18:34:37 | 显示全部楼层
回复 1# tims2025

要泛论用AI足矣...lz真正要问的关键点是什么?或遇到与之关联的具体问题是什么?
发表于 2025-10-27 19:07:24 | 显示全部楼层
输出到管道不同,Write-Output是管道1,Write-Host是管道6;Write-Output相当于cmd的echo,但是Write-Output通常省略,例如下面的结果是一样的:
  1. Write-Output "hello"
  2. "hello"
复制代码
Write-Host直接输出到屏幕,而管道1的对象可以赋值给变量,故Write-Output可以赋值给变量;
  1. $a=1+2|Write-Output
  2. $b=1+2|Write-Host
复制代码
  1. public System.Management.Automation.Runspaces.CommandParameterCollection Parameters { get; }

  2. 方法:
  3. public void MergeMyResults (System.Management.Automation.Runspaces.PipelineResultTypes myResult, System.Management.Automation.Runspaces.PipelineResultTypes toResult);
  4. 这个方法等同于 管道重定向符号 ">&" .
  5. PowerShell supports the following output streams.
  6. Stream #        Description        Introduced in        Write Cmdlet        StreamType
  7. 1        Success stream        PowerShell 2.0        Write-Output        Object
  8. 2        Error stream        PowerShell 2.0        Write-Error        System.Management.Automation.ErrorRecord
  9. 3        Warning stream        PowerShell 3.0        Write-Warning        System.Management.Automation.WarningRecord
  10. 4        Verbose stream        PowerShell 3.0        Write-Verbose        System.Management.Automation.VerboseRecord
  11. 5        Debug stream        PowerShell 3.0        Write-Debug        System.Management.Automation.DebugRecord
  12. 6        Information stream        PowerShell 5.0        Write-Information        System.Management.Automation.InformationRecord
  13. n/a        Progress stream        PowerShell 3.0        Write-Progress        System.Management.Automation.ProgressRecord
  14. *        All streams        PowerShell 3.0               
  15.         • The Progress stream does not support redirection.
  16.         • Write-Host 也是在 stream 6, streamType是 一种特殊的System.Management.Automation.InformationRecord,该实例的属性MessageData类型是System.Management.Automation.HostInformationMessage

  17. $Command.MergeMyResults("Error","Output") 等同于 2>&1
  18. $Command.MergeMyResults("Information","Output") 等同于 6>&1

  19. 发现PipelineResultTypes的 value__正好是各自的Stream number.
  20. PS> Get-EnumValues ([System.Management.Automation.Runspaces.PipelineResultTypes])
  21. None                 :        0  00000000  00000000 00000000
  22. Output               :        1  00000001  00000000 00000001
  23. Error                :        2  00000002  00000000 00000010
  24. Warning              :        3  00000003  00000000 00000011
  25. Verbose              :        4  00000004  00000000 00000100
  26. Debug                :        5  00000005  00000000 00000101
  27. Information          :        6  00000006  00000000 00000110
  28. All                  :        7  00000007  00000000 00000111
  29. Null                 :        8  00000008  00000000 00001000
复制代码
发表于 2025-10-27 22:26:54 | 显示全部楼层
其中一个可以作为函数的返回值
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 22:35 , Processed in 0.016906 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表