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

PowerShell中批量排除文件夹获取产品版本信息的问题

PowerShell多文件夹排除问题解决

在PowerShell中获取产品版本时,需排除多个指定文件夹,目前已实现单个文件夹排除,但多文件夹排除逻辑失效。以下是相关代码及解决方法:

单个文件夹排除(可正常运行)

function VersionInfoTest {
    $Path = 'F:\Shares\Payments*\bin'
    $Exclude1 = "F:\Shares\PaymentsCha*"

    $Results = Get-ChildItem -Path $Path | where FullName -NotLike $Exclude1 | ForEach-Object {
        $Site = $_.fullname.substring(18, 3)
        $Version = dir F:\Shares\Payments$Site\bin\*App.dll | % { $_.VersionInfo.FileVersion }

        [pscustomobject]@{
            "Site ID" = $Site
            "Version" = $Version
        }
    }

    $Results | Format-Table -AutoSize 

    $Results | Export-Csv 'VersionInfoTest.csv' -NoTypeInformation

    $Results | ConvertTo-HTML -Property 'Site ID', 'Version' | Set-Content VersionInfoTest.html
}

多文件夹排除尝试(失效代码)

function VersionInfoTest {  
    $Path = 'F:\Shares\Payments*\bin'
    $Exclude1 = "F:\Shares\PaymentsCha*"
    $Exclude2 = "F:\Shares\PaymentsPor*"
    $Exclude3 = "F:\Shares\PaymentsSma*"
    $Exclude4 = "F:\Shares\PaymentsWCF*"

    $Results = Get-ChildItem -Path $Path | where FullName -NotLike $Exclude1, $Exclude2, $Exclude3, $Exclude4 | ForEach-Object {
        $Site = $_.fullname.substring(18, 3)
        $Version = dir F:\Shares\Payments$Site\bin\*App.dll | % { $_.VersionInfo.FileVersion }

        [pscustomobject]@{
            "Site ID" = $Site
            "Version" = $Version
        }
    }

    $Results | Format-Table -AutoSize 

    $Results | Export-Csv 'VersionInfoTest.csv' -NoTypeInformation

    $Results | ConvertTo-HTML -Property 'Site ID', 'Version' | Set-Content VersionInfoTest.html
}

问题原因及修复方案

问题原因

原多排除代码中,-NotLike后直接传入多个值的逻辑错误:PowerShell中-NotLike面对多个值时,只要当前路径不匹配其中任意一个模式就会返回True,导致本该排除的路径被错误保留。正确逻辑应为当前路径不匹配所有排除模式才保留。

修复方案1:使用数组遍历判断

将所有排除模式存入数组,在Where-Object脚本块中检查当前路径是否不匹配任一排除模式:

function VersionInfoTest {  
    $Path = 'F:\Shares\Payments*\bin'
    # 统一管理排除模式数组
    $ExcludePatterns = @(
        "F:\Shares\PaymentsCha*",
        "F:\Shares\PaymentsPor*",
        "F:\Shares\PaymentsSma*",
        "F:\Shares\PaymentsWCF*"
    )

    $Results = Get-ChildItem -Path $Path | Where-Object {
        $currentPath = $_.FullName
        # 确认当前路径不匹配任何一个排除模式
        -not ($ExcludePatterns | Where-Object { $currentPath -like $_ })
    } | ForEach-Object {
        $Site = $_.FullName.Substring(18, 3)
        $Version = Get-ChildItem "F:\Shares\Payments$Site\bin\*App.dll" | ForEach-Object { $_.VersionInfo.FileVersion }

        [pscustomobject]@{
            "Site ID" = $Site
            "Version" = $Version
        }
    }

    $Results | Format-Table -AutoSize 
    $Results | Export-Csv 'VersionInfoTest.csv' -NoTypeInformation
    $Results | ConvertTo-HTML -Property 'Site ID', 'Version' | Set-Content VersionInfoTest.html
}

修复方案2:使用正则表达式(更简洁)

若排除模式有规律,可将其合并为正则表达式,用-notmatch直接匹配:

function VersionInfoTest {  
    $Path = 'F:\Shares\Payments*\bin'
    # 合并排除模式为正则表达式
    $ExcludeRegex = '^F:\\Shares\\Payments(Cha|Por|Sma|WCF).*'

    $Results = Get-ChildItem -Path $Path | Where-Object { $_.FullName -notmatch $ExcludeRegex } | ForEach-Object {
        $Site = $_.FullName.Substring(18, 3)
        $Version = Get-ChildItem "F:\Shares\Payments$Site\bin\*App.dll" | ForEach-Object { $_.VersionInfo.FileVersion }

        [pscustomobject]@{
            "Site ID" = $Site
            "Version" = $Version
        }
    }

    $Results | Format-Table -AutoSize 
    $Results | Export-Csv 'VersionInfoTest.csv' -NoTypeInformation
    $Results | ConvertTo-HTML -Property 'Site ID', 'Version' | Set-Content VersionInfoTest.html
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 02:57:19