Скрипт по сбору статистики использования файлов на Windows
Опубликовано:
Тематические термины: Windows, Powershell.
Данный скрипт позволит получить сведения о типах файлов, которые есть на сервере и некоторые по ним сведения (количество, размер, дата обращения).
Пример скрипта
- $FilesDir = "D:\Отделы"
- $FileResult = "C:\Temp\result.csv"
- if ($args[0]) { $FilesDir = $args[0] }
- $FilesArray = @{}
- $i = 0
- $Count = 0
- Get-ChildItem $FilesDir -Recurse -Attributes !Directory+!System | ForEach-Object {
- $fileName = $_.Name
- $fileFullName = $_.VersionInfo.FileName
- if ($fileName -and $fileFullName) {
- $Extension = $fileFullName.split('.')[-1]
- $FileInfo = (Get-Item $fileFullName)
- $Length = $FileInfo.Length
- $CreationTime = $FileInfo.CreationTime
- $LastWriteTime = $FileInfo.LastWriteTime
- $LastAccessTime = $FileInfo.LastAccessTime
- if (!$FilesArray[$Extension]) {
- $FilesArray[$Extension] = @{}
- $FilesArray[$Extension]['size'] = @()
- $FilesArray[$Extension]['CreationTime'] = @()
- $FilesArray[$Extension]['LastWriteTime'] = @()
- $FilesArray[$Extension]['LastAccessTime'] = @()
- }
- $FilesArray[$Extension].add($fileFullName,$fileName)
- $FilesArray[$Extension]['size'] += $Length
- $FilesArray[$Extension]['CreationTime'] += $CreationTime
- $FilesArray[$Extension]['LastWriteTime'] += $LastWriteTime
- $FilesArray[$Extension]['LastAccessTime'] += $LastAccessTime
- $Count++
- Write-Host "$Count ... " -NoNewLine
- }
- }
- echo "Extension`tFilesCount`tMaxSize`tMinSize`tSumSize`tMaxCreationTime`tMinCreationTime`tMaxLastWriteTime`tMinLastWriteTime`tMaxLastAccessTime`tMinLastAccessTime" > $FileResult
- $FilesArray.Keys | % {
- $Extension = $_
- $FilesCount = $FilesArray[$_]['size'].Count
- $i += $FilesCount
- $MaxSize = ($FilesArray[$_]['size'] | measure -Maximum).Maximum
- $MinSize = ($FilesArray[$_]['size'] | measure -Minimum).Minimum
- $SumSize = ($FilesArray[$_]['size'] | measure -Sum).Sum
- $MaxCreationTime = ($FilesArray[$_]['CreationTime'] | measure -Maximum).Maximum
- $MinCreationTime = ($FilesArray[$_]['CreationTime'] | measure -Minimum).Minimum
- $MaxLastWriteTime = ($FilesArray[$_]['LastWriteTime'] | measure -Maximum).Maximum
- $MinLastWriteTime = ($FilesArray[$_]['LastWriteTime'] | measure -Minimum).Minimum
- $MaxLastAccessTime = ($FilesArray[$_]['LastAccessTime'] | measure -Maximum).Maximum
- $MinLastAccessTime = ($FilesArray[$_]['LastAccessTime'] | measure -Minimum).Minimum
- Write-Host "[$i/$Count]"
- echo "$Extension`t$FilesCount`t$MaxSize`t$MinSize`t$SumSize`t$MaxCreationTime`t$MinCreationTime`t$MaxLastWriteTime`t$MinLastWriteTime`t$MaxLastAccessTime`t$MinLastAccessTime" >> $FileResult
- }