【问题标题】:make get-Childitem stop if a file is found如果找到文件,则停止 get-Childitem
【发布时间】:2020-09-28 20:57:04
【问题描述】:

我有这个代码:

    $Paths = 'C:\' , 'P:\' , "\\fril01\ufr$\$env:username"

    $torEXE = Get-childitem -path $paths -recurse -Exclude $ExcludePaths -erroraction 'silentlycontinue'  | where-object {$_.name -eq "Tor.exe"}

    if ($torEXE.Exists) {$answer = 1}

检查文件 tor.exe,但正如您所见,此检查可能需要一些时间。检查可能会在前几秒钟找到tor.exe,但会继续检查所有路径。我希望它在找到 tor.exe 后立即停止,而不是继续搜索它。 怎么办?

【问题讨论】:

    标签: windows powershell directory get-childitem


    【解决方案1】:

    在第一个$N 对象到达Select-Object 后,将|Select-Object -First $N 粘贴到管道末尾的make it stop executing

    $torEXE = Get-ChildItem -Path $paths -Recurse -Exclude $ExcludePaths -ErrorAction 'silentlycontinue'  | Where-Object {$_.Name -eq "Tor.exe"} |Select -First 1
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-28
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    相关资源
    最近更新 更多