Write-Information
指定 Windows PowerShell 如何处理命令的信息流数据。
语法
Write-Information
[-MessageData] <Object>
[[-Tags] <String[]>]
[<CommonParameters>]
说明
cmdlet Write-Information
指定Windows PowerShell如何处理命令的信息流数据。
Windows PowerShell 5.0 引入了一个新的结构化信息流(Windows PowerShell 流中的编号 6),可用于在脚本和其调用方(或主机环境)之间传输结构化数据。
Write-Information
允许向流添加信息性消息,并指定Windows PowerShell如何处理命令的信息流数据。 信息流也适用于 PowerShell.Streams
、作业、计划作业和工作流。
注意
信息流不遵循以“[Stream Name]:”作为其消息前缀的标准约定。 这是为了简洁和视觉清洁。
首选项 $InformationPreference
变量值确定你提供给 Write-Information
的消息是否显示在脚本操作的预期点。
由于此变量的默认值为 SilentlyContinue
,因此默认情况下不显示信息性消息。
如果不想更改 的值 $InformationPreference
,可以通过将通用参数添加到 InformationAction
命令来替代其值。
有关详细信息,请参阅 about_Preference_Variables 和 about_CommonParameters。
注意
从 Windows PowerShell 5.0 开始,Write-Host
是一个包装器Write-Information
,它允许你使用 Write-Host
向信息流发出输出。
这样就可以 捕获 或 禁止 使用 Write-Host
写入的数据,同时保持向后兼容性。
有关详细信息,请参阅 Write-Host
Write-Information
也是受支持的工作流活动。
示例
示例 1:为 Get- results 写入信息
Get-WindowsFeature -Name p*; Write-Information -MessageData "Got your features!" -InformationAction Continue
Display Name Name Install State
------------ ---- -------------
[ ] Print and Document Services Print-Services Available
[ ] Print Server Print-Server Available
[ ] Distributed Scan Server Print-Scan-Server Available
[ ] Internet Printing Print-Internet Available
[ ] LPD Service Print-LPD-Service Available
[ ] Peer Name Resolution Protocol PNRP Available
[X] Windows PowerShell PowerShellRoot Installed
[X] Windows PowerShell 5.0 PowerShell Installed
[ ] Windows PowerShell 2.0 Engine PowerShell-V2 Removed
[X] Windows PowerShell ISE PowerShell-ISE Installed
Got your features!
在此示例中,在运行 Get-WindowsFeature
命令查找 Name 值以“p”开头的所有特征后,将显示一条信息性消息“获取了你的功能!”
由于变量 $InformationPreference
仍设置为其默认值 , SilentlyContinue
因此添加 InformationAction
参数以替代 $InformationPreference
值并显示消息。
值为 InformationAction
Continue,这意味着显示消息,但脚本或命令会继续(如果尚未完成)。
示例 2:写入信息并进行标记
Get-WindowsFeature -Name p*; Write-Information -MessageData "To filter your results for PowerShell, pipe your results to the Where-Object cmdlet." -Tags "Instructions" -InformationAction Continue
Display Name Name Install State
------------ ---- -------------
[ ] Print and Document Services Print-Services Available
[ ] Print Server Print-Server Available
[ ] Distributed Scan Server Print-Scan-Server Available
[ ] Internet Printing Print-Internet Available
[ ] LPD Service Print-LPD-Service Available
[ ] Peer Name Resolution Protocol PNRP Available
[X] Windows PowerShell PowerShellRoot Installed
[X] Windows PowerShell 5.0 PowerShell Installed
[ ] Windows PowerShell 2.0 Engine PowerShell-V2 Removed
[X] Windows PowerShell ISE PowerShell-ISE Installed
To filter your results for PowerShell, pipe your results to the Where-Object cmdlet.
在此示例中,使用 Write-Information
告知用户在运行完当前命令后需要运行另一个命令。
该示例将标记 Instructions 添加到信息性消息。
运行此命令之后,如果在信息流中搜索使用 Instructions 标记的消息,则此处指定的消息会处于结果中。
示例 3:将信息写入文件
function Test-Info
{
Get-Process P*
Write-Information "Here you go"
}
Test-Info 6> Info.txt
在此示例中,使用代码 6> 将函数中的信息流重定向到文件 Info.txt。 打开 Info.txt 文件时,你会看到文本“Here you go”。
参数
-MessageData
指定要在用户运行脚本或命令时向他们显示的信息性消息。 为获得最佳结果,请将信息性消息用引号引起来。 例如“Test complete”。
Type: | Object |
Aliases: | Msg |
Position: | 0 |
Default value: | None |
Required: | True |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Tags
指定一个简单字符串,可用于对已添加到信息流的消息进行排序和筛选。Write-Information
此参数的工作方式类似于 中的 New-ModuleManifest
Tags 参数。
Type: | String[] |
Position: | 1 |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
输入
None
Write-Information
不接受通过管道传递的输入。