在写了windows AD域控密码过期邮件推送告警后,有网友问我能不能写一篇关于企业微信推送的文章
如果需要看邮件推送或者需要自定义脚本,可以参考我的文章 windows AD域控密码过期邮件通知迭代版本
1.企业微信告警要求
1.1.企业微信推送需要具备的条件
- 需要在企业微信中创建企业微信应用,企业微信的应用创建可以参考:zabbix第三篇-zabbix配置企业微信告警
- 需要在AD域内找一个参数写上企业微信userid,以便将信息发送至对应的人员
1.2.AD域控内添加企业微信userid值
- 我再这里使用web page属性配置为企业微信userid值
- 用户的userid值,可以通过企业微信的通讯录导出
2.powersshell通过企业微信发送的脚本
# 引入Activedirectory模块
Import-Module ActiveDirectory# 强制使用 TLS 1.2,企业微信接口只支持TLS1.2及以上版本
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12# 企业微信的CorpID
$corpID = "corpID"
# 企业微信应用的应用的Secret
$corpSecret = "corpSecret"
# 企业微信应用的ID
$agentID = "agentID"# 获取访问令牌
$authUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpID&corpsecret=$corpSecret"
$authResponse = Invoke-RestMethod -Uri $authUrl -Method Get
$accessToken = $authResponse.access_token# 筛选合规用户
$alladuser = Get-ADUser -SearchBase "DC=SEC,DC=lab" -Filter {Enabled -eq $true -and PasswordNeverExpires -eq $false} | %{$_.SamAccountName}#################################################
# 检测AD密码过期时间并通过企业微信通知相应账户和管理员
#################################################
foreach ($user in $alladuser) {$userpassinfo = Get-ADUser $user -Properties msDS-UserPasswordExpiryTimeComputed$pwdExpiryTimeFileTime = $userpassinfo.'msDS-UserPasswordExpiryTimeComputed'$userweixin = Get-ADUser $user -Properties * | %{$_.wWWHomePage}$userdisName = Get-ADUser $user -Properties * | %{$_.DisplayName}$pwdExpiryTime = [datetime]::FromFileTime([int64]$pwdExpiryTimeFileTime)$now = Get-Date$expire_days = ($pwdExpiryTime - $now).Daysif ($expire_days -eq 15 -and $userweixin) {$chineseusername = Get-ADUser $user -Properties * | %{$_.DisplayName}# 企业微信消息正文$weixinMessageBody ="亲爱的 $chineseusername 同学:`r`n`r`n" +"您的域账户密码即将在 $expire_days 天后过期,$pwdExpiryTime 之后密码过期未修改会导致您无法连接使用域账号登陆各种系统请您尽快更改。`r`n`r`n" +"重置密码过程请遵循以下原则:`r`n" +"○ 密码长度最少 8 位;`r`n" +"○ 密码可使用最长时间 90天,过期需要更改密码;`r`n" +"○ 密码不得重新设置为初始密码;`r`n" +"○ 强制密码历史 2个(不能使用之前最近使用的 2 个密码);`r`n`r`n" +"○ 密码符合复杂性需求(大写字母、小写字母、数字和符号四种中必须有三种、且密码口令中不得包括全部或部分用户名)`r`n" +"密码修改系统如下:`r`n" +"○ 修改密码连接:密码修改的URL `r`n" +"○ 重置密码链接:密码重置系统的URL `r`n" +"谢谢,祝您生活愉快!" # 构建企业微信消息体$weixinMessage = @{"touser" = $userweixin"msgtype" = "text""agentid" = $agentID"text" = @{"content" = $weixinMessageBody}"safe" = 0} | ConvertTo-Json -Depth 10# 使用 UTF-8 编码发送请求$bytes = [System.Text.Encoding]::UTF8.GetBytes($weixinMessage)# 发送企业微信消息$weixinUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$accessToken"Invoke-RestMethod -Uri $weixinUrl -Method Post -Body $bytes -ContentType "application/json"}elseif ($expire_days -le 2 -and $userweixin) {$chineseusername = Get-ADUser $user -Properties * | %{$_.DisplayName}# 企业微信消息正文$weixinMessageBody ="亲爱的 $chineseusername 同学:`r`n`r`n" +"您的域账户密码即将在 $expire_days 天后过期,$pwdExpiryTime 之后密码过期未修改会导致您无法连接使用域账号登陆各种系统请您尽快更改。`r`n`r`n" +"重置密码过程请遵循以下原则:`r`n" +"○ 密码长度最少 8 位;`r`n" +"○ 密码可使用最长时间 90天,过期需要更改密码;`r`n" +"○ 密码不得重新设置为初始密码;`r`n" +"○ 强制密码历史 2个(不能使用之前最近使用的 2 个密码);`r`n`r`n" +"○ 密码符合复杂性需求(大写字母、小写字母、数字和符号四种中必须有三种、且密码口令中不得包括全部或部分用户名)`r`n" +"密码修改系统如下:`r`n" +"○ 修改密码连接:密码修改的URL `r`n" +"○ 重置密码链接:密码重置系统的URL `r`n" +"谢谢,祝您生活愉快!" # 构建企业微信消息体$weixinMessage = @{"touser" = $userweixin"msgtype" = "text""agentid" = $agentID"text" = @{"content" = $weixinMessageBody}"safe" = 0} | ConvertTo-Json -Depth 10# 使用 UTF-8 编码发送请求$bytes = [System.Text.Encoding]::UTF8.GetBytes($weixinMessage)# 发送企业微信消息$weixinUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$accessToken"Invoke-RestMethod -Uri $weixinUrl -Method Post -Body $bytes -ContentType "application/json"}
}
3.企业微信告警的验证
- 在ad域机器上运行该脚本后,就可以推送相关消息
4.定时运行脚本
- 在定时任务中新建定时任务,定时运行powershell脚本