SQL Server 是企业级关系型数据库的常青树,广泛应用于金融、制造、零售等行业。作为 DBA 或运维工程师,日常需要频繁执行实例巡检、数据库备份、性能监控、空间分析等操作。传统做法是通过 SQL Server Management Studio(SSMS)手工完成,但面对多实例、多数据库的环境,图形界面操作效率低下且容易遗漏。PowerShell 凭借其强大的自动化能力和丰富的 SQL Server 模块生态,让这些任务变得可编程、可复用、可调度。
微软提供的 SqlServer 模块封装了 SQL Server Management Objects(SMO)库,几乎覆盖了 SSMS 能做的所有事情。从查询实例信息、管理数据库文件,到执行 T-SQL、配置安全策略,都可以通过 PowerShell 脚本完成。结合 Windows 任务计划或 SQL Agent Job,还能实现定时自动巡检和告警推送,大幅减轻 DBA 的重复劳动负担。
Name DisplayName Category ---- ----------- -------- 1e30110a-5ceb-460c-a204-fsdfsdf Require a tagon resources Tags 8e346d3c-483d-4fef-8d21-dsfsdf Inherit a tagfrom the resource groupTags ...
模块名称: MyUtils 版本号: 1.0.0 导出函数: Get-SystemReport, Invoke-HealthCheck VERBOSE: Successfully created an API key for PSGallery. VERBOSE: Attempting to publish module 'MyUtils' version '1.0.0' to 'PSGallery'. VERBOSE: Successfully published module 'MyUtils' version '1.0.0' to 'PSGallery'. 模块已成功发布到 PowerShell Gallery!
模块已上线! 名称: MyUtils 版本: 1.0.0 描述: A collection of utility functions for daily PowerShell tasks 作者: Your Name 下载量: 0 页面: https://www.powershellgallery.com/packages/MyUtils/1.0.0
# 更新清单中的版本号和发布说明 $updateData = @{ ModuleVersion = $newVersion.ToString() ReleaseNotes = "Fixed edge case in Get-SystemReport when WMI service is stopped." } Update-ModuleManifest-Path$manifestPath @updateData
当前版本: 1.0.0 → 新版本: 1.0.1 VERBOSE: Attempting to publish module 'MyUtils' version '1.0.1' to 'PSGallery'. VERBOSE: Successfully published module 'MyUtils' version '1.0.1' to 'PSGallery'. 新版本 1.0.1 已发布
操作组已就绪,可关联到告警规则。 Action Group ID: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/rg-production/providers/microsoft.insights/actionGroups/ag-production-critical
Microsoft Sentinel 是微软云原生的 SIEM(安全信息与事件管理)解决方案,能够收集、检测、调查和响应来自整个企业环境的安全威胁。在大型企业中,安全运营团队每天需要处理成百上千条告警,手动在门户中逐一排查效率极低。通过 PowerShell 自动化与 Sentinel API 的集成,我们可以实现告警的批量查询、自动化响应规则的部署以及威胁情报的快速推送,大幅提升安全运营效率。
本文将介绍如何使用 PowerShell 连接 Microsoft Sentinel REST API,完成查询安全告警、创建自动化分析规则、批量导入威胁指标(TI Indicator)以及导出事件报告等常见操作。这些方法不仅适用于日常安全运维,也能嵌入 CI/CD 流水线,实现安全策略的版本化管理和自动部署。
在开始之前,需要确保已安装 Az PowerShell 模块并完成身份认证。以下示例基于 Azure 资源管理器 REST API,适用于已部署 Microsoft Sentinel 工作区的环境。
连接 Sentinel 并获取工作区信息
第一步是连接 Azure 账户并获取 Sentinel 工作区的基本信息。我们需要订阅 ID、资源组名称和工作区名称三个关键参数,它们构成了所有 Sentinel API 调用的基础路径。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 安装 Az 模块(如果尚未安装) Install-Module-Name Az.Accounts, Az.OperationalInsights, Az.SecurityInsights -Force-Scope CurrentUser