You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell当前目录下通配符无法匹配.nupkg文件问题排查

解决PowerShell中Get-ChildItem不用-Recurse匹配当前目录.nupkg文件的问题

这个问题我之前也碰到过,其实是PowerShell里Get-ChildItem的-Include参数的行为特性导致的。

为什么Get-ChildItem -Include *.nupkg找不到文件?

当你不使用-Recurse参数时,-Include是作用于-Path参数指定的路径的通配符匹配逻辑。默认情况下-Path的值是.(当前目录),这时候-Include *.nupkg会尝试匹配当前目录的子目录中符合.nupkg后缀的项,而不是当前目录本身的文件。这就是为什么明明文件在当前目录,却返回空结果的原因。

几种无需-Recurse的解决方案

1. 直接使用位置参数指定通配符(最简单的写法)

直接把通配符作为Get-ChildItem的位置参数,它会直接匹配当前目录下的目标文件:

Get-ChildItem *.nupkg | Foreach { nuget add $_.FullName -Source c:\localNuget }

2. 结合-Path *和-Include参数

如果坚持要用-Include,可以显式指定-Path *,让-Include过滤当前目录下的所有项:

Get-ChildItem -Path * -Include *.nupkg | Foreach { nuget add $_.FullName -Source c:\localNuget }

3. 使用-Filter参数(性能更优)

-Filter是文件系统层面的过滤,比-Include的PowerShell层面过滤性能更好,尤其是当目录内容较多时:

Get-ChildItem -Filter *.nupkg | Foreach { nuget add $_.FullName -Source c:\localNuget }

额外优化建议

你还可以把两个步骤合并成一个脚本,避免生成文件后再单独查找,比如:

# 递归查找nuspec并打包,同时收集生成的nupkg文件路径
$nupkgFiles = Get-ChildItem -Include *.nuspec -Recurse | Foreach {
    nuget pack $_.FullName
    # 返回生成的nupkg文件对象
    Get-Item (Join-Path $_.Directory.FullName "$($_.BaseName).nupkg")
}

# 上传所有生成的nupkg到本地源
$nupkgFiles | Foreach { nuget add $_.FullName -Source c:\localNuget }

这样不需要单独查找当前目录的nupkg,直接在打包后获取文件对象,更可靠也更高效。

内容的提问来源于stack exchange,提问作者Bradley Uffner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:12:40