为什么“Get-ChildItem -File | Get-FileHash”有效?

2024-05-06

我对 Bash 比对 Powershell 更熟悉,有时我对后者的对象模型感到困惑。

看着Get-FileHash 的文档 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-filehash?view=powershell-7,似乎有3种指定输入的方式:

  • 获取文件哈希 [-路径]
  • 获取文件哈希 [-LiteralPath]
  • 获取文件哈希 [-InputStream]

前两个是文件名,第三个是数据流。

Now, Get-ChildItem -File https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7似乎输出System.IO.FileInfo https://learn.microsoft.com/en-us/dotnet/api/system.io.fileinfo?view=netframework-4.8物体,从什么来判断Get-Member https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-member?view=powershell-7 says:

$ Get-ChildItem -File | Get-Member
TypeName: System.IO.FileInfo

然而管道Get-ChildItem -File | Get-FileHash工作正常。我的问题是,允许转换的机制是什么System.IO.FileInfo预期的输入类型Get-FileHash?


The System.IO.FileInfo https://docs.microsoft.com/en-US/dotnet/api/System.IO.FileInfo / System.IO.DirectoryInfo https://docs.microsoft.com/en-US/dotnet/api/System.IO.DirectoryInfo instances output by PowerShell cmdlets have a .PSPath property[*] that contains the instances' fully qualified path, which is the full file-system path prefixed by the PS provider name (e.g., Microsoft.PowerShell.Core\FileSystem::C:\windows).

文件处理 cmdlets例如Get-FileHash have a -LiteralPath范围其别名为-PSPath.

因为一个-LiteralPath参数(典型值)接受来自管道的输入按属性名称,输入对象具有.PSPath属性自动绑定到它,凭借PSPath参数别名。

作为旁白:

  • 文件处理 cmdlet 还具有-Path参数,它将其参数解释为wildcard表达式 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Wildcards,而不是字面路径。

    • 当你管道路径strings对于此类 cmdlet,它们绑定到-Path,这特别意味着它们确实被解释为通配符 - 虽然这通常并不重要,因为大多数文字路径不包含通配符元字符,但对于包含[ and ],然后被误解;避免这种误解需要将它们转义为`[ and `],如图所示这个答案 https://stackoverflow.com/a/70413617/45375.
  • 由于错误Windows PowerShell(自从固定在PowerShell(核心)7+), Get-FileHash具体来说,不接受字符串通过管道 - see 这个答案 https://stackoverflow.com/a/69922416/45375了解详情。


如何发现这种行为:

  • Via the 在线帮助主题 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-filehash?view=powershell-7:
  • 以编程方式:

    • Note: Get-Help Get-FileHash -Parameter LiteralPath | Select-Object name, aliases, pipelineinput也适用于this情况,但这种方法通常仅限于附带的目标命令MAML https://en.wikipedia.org/wiki/Microsoft_Assistance_Markup_Language基于帮助文件,甚至那些有的帮助文件也可以是不同步与实际的命令定义。
& {
  Get-Command $args[0] | % Parameters | % $args[1] |
  Select-Object Name, Aliases, @{
    n = 'Accepts pipeline input';
    e = { $(if ($_.Attributes.ValueFromPipeline) { 'by value' }), $(if ($_.Attributes.ValueFromPipelineByPropertyName) { 'by property name' }) -join ', ' -replace '^, ' }
  }
} Get-FileHash LiteralPath

Output:

Name        Aliases      Accepts pipeline input
----        -------      ----------------------
LiteralPath {PSPath, LP} by property name

