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"

}

结果:

 

posted on 2024-07-12 11:18  momingliu11  阅读(45)  评论(0编辑  收藏  举报