На терминальном сервере необходимо время от времени при заканчивании квоты смотреть файлы пользователя, для поиска их всех необходимо создать скрипт PS, этот скрипт вприниципе рабочий, берет пользователя и необходимые папки для проверки, но они выводят размер самого файла, а не размер занимаемый на диске, который учитывается в квоте, подскажите как можно собирать данные по занимаемому месту файлов и папок
param (
[string[]]$directoryPaths = @(
"C:\work"
),
[string]$domainUser = "Domain\User"
)
function Format-Size {
param (
[double]$sizeInBytes
)
$units = "Bytes", "KB", "MB", "GB"
$index = 0
while ($sizeInBytes -ge 1KB -and $index -lt $units.Length) {
$sizeInBytes /= 1KB
$index++
}
[PSCustomObject]@{
Size = "{0:F3}" -f $sizeInBytes
Unit = $units[$index]
}
}
function Get-ItemSize {
param (
[string]$itemPath
)
$item = Get-Item -LiteralPath $itemPath -ErrorAction SilentlyContinue
if ($item) {
if ($item.PSIsContainer) {
$size = Get-FolderSize -folderPath $item.FullName
} elseif ($item.PSIsContainer -eq $false) {
try {
$size = $item.Length
} catch {
$size = 0
}
}
} else {
$size = 0
}
return $size
}
function Get-FolderSize {
param (
[string]$folderPath
)
$folderSize = 0
$files = Get-ChildItem -Path $folderPath -Recurse -File -ErrorAction SilentlyContinue
foreach ($file in $files) {
$folderSize += Get-ItemSize -itemPath $file.FullName
}
return $folderSize
}
$totalFileSize = 0
foreach ($directoryPath in $directoryPaths) {
# Получаем все файлы и папки в указанной директории и её поддиректориях
$items = Get-ChildItem -Path $directoryPath -Recurse -ErrorAction SilentlyContinue
# Фильтруем элементы, принадлежащие указанному доменному пользователю
$filteredItems = $items | Where-Object {
$itemOwner = (Get-Acl -Path $_.FullName -ErrorAction SilentlyContinue).Owner
$itemOwner -eq $domainUser
}
# Выводим информацию о каждом элементе
foreach ($item in $filteredItems) {
$itemType = if ($item.PSIsContainer) { "Folder" } else { "File" }
$itemSize = Get-ItemSize -itemPath $item.FullName
if ($itemType -eq "File") {
$totalFileSize += $itemSize
}
$formattedSize = Format-Size -sizeInBytes $itemSize
Write-Host "${itemType}: $($item.FullName), Size: $($formattedSize.Size) $($formattedSize.Unit)"
}
}
# Выводим сумму размеров всех файлов в конце
$formattedTotalSize = Format-Size -sizeInBytes $totalFileSize
Write-Host "Total Size of Files: $($formattedTotalSize.Size) $($formattedTotalSize.Unit)"