Write-Information
指定 Windows PowerShell 如何处理命令的信息流数据。
语法
Write-Information
[-MessageData] <Object>
[[-Tags] <String[]>]
[<CommonParameters>]
说明
Write-Information
cmdlet 指定 Windows PowerShell 如何处理命令的信息流数据。
Windows PowerShell 5.0 引入了新的结构化信息流(Windows PowerShell 流中的数字 6),可用于在脚本及其调用方(或托管环境)之间传输结构化数据。
Write-Information
允许向流添加信息性消息,并指定 Windows PowerShell 如何处理命令的信息流数据。 信息流也适用于 PowerShell.Streams
、作业、计划作业和工作流。
注意
信息流不遵循使用“[流名称]:”作为消息前缀的标准约定。 这是为了简洁和视觉清洁。
$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
命令后显示信息性消息“获取功能!”,以查找名称值以“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
让用户知道在运行当前命令后,他们需要运行另一个命令。
该示例将标记“说明”添加到信息性消息。
运行此命令后,如果在信息流中搜索标记为“说明”的消息,此处指定的消息将是结果之一。
示例 3:将信息写入文件
function Test-Info
{
Get-Process P*
Write-Information "Here you go"
}
Test-Info 6> Info.txt
在此示例中,使用代码 6>将函数中的信息流重定向到文件,Info.txt。 打开 Info.txt 文件时,会看到文本“转到此处”。
参数
-MessageData
指定要在用户运行脚本或命令时向用户显示的信息性消息。 为获得最佳结果,请将信息性消息括在引号中。 例如“测试完成”。
类型: | Object |
别名: | Msg |
Position: | 0 |
默认值: | None |
必需: | True |
接受管道输入: | False |
接受通配符: | False |
-Tags
指定一个简单的字符串,可用于使用 Write-Information
对已添加到信息流的消息进行排序和筛选。
此参数的工作方式类似于 New-ModuleManifest
中的 Tags 参数。
类型: | String[] |
Position: | 1 |
默认值: | None |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
输入
None
Write-Information
不接受管道输入。