您的位置:首页 > 运维架构 > Shell

powershell监控windows服务并通过企业微信报警

2019-04-04 16:45 483 查看

新建services文档里面是需要监控的服务内容如下图:

ps脚本内容

#微信消息模块
function send-WeChat {
Param(
[String]$corpid,
[String]$secretid,
[String]$Content,
[String]$UserId,
[String]$AgentId
)
$auth_string = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$secretid"
$auth_values = Invoke-RestMethod $auth_string
$token = $auth_values.access_token
$body="{ 
`"touser`":`"$UserId`",
`"agentid`":`"$AgentId`",
`"text`":{
`"content`":`"$content`"
},
`"msgtype`":`"text`"
}"
$To_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 $To_CN
}


$Services = import-csv F:\service.txt
for ($i=0;$i -le 4;$i++){

$ServerName=$Services.ServerName[$i]
Get-Service $ServerName |foreach-object{$Status=$_.status}
Write-Host $Status
    if($Status -eq "Running")
    {
        Write-Host $ServerName"服务正常"
   }
       elseif($Status -eq "Stopped")
    {

    $corpid="xxxxxxxxxxxxx"
    $secretid="xxxxxxxxxxxxxxxxxxxx"
    $content=$ServerName+"服务已停止"#发送的文本信息
    $UserId="xxx" #用户组ID @all为全部
    $AgentId="xxxxx" #代理ID
    send-WeChat -corpid $corpid -pwd $secretid -Content $content -UserId $UserId -AgentId $AgentId
    }
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: