Write-Information
指定 PowerShell 如何处理命令的信息流数据。
语法
Write-Information
[-MessageData] <Object>
[[-Tags] <String[]>]
[<CommonParameters>]
说明
cmdlet Write-Information
指定 PowerShell 如何处理命令的信息流数据。
Windows PowerShell 5.0 引入了新的结构化信息流。 可以使用此流在脚本与其调用方或主机应用程序之间传输结构化数据。
Write-Information
允许向流添加信息性消息,并指定 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
示例
示例 1:为 Get- results 写入信息
在此示例中,在运行 Get-Process
命令以查找 Name 值以“p”开头的所有进程之前,将显示一条信息性消息“进程”。
由于变量 $InformationPreference
仍设置为其默认值 SilentlyContinue,因此添加 InformationAction 参数以替代 $InformationPreference
值并显示消息。 InformationAction 值为 Continue,这意味着显示消息,但脚本或命令会继续(如果尚未完成)。
Write-Information -MessageData "Processes starting with 'P'" -InformationAction Continue
Get-Process -Name p*
Processes starting with 'P'
18 19.76 15.16 0.00 6232 0 PFERemediation
20 8.92 25.15 0.00 24944 0 policyHost
9 1.77 7.64 0.00 1780 0 powercfg
10 26.67 32.18 0.00 7028 0 powercfg
8 26.55 31.59 0.00 13600 0 powercfg
9 1.66 7.55 0.00 22620 0 powercfg
21 6.17 4.54 202.20 12536 1 PowerMgr
42 84.26 12.71 2,488.84 20588 1 powershell
27 47.07 45.38 2.05 25988 1 powershell
27 24.45 5.31 0.00 12364 0 PresentationFontCache
92 112.04 13.36 82.30 13176 1 pwsh
106 163.73 93.21 302.25 14620 1 pwsh
227 764.01 92.16 1,757.22 25328 1 pwsh
示例 2:写入信息并进行标记
在此示例中,使用 Write-Information
告知用户在运行完当前命令后需要运行另一个命令。 该示例将 标记 "Instructions"
添加到信息性消息中。 运行此命令后,在信息流中搜索标记 "Instructions"
的消息时,消息位于结果中。
$message = "To filter your results for PowerShell, pipe your results to the Where-Object cmdlet."
Get-Process -Name p*
Write-Information -MessageData $message -Tags "Instructions" -InformationAction Continue
NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName
------ ----- ----- ------ -- -- -----------
18 19.76 15.16 0.00 6232 0 PFERemediation
20 8.92 25.15 0.00 24944 0 policyHost
9 1.77 7.64 0.00 1780 0 powercfg
10 26.67 32.18 0.00 7028 0 powercfg
8 26.55 31.59 0.00 13600 0 powercfg
9 1.66 7.55 0.00 22620 0 powercfg
21 6.17 4.54 202.20 12536 1 PowerMgr
42 84.26 12.71 2,488.84 20588 1 powershell
27 47.07 45.38 2.05 25988 1 powershell
27 24.45 5.31 0.00 12364 0 PresentationFontCache
92 112.04 13.36 82.30 13176 1 pwsh
106 163.73 93.21 302.25 14620 1 pwsh
227 764.01 92.16 1,757.22 25328 1 pwsh
To filter your results for PowerShell, pipe your results to the Where-Object cmdlet.
示例 3:将信息写入文件
在此示例中,使用代码 6>
将 函数中的信息流重定向到 Info.txt
。 打开 Info.txt
文件时,会看到文本“你现在去”。
function Test-Info
{
Get-Process P*
Write-Information "Here you go"
}
Test-Info 6> Info.txt
示例 4:传递对象以写入信息
在此示例中,可以使用 Write-Information
从 Get-Process
通过多个管道的对象输出写入前 10 个 CPU 使用率最高的进程。
Get-Process | Sort-Object CPU -Descending |
Select-Object Id, ProcessName, CPU -First 10 |
Write-Information -InformationAction Continue
@{Id=12692; ProcessName=chrome; CPU=39431.296875}
@{Id=21292; ProcessName=OUTLOOK; CPU=23991.875}
@{Id=10548; ProcessName=CefSharp.BrowserSubprocess; CPU=20546.203125}
@{Id=312848; ProcessName=Taskmgr; CPU=13173.1875}
@{Id=10848; ProcessName=SnapClient; CPU=7014.265625}
@{Id=9760; ProcessName=Receiver; CPU=6792.359375}
@{Id=12040; ProcessName=Teams; CPU=5605.578125}
@{Id=498388; ProcessName=chrome; CPU=3062.453125}
@{Id=6900; ProcessName=chrome; CPU=2546.9375}
@{Id=9044; ProcessName=explorer; CPU=2358.765625}
示例 5:将信息记录保存到变量
使用 InformationVariable 参数,可以将信息记录保存到变量中。 这允许稍后在脚本中检查信息流消息。
Get-Process -Id $PID |
Select-Object ProcessName, CPU, Path |
Write-Information -Tags 'PowerShell' -InformationVariable 'InfoMsg'
$InfoMsg | Select-Object *
MessageData : @{ProcessName=pwsh; CPU=12.36; Path=/opt/microsoft/powershell/7/pwsh}
Source : Write-Information
TimeGenerated : 10/19/2023 11:28:15
Tags : {PowerShell}
User : sdwheeler
Computer : circumflex
ProcessId : 237
NativeThreadId : 261
ManagedThreadId : 10
参数
-MessageData
指定要在用户运行脚本或命令时向他们显示的信息性消息。 为获得最佳结果,请将信息性消息用引号引起来。
Type: | Object |
Aliases: | Msg, Message |
Position: | 0 |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
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 |
输入
可以通过管道将对象传递给信息流到此 cmdlet。
输出
None
此 cmdlet 不返回任何输出。 它只写入信息消息流。