Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell脚本,用于对单个文件夹中的文件进行计数,并在文件超过设置数时将计数输出到日志文件_Powershell - Fatal编程技术网

Powershell脚本,用于对单个文件夹中的文件进行计数,并在文件超过设置数时将计数输出到日志文件

Powershell脚本,用于对单个文件夹中的文件进行计数,并在文件超过设置数时将计数输出到日志文件,powershell,Powershell,“我需要每天14小时,每20秒循环一次。这就是我目前所拥有的,但我在循环和输出方面遇到了一些问题 $timeout = New-TimeSpan -Hours 14 $sw = [diagnostics.stopwatch]::StartNew() $count = (Get-ChildItem -Path 'D:\folder' | Measure-Object).count <# Loop condition

“我需要每天14小时,每20秒循环一次。这就是我目前所拥有的,但我在循环和输出方面遇到了一些问题


        $timeout = New-TimeSpan -Hours 14
        $sw = [diagnostics.stopwatch]::StartNew()
        $count = (Get-ChildItem -Path 'D:\folder' | Measure-Object).count
     
        <# Loop condition check every 20 seconds until timeout #>
        While ($sw.elapsed -lt $timeout){
        If ($count -gt 30)
        {
        "$([datetime]::Now) $count" | Out-File C:\temp\file_count.log -Append
        }
        start-sleep -Seconds 20
        }


$timeout=新时间跨度-14小时
$sw=[diagnostics.stopwatch]::StartNew()
$count=(获取子项-路径“D:\folder”|度量对象)。计数
While($sw.eassed-lt$timeout){
如果($count-gt 30)
{
“$([datetime]::Now)$count”|输出文件C:\temp\File\u count.log-追加
}
开始睡眠-20秒
}

我认为您的问题是,在
while
循环之外,您只对文件管理器进行了一次计数。
$Count=
移到
while
语句下方,我认为您的问题已经解决了

欢迎使用堆栈溢出!您当前的代码到底面临哪些问题?阅读,以便我们更好地回答您的问题。您没有展示或解释您的问题是什么。如前所述,这只是每隔20秒将文件夹中的文件数写入日志文件。如前所述,这应该是可行的。通过此检查,您每天只需输入大约40多个条目。@MichielTai是正确的,您需要在while循环中执行计数。如果您只想计算文件数,而不想计算目录数,请将switch
-File
添加到
Get-ChildItem
cmdlet中。