在零信任安全架构中,设备合规性验证是重要环节。以下脚本实现自动化设备安全检查:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| function Get-DeviceCompliance { [CmdletBinding()] param( [ValidateSet('Basic','Advanced')] [string]$CheckLevel = 'Basic' )
$report = [PSCustomObject]@{ TPMEnabled = $false BitLockerStatus = 'NotEncrypted' FirewallActive = $false LastUpdateDays = 999 Compliant = $false }
try { $tpm = Get-CimInstance -ClassName Win32_Tpm -Namespace root/cimv2/Security/MicrosoftTpm $report.TPMEnabled = $tpm.IsEnabled_InitialValue
$blv = Get-BitLockerVolume -MountPoint $env:SystemDrive 2>$null $report.BitLockerStatus = if($blv.ProtectionStatus -eq 'On') {'Encrypted'} else {'NotEncrypted'}
$fw = Get-NetFirewallProfile | Where-Object {$_.Enabled -eq 'True'} $report.FirewallActive = [bool]($fw | Measure-Object).Count
$lastUpdate = (Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 1).InstalledOn $report.LastUpdateDays = (New-TimeSpan -Start $lastUpdate -End (Get-Date)).Days
if($CheckLevel -eq 'Advanced') { $report | Add-Member -NotePropertyName SecureBoot -NotePropertyValue (Confirm-SecureBootUEFI) $report | Add-Member -NotePropertyName HyperVEnabled -NotePropertyValue (Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V).State }
$report.Compliant = $report.TPMEnabled -and ($report.BitLockerStatus -eq 'Encrypted') -and $report.FirewallActive -and ($report.LastUpdateDays -lt 30) } catch { Write-Warning "设备检查异常: $_" }
return $report }
|
实现原理:
- 通过WMI/CIM接口获取TPM芯片状态,验证硬件安全基础
- 检查BitLocker加密状态,确保数据存储安全
- 扫描防火墙配置,确认至少有一个激活的防护配置文件
- 计算系统最后更新天数,确保漏洞及时修补
- 高级模式增加UEFI安全启动和虚拟化安全检查
使用示例:
1 2 3 4 5
| Get-DeviceCompliance
Get-DeviceCompliance -CheckLevel Advanced
|
最佳实践:
- 与Intune等MDM解决方案集成
- 定期通过任务计划执行检查
- 对不合规设备启动修复流程
- 记录检查结果到中央日志服务器
注意事项:
• 需要本地管理员权限执行
• 部分检查仅支持Windows 10/11企业版
• 建议配合组策略共同使用