PowerShell发送企业微信消息
参考:https://developer.work.weixin.qq.com/document/path/90372#%E6%96%87%E6%9C%AC%E5%8D%A1%E7%89%87%E6%B6%88%E6%81%AF
发送文本类型消息
Import-Module Microsoft.PowerShell.Utility
Function SendWechat($user,$days) { $corpid = "wechat" $secret = "123-446" $agentid = "1000000" $auth_sring = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$secret" $auth_values = Invoke-RestMethod $auth_sring $token = $auth_values.access_token $content = "$user 您好,您的邮箱密码即将在 $days 天后过期" $body = "{ `"touser`":`"$user`", `"agentid`":`"$agentid`", `"text`":{`"content`":`"$content`"}, `"msgtype`":`"text`" }" $body_u8 = [System.Text.Encoding]::UTF8.GetBytes($body) Invoke-RestMethod "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$token" -ContentType "application/json" -Method Post -Body $body_u8 } $user = "zhangsan" SendWechat $user 6
发送卡片类型消息
Function SendWechatCard($user,$days) { $corpid = "wecorp" $secret = "1234" $agentid = "1000000" $auth_sring = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$secret" $auth_values = Invoke-RestMethod $auth_sring $token = $auth_values.access_token $content = "$user 您好,您的邮箱密码即将在 $days 天后过期,为了不影响您正常登录公司系统,请尽快修改新密码" $body = "{ `"touser`":`"$user`", `"agentid`":`"$agentid`", `"textcard`":{`"title`":`"密码到期提醒`",`"description`":`"$content`",`"url`":`"www.baidu.com`",`"btntext`":`"More`"}, `"msgtype`":`"textcard`" }" $body_u8 = [System.Text.Encoding]::UTF8.GetBytes($body) Invoke-RestMethod "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$token" -ContentType "application/json" -Method Post -Body $body_u8 }
结果:
发送Markdown类型消息
Function SendWechatMarkdown($user,$days,$DisplayName) { $corpid = "wechat" $secret = "sec123" $agentid = "1000111111" $auth_sring = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$secret" $auth_values = Invoke-RestMethod $auth_sring $token = $auth_values.access_token $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6e726f64bf282063&redirect_uri=http%3A%2F%2Fyxpwdreset.yxops.cn%2Fsync-tool-up%2FehrToAD%2Flogin%2FloginByCode&response_type=code&scope=snsapi_privateinfo&agentid=1000231&state=ehr2ad#wechat_redirect" $content = "\n>**密码到期提醒** \n $DisplayName 您好,您的邮箱密码即将在<font color='warning'> $days </font>天后过期,为了不影响您正常登录公司系统,请点击:[修改密码]( $url )`n密码修改方式:企业微信" $body = @" { "msgtype":"markdown", "touser": "$user", "agentid":"$agentid", "markdown": {"content": "$content"} } "@ $body_u8 = [System.Text.Encoding]::UTF8.GetBytes($body) $response = Invoke-RestMethod "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$token" -ContentType "application/json" -Method Post -Body $body_u8 $errmsg = $response.errmsg return "Send wechat message,$errmsg" }
结果:
分类:
PowerShell
标签:
wechat
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-07-12 禁用IPv6
2016-07-12 检查远端服务器端口是否打开
2016-07-12 远程桌面卡