Powershell选择对象:在计算属性中执行本机命令
我正在尝试创建一个报告文件,其中列出我的Git存储库中的所有文件,包括以下列:Powershell选择对象:在计算属性中执行本机命令,powershell,Powershell,我正在尝试创建一个报告文件,其中列出我的Git存储库中的所有文件,包括以下列: 名字 大小 最后修改 Git提交 前三个是没有问题的: gci -File -Recurse | Select-Object -Property @{ label = 'Size(KB)' expr = { [string]::Format("{0:0.00}", $_.Length/1KB) } }, LastWriteTime, FullName 但是,检索git提交需要运行git
- 名字
- 大小
- 最后修改
- Git提交
gci -File -Recurse | Select-Object -Property @{
label = 'Size(KB)'
expr = { [string]::Format("{0:0.00}", $_.Length/1KB) }
}, LastWriteTime, FullName
但是,检索git提交需要运行git,这是一个本机命令
我试过,除其他外:
gci -File -Recurse | Select-Object -Property @{
label = 'Git commit'
expr = { Invoke-Expression "git log -1 --format:format=`"%s`" -- $($_.FullName)" }
},
@{
label = 'Size(KB)'
expr = { [string]::Format("{0:0.00}", $_.Length/1KB) }
}, LastWriteTime, FullName
但它只是被卡住了
有人知道怎么做吗
附言。
Git的所有标志和选项都无关紧要,我只是复制了我已经做过的事情。正如@mklement0在评论中所建议的,问题只是您的
--format
命令的格式设置关闭了,足以引起问题
你有:
--format:format="%s" # won't work
--format=format:"%s" # works
所以为了修复它,我们只需以正确的格式交换,给我们这个命令和下面的输出
gci -File | Select-Object -Property @{
label = 'Git commit'
expr = { Invoke-Expression "git log -1 --format=format:`"%s`" $($_.Name)" }
},
@{
label = 'Size(KB)'
expr = { [string]::Format("{0:0.00}", $_.Length/1KB) }
}, LastWriteTime, FullName
Git commit Size(KB) LastWriteTime FullName
---------- -------- ------------- --------
applied gitingore 6.07 5/11/2020 11:22:06 AM C:\git\ClientFaux\.gitignore
cleanin up layout 1.40 10/9/2020 3:20:33 PM C:\git\ClientFaux\ClientFaux.sln
Create LICENSE (#25) 34.98 7/13/2020 9:55:00 AM C:\git\ClientFaux\LICENSE
Update README.md (#27) 3.37 7/13/2020 9:55:00 AM C:\git\ClientFaux\README.md
31.13 7/13/2020 9:55:27 AM C:\git\ClientFaux\UpgradeLog.htm
正如@mklement0在评论中所建议的,问题在于
--format
命令的格式设置已关闭,足以引起问题
你有:
--format:format="%s" # won't work
--format=format:"%s" # works
所以为了修复它,我们只需以正确的格式交换,给我们这个命令和下面的输出
gci -File | Select-Object -Property @{
label = 'Git commit'
expr = { Invoke-Expression "git log -1 --format=format:`"%s`" $($_.Name)" }
},
@{
label = 'Size(KB)'
expr = { [string]::Format("{0:0.00}", $_.Length/1KB) }
}, LastWriteTime, FullName
Git commit Size(KB) LastWriteTime FullName
---------- -------- ------------- --------
applied gitingore 6.07 5/11/2020 11:22:06 AM C:\git\ClientFaux\.gitignore
cleanin up layout 1.40 10/9/2020 3:20:33 PM C:\git\ClientFaux\ClientFaux.sln
Create LICENSE (#25) 34.98 7/13/2020 9:55:00 AM C:\git\ClientFaux\LICENSE
Update README.md (#27) 3.37 7/13/2020 9:55:00 AM C:\git\ClientFaux\README.md
31.13 7/13/2020 9:55:27 AM C:\git\ClientFaux\UpgradeLog.htm
包含关键指针,但让我为您的代码提供一个(已更正的)PowerShell惯用格式
Get-ChildItem -File -Recurse | Select-Object -Property @{
label = 'Git commit'
expr = { git log -1 --format=format:%s -- $_.FullName }
},
@{
label = 'Size(KB)'
expr = { '{0:0.00}' -f ($_.Length/1KB) }
}, LastWriteTime, FullName
注:
- ;显然,因此上面使用了直接调用
git
- 如前所述,
在语法上不正确,应该是--format:format=“%s”
(我省略了上面的--format=format:“%s”
,PowerShell无论如何都会在幕后删除它)”
,作为直接调用底层.NET API的PowerShell惯用替代方法-f
调用计算属性中的本机可执行文件: 一般来说,请注意,从 具体而言,以下注意事项适用:
- 在PowerShell中,本机可执行文件的标准输出总是转换为文本(
实例)。如果输出包含多行,则属性值将成为包含字符串的(常规[string]
)数组[object[]]
- 如果本机可执行文件不生成标准输出,则属性的值为“nothing”,即所谓的“Automation Null”值(
),在大多数上下文中的行为类似于[System.Management.Automation.Internal.AutomationNull]::value
)$Null
- 如果本机可执行文件不生成标准输出,则属性的值为“nothing”,即所谓的“Automation Null”值(
- 来自本机可执行文件的任何stderr输出都会被悄悄地丢弃。(类似地,
脚本块中PowerShell命令或表达式的错误也会被悄悄地忽略。)expression
表达式
脚本块进行故障排除,请通过命令(其内置别名为%
)独立执行,以显示任何错误;例如:
# Uses broken `git` syntax; note the resulting error message.
PS> (Get-ChildItem -File)[0] | % { git log -1 --format:format=%s -- $_.FullName }
fatal: unrecognized argument: --format:format=%s
至于你所尝试的: 由于您的
git
命令在语法上不正确,git
只生成stderr输出,PowerShell随后将忽略该输出,如上所述
因此,您的Git commit
属性最终包含“nothing”,这只会在输出格式中显示为空。包含关键指针,但让我提供一个(更正的)PowerShell代码惯用的重新格式
Get-ChildItem -File -Recurse | Select-Object -Property @{
label = 'Git commit'
expr = { git log -1 --format=format:%s -- $_.FullName }
},
@{
label = 'Size(KB)'
expr = { '{0:0.00}' -f ($_.Length/1KB) }
}, LastWriteTime, FullName
注:
- ;肯定是,因此上面使用了直接调用
git
- 如前所述,
在语法上不正确,应该是--format:format=“%s”
(我省略了上面的--format=format:“%s”
,PowerShell无论如何都会在幕后删除它)”
,作为直接调用底层.NET API的PowerShell惯用替代方法-f
调用计算属性中的本机可执行文件: 一般来说,请注意,从 具体而言,以下注意事项适用:
- 在PowerShell中,本机可执行文件的标准输出总是转换为文本(
实例)。如果输出包含多行,则属性值将成为包含字符串的(常规[string]
)数组[object[]]
- 如果本机可执行文件不生成标准输出,则属性的值为“nothing”,即所谓的“Automation Null”值(
),在大多数上下文中的行为类似于[System.Management.Automation.Internal.AutomationNull]::value
)$Null
- 如果本机可执行文件不生成标准输出,则属性的值为“nothing”,即所谓的“Automation Null”值(
- 来自本机可执行文件的任何stderr输出都会被悄悄地丢弃。(类似地,
脚本块中PowerShell命令或表达式的错误也会被悄悄忽略。)expression
表达式
脚本块进行故障排除,请通过命令(其内置别名为%
)独立执行,以显示任何错误;e、 g:
# Uses broken `git` syntax; note the resulting error message.
PS> (Get-ChildItem -File)[0] | % { git log -1 --format:format=%s -- $_.FullName }
fatal: unrecognized argument: --format:format=%s
至于你所尝试的: 由于您的
git
命令在语法上不正确,git
只生成stderr输出,PowerShell随后将忽略该输出,如上所述
因此,您的Git commit
属性最终包含“nothing”,这只是在输出格式中呈现为空白;当然可以。选项--format:format=“%s”
看起来不对;它应该是--format=format:“%s”
。(PowerShell将剥离“
,但这不会有什么区别。)