[*] It is PowerShell's file-system provider that adds this property, among others. All PowerShell providers https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Providers decorate their output items this way, such as the Microsoft.Win32.RegistryKey instances output by the registry provider. The underlying .NET types do not have it. See this answer https://stackoverflow.com/a/55881978/45375 for more information.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么“Get-ChildItem -File | Get-FileHash”有效? 的相关文章

  • Powershell Core 6 中的 HtmlWebResponseObject.ParsedHtml 替换

    我的目标是解析检索到的 html 文件Invoke WebRequest 如果可能的话 我想避免任何外部库 我面临的问题是Invoke WebRequest返回一个BasicHtmlWebResponseObject代替HtmlWebRes
  • Powershell 调用程序集委托

    我有一个用于过程控制应用程序的 dll 程序集 我在我的内部使用 load电源外壳 script 该DLL包含我需要使用的委托类型 委托名称是 X Y Delegate 我在该 DLL 中有另一个方法 应该这样调用 Method deleg
  • Powershell脚本命令持久化

    我开始学习 Powershell 并编写一个模块 psm1 来存储我的函数 然后我将这段代码插入到模块中 以便在修改模块时重新加载模块 function reload Remove Module init Import Module F S
  • 自动递增 EC2 实例名称

    我在 Stackoverflow 上看到很多问题 询问是否有办法自动递增实例名称 例如 foo1 foo2 fooN 我想看看是否有办法在 Powershell 中执行此操作 我正在使用 AutoLaunchConfiguration AS
  • 从 C# 调用时无法识别 Powershell 命令

    这是这个的延续Question https stackoverflow com questions 66280000 powershell object returns null 66280138 noredirect 1 comment1
  • PowerShell 和 StringBuilder

    我是 PowerShell 新手 但熟悉 NET 类 我在用System Text StringBuilder在 PowerShell 脚本中 脚本是这样的 Function MyStringFunc String line r New O
  • 如何扩展和重写集合类中的 Add

    背景 我在 PowerShell 中有一个具有 4 个属性的数据对象 其中 3 个是字符串 第 4 个是哈希表 我想安排一个新类型 将其定义为该数据对象的集合 在这个集合类中 我希望强制执行一种特定的格式 这将使我在模块中其他地方的代码更加
  • 将 Metro 应用程序固定到任务栏 Windows 10 Powershell

    以下代码将固定 Metro 应用程序以在给定 AUMID 的情况下启动 如果你改变 match Pin To Start 不幸的是 将匹配更改为 固定到任务栏 不起作用 这里发生了什么 function Pin Taskbar param
  • 通过将密码与命令一起传递,使用 powerShell 脚本进行 ssh

    如果我输入 我可以从 PowerShell ssh 到服务器 ssh 用户名 主机 这会提示输入密码 我输入有效的密码 但我试图编写一个脚本 通过 ssh 进入服务器执行一些脚本并返回 所以我必须传递密码和命令我该怎么做 关于 shell
  • FileInfo.BaseName 存在于 PowerShell 中,但不存在于直接 .NET 中

    为什么在 NET 中System IO FileInfo对象没有BaseName属性 但我可以通过 PowerShell 使用该属性 例如 FolderItems Get ChildItem Path C Where Object isno
  • 在powershell的列中处理带有换行符的CSV

    目前 我有一个系统 它创建一个像下面这样的分隔文件 在其中我模拟了偶尔出现在列中的额外换行符 列 1 列 2 列 3 列 4 文本1 文本2 LF 文本3 LF 文本4 CR LF 文本1 文本2 LF LF 文本3 文本4 CR LF 文
  • Powershell日期类型无法找到

    我正在尝试使用PowerShell连接virustotal API 代码来自virustotal网站 我得到 无法找到类型 System Security Cryptography ProtectedData 错误信息 代码如下 funct
  • Powershell:复制时自动更改文件属性

    我有一个只读文件的文件夹 当我将这些文件之一复制到测试位置时 我希望能够复制它们而不保留其原始属性 我可以做到这一点 但这很长 copy item srcfilefullname destfilefullname set itemprope
  • 导入模块 WebAdministration 不会从脚本加载,但会从命令行加载

    我正在进行一个使用的项目PowerShell编写构建脚本 该构建利用了WebAdministration模块来管理本地 IIS 实例 当我运行构建脚本时 尝试导入时会引发以下错误WebAdministration 错误 06 29 2016
  • 有没有办法在 MS Windows(Powershell 或 CMD)的 ripgrep 中转义引号?

    我想找一个字符串 Hello Hello 以双引号开头 在文本文件中使用ripgrep 通常 在 Bash 或 ZSH 中 这可以通过用反斜杠转义或用单引号括起来来实现 rg Hello rg Hello 然而 在 MS Windows P
  • 通过powershell运行ADB命令

    所以我尝试通过 powershell 脚本运行一些 ADB 命令 这是我正在尝试做的一个简单示例 adb shell echo in adb shell su root echo you are now root ls cd data da
  • 远程计算机上的活动登录用户

    我正在使用下面的脚本来获取远程计算机上的登录用户 它工作正常 但我需要让用户处于 活动 状态 如何获取远程计算机上的活动登录用户 function Global Get LoggedOnUser Requires Version 2 0 C
  • 动态参数值取决于另一个动态参数值

    启动前提 非常严格的环境 Windows 7 SP1 Powershell 3 0 使用外部库的可能性有限或不可能 我正在尝试重写之前创建的 bash 工具 这次使用 PowerShell 在 bash 中 我实现了自动完成功能 以使该工具
  • 如何将目录及其子目录中的所有 PDF 文件复制到一个位置?

    如何全部复制PDF文件从目录及其子目录到单个目录 实际上还有更多的文件 并且深度有些任意 假设四个目录的最大深度是公平的 我想这些文件需要重命名 如果a pdf例如 位于多个目录中 因为我会adding https ebooks stack
  • Powershell 将单个字符串与多个正则表达式匹配?

    除了依次迭代每个正则表达式之外 是否有一种更 powershelly 的方式将单个字符串与正则表达式的数组 集合进行匹配 我真正想做的是这样的 database Name match includeRegexArray 考虑到 Powers

随机推荐