【问题标题】:Powershell: Count files recursively while ignoring specific directoriesPowershell:在忽略特定目录的同时递归地计算文件
【发布时间】:2016-02-15 09:46:03
【问题描述】:

我目前正在尝试为某些文件夹中包含超过 50 个文件时设置警报。文件夹目录是这样的:

C:\Application\CustomerID\oneDirectory\RegisterName\transactions\FilesToCount

对于oneDirectory,有多个我不关心 - 我只关心oneDirectory。还有多个我关心的RegisterNames。对于transactions,还有多个我不关心的其他目录。对于CustomerID,我确实关心多个其他目录(客户)

基本上,我需要为每个客户的每个寄存器计算transactions 文件夹中的文件数,同时忽略所有其他文件夹。如果任何一个客户有超过 50 个左右的文件,我需要收到一封电子邮件告诉我。我还需要忽略.ini 文件。

现在,我有以下脚本可以正常工作并执行上面所需的功能,但运行时间太长,我不知道为什么:

$mainFolder = "C:\Application\Customer."
$tenantID = "CustomerID"
$maxTenantID = "HighestCustomerID"
$registerPath = "\oneDirectory\"

while($tenantID -le $maxTenantID) {

    $loc = $mainFolder + $tenantID + $registerPath

    $ignore = @("failed_transactions", "saved_transactions", "temp", "offline_logs", "pos_logs", "pos_reports")

    $files = Get-ChildItem -Path $loc -Recurse  -Exclude *.ini | ? { !$_.PSIsContainer } | % {

        $relative = $_.FullName.Replace($loc, "")
        $nomatch = $true

        foreach ($folder in $ignore) {
            if($relative -like "*\$folder\*") {
                $nomatch = $false
            }
        }

        if ($nomatch) { $_ }
    }

    $currentCount = $files.Count    
    if($currentCount -gt 50) {
        Send-MailMessage -From "email@whatever.com" -To "email@whatever.com" -SmtpServer "smtp.whatever.com" -Subject "Transactions stuck" -Body "$loc has over 50 transactions."
    }

    $tenantID++
}

如前所述,上面的代码可以正确完成工作,但我正在尝试设置一个计划任务以每小时运行一次。在测试期间,这需要一个多小时才能完成。我什至把它分成三个脚本,每个脚本占客户列表的 1/3,但仍然需要很长时间。

我不确定它背后的逻辑是否存在问题,或者它是否不够有效。这是我的第一个 powershell 脚本,所以我不知道从哪里开始。

【问题讨论】:

  • 嗨,最大的 oneDirectory 文件夹中有多少文件(大约)?
  • oneDirectory 列出每个客户的所有寄存器(目录)。所以我会说他们都有不到100个目录。奇怪的是,当我运行脚本时,有时即使“oneDirectory”中只有一个寄存器,它也会变得很慢
  • 定时任务不好玩吗?我认为计划任务的最短休息时间是 1 小时。很抱歉让您感到困惑,但您对结果有何期待?您是否可以指定一组文件(例如 .txt 文件)?这似乎是一个报告脚本。您希望报告显示什么内容?

标签: powershell recursion count directory


【解决方案1】:

如果它真的是你的第一个 PS 脚本,那么这是一个非常好的第一枪:)。

提示:每次将循环嵌套在另一个循环中时,都应使用制表符缩进它。您也可以将; 留在 PS ^^ 中(除了在同一行上有多个语句)-如果可以的话,我会编辑您的帖子-完成,还在末尾添加了一个缺少的双引号消息正文

我尽量简化。

您能否尝试在配置中使用适当的值来运行它,看看结果是否为您提供了每个客户的正确卡住交易计数?

我会根据需要尝试改进。

编辑:也许让你慢下来的是邮件发送步骤:) 在第一次测试期间将其注释掉

#config
$mainFolder = "C:\Application\Customer."
$tenantID = 0 # test value
$maxTenantID = 2 # test value
$registerPath = "oneDirectory"

#initialize results array
$results = @()

#iterate through customers
while($tenantID -le $maxTenantID) {

    #build Path
    $loc = Join-Path $mainFolder$tenantID $registerPath

    #list files (excluding .ini)
    $files = Get-ChildItem -Path $loc -Recurse -Exclude "*.ini" |
        Where-Object { !$_.PSIsContainer }

    #reset counter
    $stuckCount = 0

    #iterate through files    
    foreach($file in $files) {

        #split path to get parent and ancestor        
        $parent = Split-Path $file.Directory -Leaf
        $ancestor = Split-Path $file.Directory.Parent.Parent -Leaf

        #if ancestor is oneDirectory and parent is transactions
        if(($ancestor -eq "oneDirectory") -and ($parent -eq "transactions")) {

            #increment counter
            $stuckCount++
        }
    }

    #create a custom object with customer name and stuck transactions count, and add it to the results array
    $results += [PSCustomObject]@{ Customer = $tenantID ; StuckCount = $stuckCount }

    if($stuckCount -gt 50) {
        #uncomment to send a mail if stuck transation count is over 50 for this customer
        #Send-MailMessage -From "email@whatever.com" -To "email@whatever.com" -SmtpServer "smtp.whatever.com" -Subject "Transactions stuck" -Body "$loc has over 50 transactions."
    }

    #increment customer ID
    $tenantID++
}

#output results array
$results

#example output :
#Customer StuckCount
#-------- -----------
#       0          58
#       1         110
#       2          13

#you could also send a global mail report here using $results array, for example :
#
#$results | ? { $_.StuckCount -gt 50 }
#
#will only list the customers with a stuck transaction count over 50

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 2011-10-06
    • 2021-09-09
    • 2020-06-08
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    相关资源
    最近更新 更多