我的服务器上有 Powershell v1。如果我有较新版本的 PowerShell,那会很容易,但目前对我来说这不是一个选择。
我有一个脚本,用于查看特定文件夹中的文件是否已超过 10 分钟,如果是,则发送通知。如果没有子目录或只有几个目录,该脚本效果很好,但目录越多,所需的时间就越长。
我也不能只创建一个单独的文件夹来查看,这也不是一个选择。
有没有办法可以运行此行,以便我只查看文件而不查看目录?哦,是的,我确实在没有 -recurse 的情况下运行了它,但它不起作用。
function FileAge($path, $days, $hours, $mins)
{
$files = @(get-childitem $path -include *.* -recurse | where {($_.psIsContainer -eq $false) -and ($_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins))})
...
无需赘述,如果我有路径 c:\temp,它确实可以工作。大约需要 30 秒,但如果我将路径更改为 c:\,则需要 2 个小时。
答案1
命令行:
ps-run.cmd listf15.ps1
首次运行~33 GB/分钟(~30 分钟 - 1Tb)
上次运行~125-250 GB/分钟(~8-4 分钟 - 1Tb)
它很慢……
ps-运行.cmd:
@IF "%time:~0,1%" GTR "0" SET current=%DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%@%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2%
@IF "%time:~0,1%" LSS "0" SET current=%DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%@0%TIME:~1,1%.%TIME:~3,2%.%TIME:~6,2%
@echo %current%
powershell .\%* 9>&1 8>&1 7>&1 6>&1 5>&1 4>&1 3>&1 2>&1 0>&1 >> .\err%current%.txt
::STDIN 0 Keyboard Input
::STDOUT 1 Output in the Command Prompt window
::STDERR 2 Output error in the command prompt window
::UNDEFINED 3-9
列表f15.ps1:
$RunTime = [System.Diagnostics.Stopwatch]::StartNew()
$CurentTime=$(get-date)
echo "Run @ $CurentTime"
[string]$ReportLine = [char]183
$ReportLine = [string]$ReportLine * 80
$cd = [environment]::CurrentDirectory
$logfile=$cd+'\SystemDrive.txt'
$HH=10
[DateTime]$TM=(get-date).AddHours(-$HH)
ac -Path $logfile -Va $ReportLine
ac -Path $logfile -Va "Last Write Time $HH hours"
ac -Path $logfile -Va $TM" - "$CurentTime
ac -Path $logfile -Va $ReportLine
$Files = ls ${Env:SystemDrive} -recurse -force -filter "*" | ?{!$_.PSIsContainer -and ($_.LastWriteTime -GT [DateTime]$TM)}
$i =0
Foreach ($file in $Files) {
'"{0:yyyy-MM-dd HH:mm:ss}";"{1}";"{2}"' -f $file.LastWriteTime, $file.Length, $file.FullName | `
Out-File -encoding "Default" -append $logfile -width 400 -OutBuffer 2147483647 -EA 0
$i++
}
write-host "Count : $i"
$CurentTime=$(get-date)
ac -Path $logfile -Va $ReportLine
ac -Path $logfile -Va "Count : $i"
write-host "End @ $CurentTime"
write-host "Total Run : $($RunTime.Elapsed.ToString())"
ac -Path $logfile -Va $CurentTime
ac -Path $logfile -Va "Total Run : $($RunTime.Elapsed.ToString())"
ac -Path $logfile -Va $ReportLine
输出,SystemDrive.txt:
················································································
Last Write Time 10 hours
06/20/2013 16:48:04 - 06/21/2013 02:48:04
················································································
"2013-06-20 17:45:47";"0";"C:\Users\Administrator\Application Data\Macromedia\Flash Player\Logs\flashlog.txt"
"2013-06-20 19:41:57";"602";"C:\Users\Administrator\Application Data\Macromedia\Flash Player\macromedia.com\support\flashplayer\sys\settings.sol"
"2013-06-20 19:41:57";"81";"C:\Users\Administrator\Application Data\Macromedia\Flash Player\macromedia.com\support\flashplayer\sys\#s.ytimg.com\settings.sol"
"2013-06-21 00:45:35";"372637";"C:\Users\Administrator\Application Data\Microsoft\CryptnetUrlCache\Content\018C4A5B5879D48381195FA49E2E6532"
"2013-06-20 18:10:34";"31235";"C:\Users\Administrator\Application Data\Microsoft\CryptnetUrlCache\Content\08A1C9C30288A186F932D2F6CE954534"
... etc ....
················································································
Count : 4333
21.06.2013 2:57:42
Total Run : 00:03:55.8185680
················································································
输出,[电子邮件保护]:
Run @ 06/21/2013 02:48:04
Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At D:\VBS\PS-03FileList\listf15.ps1:20 char:12
+ $Files = ls <<<< ${Env:SystemDrive} -recurse -force -filter "*.*" | ?{!$_.PSIsContainer -and ($_.LastWriteTime -GT [DateTime]$TM)}
+ CategoryInfo : ReadError: (C:\Not! David Blaine! раскукож меня обратно! ... VJCP 68096 97):String) [Get-ChildItem], PathTooLongException
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand
%windir%\system32\WindowsPowerShell\v1.0\
powershell.exe.配置,powershell_ise.exe.配置:
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v2.0.50727"/>
<supportedRuntime version="v4.0.30319"/>
</startup>
</configuration>
没有错误:
命令行,Powershell 64位永久不使用.Net 4.0并且不使用多线程:
reg add hklm\software\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 0 /f
命令行,Powershell 32位永久不使用.Net 4.0并且不使用多线程:
reg add hklm\software\wow6432node\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 0 /f