为什么获取内容支持-等待参数



在Powershell V2中查看Get内容的帮助时,找不到-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

获取内容调用是否在后台启动进程?如何将-Wait参数与Get content一起使用?

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

此开关的文档已添加到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

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

有一次,它超时了;线程再休眠100ms,然后寻找流的开始并丢弃缓冲区。

参考:在阅读PowerShell MVP Oisin Grehan的博客"在Reflector中直接跳转到Cmdlet实现的技巧"、"Reflect Cmdlet"后,我们发现了这一点。

此外,请阅读以下问题:我们可以查看PowerShell cmdlet 的源代码吗

正如@sqlchow指出的那样-wait是获取内容的动态参数,根据此处提供的文档,只能与文件系统提供程序一起使用
即,如果您在注册表提供商中,则不能将此参数用于获取内容(至少在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有一篇关于如何找到动态参数的博客文章

最新更新