当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Write-Information (Microsoft.PowerShell.Utility)

作者:精品下载站 日期:2024-12-14 02:02:23 浏览:11 分类:玩电脑

Write-Information (Microsoft.PowerShell.Utility)


Write-Information

模块 :Microsoft.PowerShell.Utility

指定 PowerShell 如何处理命令的信息流数据。

句法

Write-Information
     [-MessageData] <Object>
     [[-Tags] <String[]>]
     [<CommonParameters>]

描述

Write-Information cmdlet 指定 PowerShell 如何处理命令的信息流数据。

Windows PowerShell 5.0 引入了新的结构化信息流。您可以使用此流在脚本及其调用者或主机应用程序之间传输结构化数据。 Write-Information 允许您将信息性消息添加到流中,并指定 PowerShell 如何处理命令的信息流数据。信息流还适用于 PowerShell.Streams、作业和计划任务。

笔记

信息流不遵循在其消息中添加“[流名称]:”前缀的标准约定。这是为了简洁和视觉简洁。

$InformationPreference 首选项变量值确定您提供给 Write-Information 的消息是否显示在脚本操作中的预期点。由于此变量的默认值为SilentlyContinue,因此默认情况下不会显示信息性消息。如果您不想更改 $InformationPreference 的值,可以通过向命令添加 InformationAction 通用参数来覆盖其值。有关详细信息,请参阅 about_Preference_Variables 和 about_CommonParameters。

笔记

从 Windows PowerShell 5.0 开始,Write-HostWrite-Information 的包装器,这允许您使用 Write-Host 将输出发送到信息流。这使得能够捕获或抑制使用Write-Host写入的数据,同时保持向后兼容性。有关详细信息,请参阅写入主机

示例

示例1:为Get-results写入信息

在此示例中,您将显示一条信息性消息“以 'P' 开头的进程”,然后运行 Get-Process 命令来查找具有以下 Name 值的所有进程:以“p”开头。由于 $InformationPreference 变量仍设置为其默认值 SilentlyContinue,因此您添加 InformationAction 参数来覆盖 $InformationPreference value,并显示消息。 InformationAction 值为继续,这意味着将显示您的消息,但脚本或命令将继续(如果尚未完成)。

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 文件时,您会看到文本“Here you go”。

function Test-Info
{
    Get-Process P*
    Write-Information "Here you go"
}
Test-Info 6> Info.txt

示例4:传递对象写入信息

在此示例中,您可以使用 Write-Information 从经过多个管道的 Get-Process 对象输出中写入 CPU 使用率最高的前 10 个进程。

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

指定要在用户运行脚本或命令时向用户显示的信息性消息。为了获得最佳结果,请将信息消息用引号引起来。

类型 :

目的

别名:

Msg, Message

位置:

0

默认值:

None

必需的:

True

接受管道输入:

True

接受通配符:

False

-Tags

您可以使用一个或多个字符串对通过Write-Information 添加到信息流的消息进行排序和过滤。此参数的工作方式与 New-ModuleManifest 中的 Tags 参数类似。

类型 :

String[]

位置:

1

默认值:

None

必需的:

False

接受管道输入:

False

接受通配符:

False

输入

对象

您可以通过管道将要传递到信息流的对象传递给此 cmdlet。

输出

此 cmdlet 不返回任何输出。它仅写入信息消息流。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