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
文章题目:MSSQL/WMI/PowerShell结合篇(四)PowerShell发送微信信息
标题来源:http://scyanting.com/article/pjceee.html