# 定义多个配置项(模拟从配置文件读取) $configItems = @( @{ Name = "App-Api-Key"; Value = "sk-api-20251023-abcdefg123456" } @{ Name = "App-Jwt-Secret"; Value = "my-super-secret-jwt-key-2025" } @{ Name = "App-Redis-Connection"; Value = "redis://cache.redis.cache.windows.net:6380,password=xxx" } @{ Name = "App-Storage-Key"; Value = "storageAccountAccessKeyXYZ789" } @{ Name = "App-SendGrid-Key"; Value = "SG.sendgridapikey123456" } )
在混合办公和零信任架构日益普及的今天,条件访问(Conditional Access)已成为 Microsoft Entra ID(原 Azure AD)中最核心的安全控制手段之一。通过条件访问策略,管理员可以根据用户位置、设备状态、风险等级等信号,动态决定是否允许访问特定资源。然而,随着策略数量增长,手动管理门户中的数十条策略变得极其低效且容易出错。
PowerShell 与 Microsoft Graph API 的结合为条件访问策略的管理提供了自动化能力。无论是批量审计现有策略、快速创建标准化的安全基线策略,还是在紧急安全事件中快速调整策略状态,脚本化操作都比手动点击门户界面更可靠、更快速。特别是在多租户环境下,统一的脚本可以帮助安全团队确保所有租户的策略配置保持一致。
本文将介绍如何使用 PowerShell 通过 Microsoft Graph API 查询、创建、更新和报告条件访问策略,帮助你在日常运维和安全运营中提升效率。
if (-not$body.Title -or$body.Title.Trim().Length -eq0) { $errors += 'Title is required' } if ($body.Title -and$body.Title.Length -gt200) { $errors += 'Title must not exceed 200 characters' } if ($body.Priority -and$body.Priority -notin@('Low', 'Medium', 'High', 'Critical')) { $errors += "Priority must be one of: Low, Medium, High, Critical" } if ($body.Status -and$body.Status -notin@('Pending', 'InProgress', 'Completed', 'Cancelled')) { $errors += "Status must be one of: Pending, InProgress, Completed, Cancelled" }
# 组装 Prometheus 文本格式指标 $labels = "instance=`"$InstanceHostname`"" $lines = @( '# HELP system_cpu_usage_percent CPU usage percentage' '# TYPE system_cpu_usage_percent gauge' "system_cpu_usage_percent{$labels} $cpuUsage$timestamp" '' '# HELP system_memory_total_bytes Total physical memory in bytes' '# TYPE system_memory_total_bytes gauge' "system_memory_total_bytes{$labels} $($osInfo.TotalBytes) $timestamp" '' '# HELP system_memory_used_bytes Used physical memory in bytes' '# TYPE system_memory_used_bytes gauge' "system_memory_used_bytes{$labels} $($osInfo.UsedBytes) $timestamp" '' '# HELP system_memory_used_percent Memory usage percentage' '# TYPE system_memory_used_percent gauge' "system_memory_used_percent{$labels} $($osInfo.UsedPercent) $timestamp" '' '# HELP system_disk_total_bytes Total disk space in bytes' '# TYPE system_disk_total_bytes gauge' "system_disk_total_bytes{$labels} $diskTotal$timestamp" '' '# HELP system_disk_used_percent Disk usage percentage' '# TYPE system_disk_used_percent gauge' "system_disk_used_percent{$labels} $diskUsedPercent$timestamp" )
# HELP system_cpu_usage_percent CPU usage percentage # TYPE system_cpu_usage_percent gauge system_cpu_usage_percent{instance="WEB-SVR01"} 23.451729137600
# HELP system_memory_total_bytes Total physical memory in bytes # TYPE system_memory_total_bytes gauge system_memory_total_bytes{instance="WEB-SVR01"} 343597383681729137600
# HELP system_memory_used_bytes Used physical memory in bytes # TYPE system_memory_used_bytes gauge system_memory_used_bytes{instance="WEB-SVR01"} 201326592001729137600
# HELP system_memory_used_percent Memory usage percentage # TYPE system_memory_used_percent gauge system_memory_used_percent{instance="WEB-SVR01"} 58.591729137600
# HELP system_disk_total_bytes Total disk space in bytes # TYPE system_disk_total_bytes gauge system_disk_total_bytes{instance="WEB-SVR01"} 5368709120001729137600
# HELP system_disk_used_percent Disk usage percentage # TYPE system_disk_used_percent gauge system_disk_used_percent{instance="WEB-SVR01"} 72.311729137600
在现代混合 IT 环境中,Windows 和 Linux 服务器往往共存于同一基础设施。Ansible 作为无代理(agentless)的配置管理工具,原生支持通过 WinRM 协议管理 Windows 主机,而 PowerShell 正是 Ansible 在 Windows 端执行任务的核心引擎——每个 Ansible 模块在 Windows 上最终都会转化为 PowerShell 脚本执行。
if ($lastLine-match'failed=0'-and$lastLine-match'unreachable=0') { Write-Host"`nPlaybook 执行成功"-ForegroundColor Green } else { Write-Host"`nPlaybook 执行存在问题,请检查日志: $logFile"-ForegroundColor Red }
错误处理与重试机制:Kubernetes API 在高负载时可能返回 429 Too Many Requests 或 503 Service Unavailable。建议在关键操作的外层包装重试逻辑,配合指数退避策略(如初次等待 1 秒,后续每次翻倍),并区分可重试错误(网络超时、5xx)和不可重试错误(403 权限不足、404 资源不存在),避免无意义的重试循环。