Загрузка данных


$ErrorActionPreference = "SilentlyContinue"

$TimeStamp = Get-Date -Format "yyyyMMdd_HHmmss"
$Report = "C:\Disk_C_UserProfiles_$TimeStamp.txt"

$TargetProfiles = @(
    "C:\Users\A.Semenov",
    "C:\Users\M.Rusakov",
    "C:\Users\S.Nikonov",
    "C:\Users\t.volkova",
    "C:\Users\S.Nikonov_do"
)

function Format-Size {
    param([long]$Bytes)

    if ($Bytes -ge 1GB) {
        return "{0:N2} GB" -f ($Bytes / 1GB)
    }
    elseif ($Bytes -ge 1MB) {
        return "{0:N2} MB" -f ($Bytes / 1MB)
    }
    elseif ($Bytes -ge 1KB) {
        return "{0:N2} KB" -f ($Bytes / 1KB)
    }
    else {
        return "$Bytes B"
    }
}

function Get-FolderSize {
    param([string]$Path)

    if (-not (Test-Path -LiteralPath $Path)) {
        return 0L
    }

    $Sum = (
        Get-ChildItem `
            -LiteralPath $Path `
            -File `
            -Force `
            -Recurse `
            -ErrorAction SilentlyContinue |
        Measure-Object -Property Length -Sum
    ).Sum

    if ($Sum) {
        return [long]$Sum
    }

    return 0L
}

function Write-Section {
    param([string]$Title)

    "" | Out-File $Report -Append -Encoding UTF8
    "============================================================" |
        Out-File $Report -Append -Encoding UTF8

    $Title |
        Out-File $Report -Append -Encoding UTF8

    "============================================================" |
        Out-File $Report -Append -Encoding UTF8

    "" | Out-File $Report -Append -Encoding UTF8
}

function Write-ObjectToReport {
    param($Object)

    $Object |
        Out-String -Width 350 |
        Out-File $Report -Append -Encoding UTF8
}

"" | Out-File $Report -Encoding UTF8

Write-Section "1. АКТИВНЫЕ СЕССИИ"

& quser.exe 2>&1 |
    Out-String -Width 350 |
    Out-File $Report -Append -Encoding UTF8

Write-Section "2. СОСТОЯНИЕ ПРОФИЛЕЙ"

$ProfileInfo = foreach ($ProfilePath in $TargetProfiles) {

    if (-not (Test-Path -LiteralPath $ProfilePath)) {
        continue
    }

    $WmiProfile = Get-WmiObject Win32_UserProfile |
        Where-Object {
            $_.LocalPath -eq $ProfilePath
        } |
        Select-Object -First 1

    $LastUseTime = $null

    if ($WmiProfile -and $WmiProfile.LastUseTime) {
        try {
            $LastUseTime = $WmiProfile.ConvertToDateTime(
                $WmiProfile.LastUseTime
            )
        }
        catch {
            $LastUseTime = $null
        }
    }

    $Size = Get-FolderSize $ProfilePath

    [pscustomobject]@{
        ProfilePath = $ProfilePath
        Size        = Format-Size $Size
        SizeBytes   = $Size
        Loaded      = if ($WmiProfile) {
            $WmiProfile.Loaded
        }
        else {
            "Unknown"
        }
        LastUseTime = $LastUseTime
        SID         = if ($WmiProfile) {
            $WmiProfile.SID
        }
        else {
            $null
        }
    }
}

Write-ObjectToReport (
    $ProfileInfo |
        Sort-Object SizeBytes -Descending |
        Select-Object ProfilePath, Size, Loaded, LastUseTime, SID
)

Write-Section "3. КРУПНЕЙШИЕ ПАПКИ ПЕРВОГО УРОВНЯ"

$FirstLevel = foreach ($Profile in $ProfileInfo) {

    Write-Host "Проверка: $($Profile.ProfilePath)" -ForegroundColor Cyan

    Get-ChildItem `
        -LiteralPath $Profile.ProfilePath `
        -Directory `
        -Force `
        -ErrorAction SilentlyContinue |
    ForEach-Object {

        $Size = Get-FolderSize $_.FullName

        [pscustomobject]@{
            ProfilePath = $Profile.ProfilePath
            Folder      = $_.FullName
            Size        = Format-Size $Size
            SizeBytes   = $Size
        }
    }
}

Write-ObjectToReport (
    $FirstLevel |
        Sort-Object SizeBytes -Descending |
        Select-Object ProfilePath, Folder, Size
)

Write-Section "4. КРУПНЕЙШИЕ ПАПКИ ВНУТРИ APPDATA"

$AppDataFolders = foreach ($Profile in $ProfileInfo) {

    $AppDataPaths = @(
        (Join-Path $Profile.ProfilePath "AppData\Local"),
        (Join-Path $Profile.ProfilePath "AppData\Roaming")
    )

    foreach ($AppDataPath in $AppDataPaths) {

        if (-not (Test-Path -LiteralPath $AppDataPath)) {
            continue
        }

        Get-ChildItem `
            -LiteralPath $AppDataPath `
            -Directory `
            -Force `
            -ErrorAction SilentlyContinue |
        ForEach-Object {

            $Size = Get-FolderSize $_.FullName

            if ($Size -ge 10MB) {
                [pscustomobject]@{
                    ProfilePath = $Profile.ProfilePath
                    Folder      = $_.FullName
                    Size        = Format-Size $Size
                    SizeBytes   = $Size
                }
            }
        }
    }
}

Write-ObjectToReport (
    $AppDataFolders |
        Sort-Object SizeBytes -Descending |
        Select-Object -First 100 ProfilePath, Folder, Size
)

Write-Section "5. КЭШИ И СРЕДЫ РАЗРАБОТКИ"

$KnownPaths = @(
    "AppData\Local\Temp",
    "AppData\Local\pip\cache",
    "AppData\Local\puccinialin",
    "AppData\Local\Microsoft\Terminal Server Client\Cache",
    "AppData\Local\Microsoft\Windows\INetCache",
    "AppData\Local\Google\Chrome\User Data\Default\Cache",
    "AppData\Local\Mozilla\Firefox\Profiles",
    ".rustup",
    ".cargo",
    ".nuget",
    ".openjfx"
)

$KnownResults = foreach ($Profile in $ProfileInfo) {

    foreach ($RelativePath in $KnownPaths) {

        $FullPath = Join-Path $Profile.ProfilePath $RelativePath

        if (Test-Path -LiteralPath $FullPath) {

            $Size = Get-FolderSize $FullPath

            [pscustomobject]@{
                ProfilePath = $Profile.ProfilePath
                Location    = $FullPath
                Size        = Format-Size $Size
                SizeBytes   = $Size
                Loaded      = $Profile.Loaded
                LastUseTime = $Profile.LastUseTime
            }
        }
    }
}

Write-ObjectToReport (
    $KnownResults |
        Sort-Object SizeBytes -Descending |
        Select-Object `
            ProfilePath,
            Location,
            Size,
            Loaded,
            LastUseTime
)

Write-Section "6. КРУПНЫЕ ФАЙЛЫ В ПРОФИЛЯХ"

$LargeFiles = foreach ($Profile in $ProfileInfo) {

    Write-Host "Поиск крупных файлов: $($Profile.ProfilePath)"

    Get-ChildItem `
        -LiteralPath $Profile.ProfilePath `
        -File `
        -Force `
        -Recurse `
        -ErrorAction SilentlyContinue |
    Where-Object {
        $_.Length -ge 30MB
    } |
    ForEach-Object {

        [pscustomobject]@{
            ProfilePath  = $Profile.ProfilePath
            Path         = $_.FullName
            Size         = Format-Size $_.Length
            SizeBytes    = $_.Length
            LastWriteTime = $_.LastWriteTime
        }
    }
}

Write-ObjectToReport (
    $LargeFiles |
        Sort-Object SizeBytes -Descending |
        Select-Object -First 100 `
            ProfilePath,
            Path,
            Size,
            LastWriteTime
)

Write-Section "7. СОСТОЯНИЕ ДИСКА C"

$Drive = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'"

$DiskInfo = [pscustomobject]@{
    SizeGB  = [math]::Round($Drive.Size / 1GB, 2)
    UsedGB  = [math]::Round(
        ($Drive.Size - $Drive.FreeSpace) / 1GB,
        2
    )
    FreeGB  = [math]::Round($Drive.FreeSpace / 1GB, 2)
    FreePct = [math]::Round(
        ($Drive.FreeSpace / $Drive.Size) * 100,
        1
    )
}

Write-ObjectToReport $DiskInfo

Write-Host ""
Write-Host "============================================================"
Write-Host "ПРОВЕРКА ЗАВЕРШЕНА"
Write-Host "============================================================"
Write-Host ""
Write-Host $Report -ForegroundColor Green