【问题标题】:Why does Get-content support -wait parameter?为什么 Get-content 支持 -wait 参数?
【发布时间】:2014-06-04 10:03:26
【问题描述】:

在 Powershell V2 中通过 Get-content 的帮助时,我找不到 -Wait 参数。虽然我可以将此参数与相同的 cmdlet 一起使用。这肯定不是 commonParameters 之一。

NAME
    Get-Content

SYNOPSIS
    Gets the content of the item at the specified location.

SYNTAX
    Get-Content [-LiteralPath] <string[]> [-Credential <PSCredential>] [-Exclude <string[]>] [-Filter <string>] [-Force
    ] [-Include <string[]>] [-ReadCount <Int64>] [-TotalCount <Int64>] [-UseTransaction] [<CommonParameters>]

    Get-Content [-Path] <string[]> [-Credential <PSCredential>] [-Exclude <string[]>] [-Filter <string>] [-Force] [-Inc
    lude <string[]>] [-ReadCount <Int64>] [-TotalCount <Int64>] [-UseTransaction] [<CommonParameters>]

我能找到的唯一命令行开关在 Powershell V2 中明确提到 -Wait 参数是 Start-process

get-content 是否在后台调用 Start-process?如何将 -Wait 参数与 Get-content 一起使用?

这可能有助于发现帮助中未提及但可与该 cmdlet 一起使用的其他参数。 提前感谢您的帮助!

【问题讨论】:

  • 它有它的用途,请在此处查看 Ed 的文章。 blogs.technet.com/b/heyscriptingguy/archive/2013/02/24/…
  • 艾德简直太棒了!虽然问题是关于为什么我能够使用等待,但它没有记录在帮助中。如果还有其他参数在帮助中没有提到,但可以使用。

标签: powershell parameters powershell-2.0


【解决方案1】:

看起来 'raw' 和 'wait' 都是动态参数。而且,“等待”正在后台使用 FileSystemWatcher。它等待更改的默认时间似乎是 500 毫秒。

一次,超时;线程再休眠 100 毫秒,然后寻找流的开头并丢弃缓冲区。

参考: 在阅读了 PowerShell MVP Oisin Grehan 的博客“在 Reflector 中直接跳转到 Cmdlet 的实现的技巧”'Reflect-Cmdlet' 后,我明白了这一点。

另外,请阅读以下问题:can we see the source code for PowerShell cmdlets

【讨论】:

  • 这些是一些非常有用的链接:)
【解决方案2】:

此开关的文档已添加到更高版本的 PowerShell 中。这是来自 PS 4.0。

PS > Get-Help Get-Content -Parameter wait

-Wait [<SwitchParameter>]
    Waits for the cmdlet to get the content before returning the command prompt. While waiting, Get-Content checks the
    file once each second until you interrupt it, such as by pressing CTRL+C.

    Wait is a dynamic parameter that the FileSystem provider adds to the Get-Content cmdlet. This parameter works only
    in file system drives.

    Required?                    false
    Position?                    named
    Default value                False
    Accept pipeline input?       false
    Accept wildcard characters?  false

【讨论】:

    【解决方案3】:

    正如@sqlchow 所指出的,-wait 是 get-content 的动态参数,并且只能根据可用的文档Here 与文件系统提供程序一起使用。
    即,如果您在注册表提供程序中,则不能将此参数与 get-content 一起使用(至少在 Powershell V2 中)

    PS C:\> cd HKLM:\
    PS HKLM:\> Get-Content -Wait
    Get-Content : A parameter cannot be found that matches parameter name 'Wait'.
    

    我们可以看到注册表提供程序无法等待。

    Ed Wilson 在How to find Dynamic Paramters 上有一篇不错的博文

    【讨论】:

    • 文件系统限制在我提供的文档中非常清楚,但是关于动态参数的链接很好。 :)
    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 2018-10-11
    • 2023-03-27
    • 2011-03-21
    • 1970-01-01
    相关资源
    最近更新 更多