这个PowerShell脚本功能全面,能从远程桌面、防火墙、共享文件夹、网络连接等多方面进行检查并生成HTML报告,为网络环境评估提供有力支持。
远程桌面服务检查
检查远程桌面是否启用,明确远程连接的基础配置情况。
查看TermService服务状态,确保该服务正常运行以支持远程桌面功能。
防火墙规则检查
检查远程桌面相关的防火墙规则,判断防火墙是否允许远程桌面连接的流量通过。
检查文件和打印机共享相关的防火墙规则,保障文件共享功能在网络中的可用性。
共享文件夹检查
列出所有非系统的共享文件夹,让用户清楚当前网络中可共享访问的资源。
检查网络适配器状态,确认网络硬件连接是否正常,为各项网络功能提供基础保障。
网络连接检查
获取公网IP地址,了解设备在互联网中的标识。
获取本地IP地址,明确设备在局域网内的网络定位。
测试外部可达性,判断设备能否正常连接外部网络。
综合分析
评估远程连接的可能性,综合远程桌面配置、服务状态、防火墙规则等因素给出判断。
评估文件拷贝的可能性,结合共享文件夹设置、防火墙规则及网络连接状况进行分析。
提供建议,根据各项检查结果,以笔记形式给出针对性的优化和改进建议,帮助用户提升网络使用体验和安全性。
<#
.SYNOPSIS
检查本地机器的远程连接设置和文件拷贝可能性(改进版)
.DESCRIPTION
此脚本检查以下内容:
1. 远程桌面服务状态
2. 防火墙规则是否允许远程连接
3. 共享文件夹设置
4. 网络连接状态
5. 文件拷贝可能性分析
改进点:
- 添加网络检查异常处理
- 确保桌面路径存在
- 添加更详细的错误日志
.NOTES
更新时间:2025年11月21日 19:16
#>
# 获取当前日期时间
$currentDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$desktopPath = [Environment]::GetFolderPath("Desktop")
# 确保桌面目录存在
if (-not (Test-Path $desktopPath)) {
$desktopPath = "$env:USERPROFILE\Desktop"
if (-not (Test-Path $desktopPath)) {
Write-Warning "无法找到桌面目录,将使用用户目录"
$desktopPath = $env:USERPROFILE
}
}
$htmlReportPath = "$desktopPath\RemoteConnectionReport_$(Get-Date -Format 'yyyyMMddHHmmss').html"
# 创建HTML报告头部
$htmlHeader = @"
<!DOCTYPE html>
<html>
<head>
<title>远程连接与文件拷贝检查报告</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1 { color: #0066cc; }
h2 { color: #0099cc; margin-top: 20px; }
table { border-collapse: collapse; width: 100%; margin-bottom: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
.success { color: green; font-weight: bold; }
.warning { color: orange; font-weight: bold; }
.error { color: red; font-weight: bold; }
.info { color: blue; font-weight: bold; }
</style>
</head>
<body>
<h1>远程连接与文件拷贝检查报告</h1>
<p>报告生成时间: $currentDate</p>
"@
# 检查远程桌面服务状态
function Check-RemoteDesktop {
try {
$rdpStatus = Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name "fDenyTSConnections" -ErrorAction Stop
$serviceStatus = Get-Service -Name "TermService" -ErrorAction Stop | Select-Object Status, StartType
$html = @"
<h2>1. 远程桌面服务检查</h2>
<table>
<tr><th>检查项</th><th>状态</th><th>详细信息</th></tr>
<tr><td>远程连接是否启用</td><td>$(if ($rdpStatus.fDenyTSConnections -eq 0) {'<span class="success">已启用</span>'} else {'<span class="error">已禁用</span>'})</td><td>注册表项 fDenyTSConnections = $($rdpStatus.fDenyTSConnections)</td></tr>
<tr><td>远程桌面服务状态</td><td>$(if ($serviceStatus.Status -eq 'Running') {'<span class="success">运行中</span>'} else {'<span class="error">未运行</span>'})</td><td>服务状态: $($serviceStatus.Status), 启动类型: $($serviceStatus.StartType)</td></tr>
</table>
"@
} catch {
$html = @"
<h2>1. 远程桌面服务检查</h2>
<p class="error">检查远程桌面服务时出错: $($_.Exception.Message)</p>
"@
}
return $html
}
# 检查防火墙规则
function Check-FirewallRules {
try {
$rdpRule = Get-NetFirewallRule -DisplayName "远程桌面*" -ErrorAction SilentlyContinue | Where-Object { $_.Enabled -eq 'True' -and $_.Direction -eq 'Inbound' }
$fileSharingRule = Get-NetFirewallRule -DisplayName "文件和打印机共享*" -ErrorAction SilentlyContinue | Where-Object { $_.Enabled -eq 'True' -and $_.Direction -eq 'Inbound' }
$html = @"
<h2>2. 防火墙规则检查</h2>
<table>
<tr><th>检查项</th><th>状态</th><th>详细信息</th></tr>
<tr><td>远程桌面防火墙规则</td><td>$(if ($rdpRule) {'<span class="success">已启用</span>'} else {'<span class="error">未启用</span>'})</td><td>$(if ($rdpRule) {"规则名称: $($rdpRule.DisplayName)"} else {"未找到启用的远程桌面入站规则"})</td></tr>
<tr><td>文件和打印机共享规则</td><td>$(if ($fileSharingRule) {'<span class="success">已启用</span>'} else {'<span class="warning">未启用</span>'})</td><td>$(if ($fileSharingRule) {"规则名称: $($fileSharingRule.DisplayName)"} else {"未找到启用的文件和打印机共享入站规则"})</td></tr>
</table>
"@
} catch {
$html = @"
<h2>2. 防火墙规则检查</h2>
<p class="error">检查防火墙规则时出错: $($_.Exception.Message)</p>
"@
}
return $html
}
# 检查共享文件夹
function Check-SharedFolders {
try {
$sharedFolders = Get-SmbShare -ErrorAction SilentlyContinue | Where-Object { $_.Name -notlike "*$" -and $_.Name -ne "IPC$" }
$networkAdapters = Get-NetAdapter -ErrorAction SilentlyContinue | Where-Object { $_.Status -eq 'Up' }
$html = @"
<h2>3. 共享文件夹检查</h2>
<table>
<tr><th>检查项</th><th>状态</th><th>详细信息</th></tr>
<tr><td>共享文件夹</td><td>$(if ($sharedFolders) {'<span class="success">存在</span>'} else {'<span class="info">无</span>'})</td><td>$(if ($sharedFolders) {"共享数量: $($sharedFolders.Count)"} else {"未找到非系统共享文件夹"})</td></tr>
<tr><td>活动网络适配器</td><td>$(if ($networkAdapters) {'<span class="success">已连接</span>'} else {'<span class="error">无</span>'})</td><td>$(if ($networkAdapters) {"适配器: $($networkAdapters.Name -join ', ')"} else {"未找到活动的网络适配器"})</td></tr>
</table>
"@
if ($sharedFolders) {
$html += "<h3>共享文件夹详情</h3><table><tr><th>共享名</th><th>路径</th><th>描述</th></tr>"
foreach ($share in $sharedFolders) {
$html += "<tr><td>$($share.Name)</td><td>$($share.Path)</td><td>$($share.Description)</td></tr>"
}
$html += "</table>"
}
} catch {
$html = @"
<h2>3. 共享文件夹检查</h2>
<p class="error">检查共享文件夹时出错: $($_.Exception.Message)</p>
"@
}
return $html
}
# 检查网络连接和可访问性
function Check-NetworkAccessibility {
try {
# 尝试多种方式获取公网IP
$publicIP = $null
$ipServices = @(
"https://api.ipify.org",
"https://icanhazip.com",
"https://ifconfig.me/ip"
)
foreach ($service in $ipServices) {
try {
$publicIP = (Invoke-RestMethod -Uri $service -TimeoutSec 5 -ErrorAction Stop).ToString().Trim()
if ($publicIP -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}') {
break
}
} catch {
Write-Verbose "无法从 $service 获取IP: $_"
}
}
if (-not $publicIP) {
$publicIP = "无法获取公网IP"
}
$localIP = (Get-NetIPAddress -ErrorAction SilentlyContinue | Where-Object { $_.AddressFamily -eq 'IPv4' -and $_.PrefixOrigin -ne 'WellKnown' }).IPAddress -join ', '
$html = @"
<h2>4. 网络连接检查</h2>
<table>
<tr><th>检查项</th><th>状态</th><th>详细信息</th></tr>
<tr><td>公网IP地址</td><td><span class="info">$publicIP</span></td><td>用于远程连接的公共IP地址</td></tr>
<tr><td>本地IP地址</td><td><span class="info">$localIP</span></td><td>内部网络使用的IP地址</td></tr>
</table>
<p class="info">注意: 由于网络限制,无法测试外部可达性</p>
"@
} catch {
$html = @"
<h2>4. 网络连接检查</h2>
<p class="error">检查网络连接时出错: $($_.Exception.Message)</p>
"@
}
return $html
}
# 综合分析远程连接和文件拷贝可能性
function Get-ConnectionAnalysis {
try {
$rdpEnabled = (Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name "fDenyTSConnections" -ErrorAction SilentlyContinue).fDenyTSConnections -eq 0
$rdpFirewall = [bool](Get-NetFirewallRule -DisplayName "远程桌面*" -ErrorAction SilentlyContinue | Where-Object { $_.Enabled -eq 'True' -and $_.Direction -eq 'Inbound' })
$fileSharingEnabled = [bool](Get-SmbShare -ErrorAction SilentlyContinue | Where-Object { $_.Name -notlike "*$" -and $_.Name -ne "IPC$" })
$fileSharingFirewall = [bool](Get-NetFirewallRule -DisplayName "文件和打印机共享*" -ErrorAction SilentlyContinue | Where-Object { $_.Enabled -eq 'True' -and $_.Direction -eq 'Inbound' })
$analysis = @()
if ($rdpEnabled -and $rdpFirewall) {
$analysis += "可以通过远程桌面(RDP)连接到此计算机"
} elseif ($rdpEnabled -and !$rdpFirewall) {
$analysis += "远程桌面已启用但可能被防火墙阻止"
} else {
$analysis += "远程桌面连接未启用"
}
if ($fileSharingEnabled -and $fileSharingFirewall) {
$analysis += "可以通过网络共享访问和拷贝文件"
} elseif ($fileSharingEnabled -and !$fileSharingFirewall) {
$analysis += "文件共享已启用但可能被防火墙阻止"
} else {
$analysis += "文件共享未启用"
}
$html = @"
<h2>5. 综合分析</h2>
<table>
<tr><th>功能</th><th>可能性</th><th>建议</th></tr>
<tr><td>远程桌面连接</td><td>$(if ($rdpEnabled -and $rdpFirewall) {'<span class="success">高</span>'} elseif ($rdpEnabled) {'<span class="warning">中等</span>'} else {'<span class="error">低</span>'})</td><td>$(if ($rdpEnabled -and $rdpFirewall) {"可以直接连接"} elseif ($rdpEnabled) {"检查防火墙设置"} else {"考虑启用远程桌面"})</td></tr>
<tr><td>文件共享拷贝</td><td>$(if ($fileSharingEnabled -and $fileSharingFirewall) {'<span class="success">高</span>'} elseif ($fileSharingEnabled) {'<span class="warning">中等</span>'} else {'<span class="error">低</span>'})</td><td>$(if ($fileSharingEnabled -and $fileSharingFirewall) {"可以直接访问共享"} elseif ($fileSharingEnabled) {"检查共享权限和防火墙"} else {"考虑设置共享文件夹"})</td></tr>
</table>
<h3>详细分析</h3>
<ul>
$(($analysis | ForEach-Object { "<li>$_</li>" }) -join '')
</ul>
"@
} catch {
$html = @"
<h2>5. 综合分析</h2>
<p class="error">进行综合分析时出错: $($_.Exception.Message)</p>
"@
}
return $html
}
# 生成完整的HTML报告
try {
$htmlReport = $htmlHeader +
(Check-RemoteDesktop) +
(Check-FirewallRules) +
(Check-SharedFolders) +
(Check-NetworkAccessibility) +
(Get-ConnectionAnalysis) +
@"
<footer>
<p>报告生成于: $currentDate</p>
<p>注意: 此报告仅供参考,实际连接可能还受路由器设置、ISP限制等因素影响。</p>
</footer>
</body>
</html>
"@
# 将报告保存到桌面
$htmlReport | Out-File -FilePath $htmlReportPath -Encoding UTF8 -Force
# 尝试打开生成的报告
if (Test-Path $htmlReportPath) {
try {
Start-Process $htmlReportPath
Write-Host "报告已生成并保存到: $htmlReportPath" -ForegroundColor Green
} catch {
Write-Host "报告已生成到: $htmlReportPath" -ForegroundColor Green
Write-Host "但无法自动打开报告: $_" -ForegroundColor Yellow
}
} else {
Write-Host "无法保存报告到指定路径: $htmlReportPath" -ForegroundColor Red
}
} catch {
Write-Host "生成报告时出现错误: $_" -ForegroundColor Red
}