PowerShell 技能连载 - 基于Azure Functions的无服务器安全检测
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
| function Invoke-SecurityScan { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string]$ResourceGroup,
[ValidateSet('Critical','High','Medium')] [string]$SeverityLevel = 'High' )
$securityReport = [PSCustomObject]@{ Timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss' ScannedResources = @() SecurityFindings = @() }
$alerts = Get-AzSecurityAlert -ResourceGroupName $ResourceGroup | Where-Object { $_.Severity -eq $SeverityLevel }
$alerts | ForEach-Object { $securityReport.ScannedResources += [PSCustomObject]@{ ResourceID = $_.ResourceId AlertType = $_.AlertType CompromiseEntity = $_.CompromisedEntity }
if($_.AlertType -eq 'UnusualResourceDeployment') { Start-AzResourceDelete -ResourceId $_.ResourceId -Force $securityReport.SecurityFindings += [PSCustomObject]@{ Action = 'DeletedSuspiciousResource' ResourceID = $_.ResourceId Timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss' } } }
$securityReport | ConvertTo-Json -Depth 3 | Out-File -FilePath "$env:TEMP/AzureSecReport_$(Get-Date -Format yyyyMMdd).json" return $securityReport }
|
核心功能:
- 实时获取Azure安全中心高等级警报
- 异常资源部署自动隔离机制
- JSON格式安全态势报告生成
- 多严重级别安全事件过滤
典型应用场景:
- 云环境异常操作实时响应
- 自动化安全基线维护
- 多云订阅安全状态聚合
- 合规审计日志自动生成