[CmdletBinding()] param( [switch]$AsJson, [string]$Csv, [ValidateRange(1, 500)] [int]$Max = 100 ) $ErrorActionPreference = 'Stop' try { $services = @(Get-CimInstance -ClassName Win32_Service -ErrorAction Stop) } catch { Write-Error "Не удалось прочитать службы: $($_.Exception.Message)" exit 2 } $results = @() foreach ($service in $services) { $path = [string]$service.PathName if (-not $path) { continue } $reasons = @() $unquoted = $null if ($path -notmatch '^"') { if ($path -match '^(.+?\.exe)(\s|$)') { $unquoted = $matches[1] } if ($unquoted -and $unquoted -match '\s') { $reasons += 'путь не в кавычках' } } if ($path -match '(?i)(\\Temp\\|\\Users\\Public\\|\\AppData\\Roaming\\|\\ProgramData\\Temp\\)') { $reasons += 'временный каталог' } $executable = $null if ($path -match '^"([^"]+)"') { $executable = $matches[1] } elseif ($unquoted) { $executable = $unquoted } elseif ($path -match '^(\S+\.exe)') { $executable = $matches[1] } if ($executable -and (Test-Path -LiteralPath $executable -PathType Leaf)) { try { $acl = Get-Acl -LiteralPath $executable -ErrorAction Stop $writable = @($acl.Access | Where-Object { $_.AccessControlType -eq 'Allow' -and $_.IdentityReference -match '(Everyone|BUILTIN\\Users|Пользователи)' -and ([string]$_.FileSystemRights) -match 'Write|Modify|FullControl' }) if ($writable.Count -gt 0) { $reasons += 'файл доступен на запись пользователям' } } catch { } } if ($reasons.Count -gt 0) { $results += [pscustomobject]@{ Name = $service.Name DisplayName = $service.DisplayName State = $service.State StartMode = $service.StartMode PathName = $path Reasons = ($reasons -join '; ') } if ($results.Count -ge $Max) { break } } } if ($AsJson) { $results | ConvertTo-Json -Depth 5 exit 0 } if ($results.Count -eq 0) { Write-Host 'Подозрительных путей служб не найдено.' exit 0 } $results | Format-Table Name, State, StartMode, Reasons -AutoSize Write-Host ("Найдено служб: {0}" -f $results.Count) if ($Csv) { $results | Export-Csv -LiteralPath $Csv -NoTypeInformation -Encoding UTF8 Write-Host "CSV сохранён: $Csv" } exit 0