MSSQL/WMI/PowerShell Associated articles (4) PowerShell sends Wechat messages
This article describes how to send Wechat messages through PowerShell
First apply for WeCom (three types of enterprise, government and organization). Call WeCom's API mass sending API to send Wechat messages through PowerShell. For more information on API, please see WeCom developer's documentation.
The following is PowerShell calling WeCom API to send a text message
-Script File: send_WeChat.ps1
Param ($param1,$param2,$param3)
# # set a key with a length of 16-32 bits
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
}
# # decryption method
Function Get-EncryptedData {
Param ($key,$data)
$data | ConvertTo-SecureString-key $key |
ForEach-Object {[Runtime.InteropServices.Marshal]:: PtrToStringAuto ([Runtime.InteropServices.Marshal]:: SecureStringToBSTR ($_))}
}
# # method of sending Wechat
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
}
# # resolving account password
$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
# # calling the sending method
Send_WeChat_To_DBA-corpid $corpid-pwd $pwd-Content $content
# # how to call send_WeChat.ps1
E:\ Monitor\ send_WeChat.ps1 $param1 $param2 $param3