本文介绍如何通过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
原文链接:https://www.f2er.com/bash/390407.html