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


$ErrorActionPreference = 'SilentlyContinue'

$Results = @()

# klipa1 ... klipa35
$KlipaPattern = '^klipa(?:[1-9]|[12][0-9]|3[0-5])$'

$UserProfiles = Get-ChildItem 'C:\Users' -Directory -Force |
    Where-Object {
        $_.Name -notin @(
            'Public',
            'Default',
            'Default User',
            'All Users'
        )
    }

foreach ($UserProfile in $UserProfiles) {

    $UserName = $UserProfile.Name

    # Берем только папки первого уровня профиля
    $FirstLevelFolders = Get-ChildItem $UserProfile.FullName -Directory -Force |
        Where-Object {
            $_.Name -ne 'AppData'
        }

    foreach ($Folder in $FirstLevelFolders) {

        # ============================================================
        # Ищем .RDP
        # ============================================================
        $RdpFiles = Get-ChildItem `
            -Path "$($Folder.FullName)\*" `
            -Recurse `
            -Force `
            -ErrorAction SilentlyContinue |
            Where-Object {
                -not $_.PSIsContainer -and $_.Extension -ieq '.rdp'
            }

        foreach ($RdpFile in $RdpFiles) {

            $FullAddressLine = Get-Content `
                -LiteralPath $RdpFile.FullName `
                -ErrorAction SilentlyContinue |
                Where-Object {
                    $_ -match '^\s*full\s+address\s*:'
                } |
                Select-Object -First 1

            if (-not $FullAddressLine) {
                continue
            }

            # Например:
            # full address:s:klipa1
            # full address:s:klipa1:3389
            # full address:s:klipa1.domain.local
            if ($FullAddressLine -match '^\s*full\s+address\s*:\s*s\s*:\s*(.+?)\s*$') {

                $Address = $Matches[1].Trim()

                # Убираем порт
                $Host = ($Address -split ':')[0]

                # Убираем доменную часть
                $ShortHost = ($Host -split '\.')[0]

                if ($ShortHost -match $KlipaPattern) {

                    $Results += [PSCustomObject]@{
                        User = $UserName
                        Host = $ShortHost
                        Type = 'RDP'
                        Path = $RdpFile.FullName
                    }
                }
            }
        }

        # ============================================================
        # Ищем .LNK
        # ============================================================
        $LnkFiles = Get-ChildItem `
            -Path "$($Folder.FullName)\*" `
            -Recurse `
            -Force `
            -ErrorAction SilentlyContinue |
            Where-Object {
                -not $_.PSIsContainer -and $_.Extension -ieq '.lnk'
            }

        if ($LnkFiles) {

            $WshShell = New-Object -ComObject WScript.Shell

            foreach ($LnkFile in $LnkFiles) {

                try {
                    $Shortcut = $WshShell.CreateShortcut($LnkFile.FullName)

                    $TargetPath = $Shortcut.TargetPath
                    $Arguments  = $Shortcut.Arguments

                    $MatchedHost = $null

                    # mstsc.exe /v:klipa1
                    if (
                        [System.IO.Path]::GetFileName($TargetPath) -ieq 'mstsc.exe'
                    ) {

                        if (
                            $Arguments -match '(?i)/v(?::|\s+)\s*(klipa(?:[1-9]|[12][0-9]|3[0-5]))(?:[\.:]\S*)?'
                        ) {
                            $MatchedHost = $Matches[1]
                        }
                    }

                    # Ярлык непосредственно на .rdp
                    if (
                        -not $MatchedHost -and
                        $TargetPath -and
                        $TargetPath -match '\.rdp$' -and
                        (Test-Path -LiteralPath $TargetPath)
                    ) {

                        $Line = Get-Content `
                            -LiteralPath $TargetPath `
                            -ErrorAction SilentlyContinue |
                            Where-Object {
                                $_ -match '^\s*full\s+address\s*:'
                            } |
                            Select-Object -First 1

                        if (
                            $Line -match '^\s*full\s+address\s*:\s*s\s*:\s*(.+?)\s*$'
                        ) {

                            $Address = $Matches[1].Trim()
                            $Host = ($Address -split ':')[0]
                            $ShortHost = ($Host -split '\.')[0]

                            if ($ShortHost -match $KlipaPattern) {
                                $MatchedHost = $ShortHost
                            }
                        }
                    }

                    if ($MatchedHost) {

                        $Results += [PSCustomObject]@{
                            User = $UserName
                            Host = $MatchedHost
                            Type = 'LNK'
                            Path = $LnkFile.FullName
                        }
                    }
                }
                catch {
                }
            }
        }
    }
}

$Results = $Results |
    Sort-Object User, Host, Type, Path -Unique

Write-Output '============================================================'
Write-Output 'RDP KLIPA SCAN'
Write-Output "Computer: $env:COMPUTERNAME"
Write-Output '============================================================'

if (-not $Results -or $Results.Count -eq 0) {

    Write-Output 'Совпадений не найдено.'
}
else {

    Write-Output "Найдено совпадений: $($Results.Count)"
    Write-Output ''

    foreach ($Result in $Results) {

        Write-Output "Пользователь : $($Result.User)"
        Write-Output "Клипа        : $($Result.Host)"
        Write-Output "Тип          : $($Result.Type)"
        Write-Output "Файл         : $($Result.Path)"
        Write-Output '------------------------------------------------------------'
    }
}