MSSQL/WMI/PowerShell结合篇四PowerShell发送微信信息

本文介绍如何通过PowerShell发送微信信息


先申请企业微信(企业、政府、组织三种类型),通过PowerShell调用企业微信的API群发接口发送微信信息,API详细信息可参考企业微信开发者文档


以下为PowerShell调用企业微信API发送文本信息

----Script File: send_WeChat.ps1


param($param1,$param2,$param3)


##设置16-32位长度的密钥

function Set-Key {

param([string]$string)

$length = $string.length

$pad = 32-$length

if (($length -lt 16) -or ($length -gt 32)) {Throw "String must be between 16 and 32 characters"}

$encoding = New-Object System.Text.ASCIIEncoding

$bytes = $encoding.GetBytes($string + "0" * $pad)

return $bytes

}


##解密方法

function Get-EncryptedData {

param($key,$data)

$data | ConvertTo-securestring -key $key |

ForEach-Object {[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::securestringToBSTR($_))}

}


##发送微信方法

function send_WeChat_To_DBA {

Param(

[String]$corpid,

[String]$pwd,

[String]$Content

)

$auth_string = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$pwd"

$auth_values = Invoke-RestMethod $auth_string

$token = $auth_values.access_token

$body="{

`"toparty`":`"receive group id`",

`"agentid`":`"your agent id`",

`"text`":{

`"content`":`"$content`"

},

`"msgtype`":`"text`"

}"

$CN=[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 $CN

}


##解析帐号密码

$cidstr= Get-Content E:\Monitor\cidstr.txt;

$pwdstr = Get-Content E:\Monitor\keystr.txt;

$cidkey = Set-Key $cidstr;

$pwdkey = Set-Key $pwdstr;

$cidfromFile = Get-Content E:\Monitor\wxcid.txt;

$pwdfromFile = Get-Content E:\Monitor\wxpwd.txt;

$corpid = Get-EncryptedData -data $cidfromFile -key $cidkey;

$pwd = Get-EncryptedData -data $pwdfromFile -key $pwdkey;


$content=$param1+$param2+$param3


##调用发送方法

send_WeChat_To_DBA -corpid $corpid -pwd $pwd -Content $content



##send_WeChat.ps1调用方式

E:\Monitor\send_WeChat.ps1 $param1 $param2 $param3

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...