powershell 使用Powershell发送Slack消息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 使用Powershell发送Slack消息相关的知识,希望对你有一定的参考价值。

#CHECK IF SERVICES HAVE STARTED OR NOT

Set-StrictMode -Version Latest

$server = $(Get-WmiObject Win32_Computersystem).name
$uri = "https://hooks.slack.com/services/my_slack_uri_here"
$user = "Mr. Robot"
$channel = "#server_chatter"
$emoji = ":desktop_computer:"
$service = "Service_Name"

function TellSlack{

    $payload = @{
	    "channel" = $channel
	    "icon_emoji" = $emoji
	    "text" = "$service was online"
	    "username" = $user
    }

    Invoke-WebRequest `
	    -Body (ConvertTo-Json -Compress -InputObject $payload) `
	    -Method Post `
	    -Uri $uri | Out-Null
}

function FuncCheckService{
    param($ServiceName)
    $arrService = Get-Service -Name $ServiceName
    if ($arrService.Status -ne "Running"){
        Start-Service $ServiceName
        Write-Host "Starting " $ServiceName " service" 
        " ---------------------- " 
        " Service is now started"
    }
    if ($arrService.Status -eq "running"){ 
        Write-Host "$ServiceName service is already started"
        TellSlack
    }
}

FuncCheckService -ServiceName $service
Set-StrictMode -Version Latest

$payload = @{
	"channel" = "#server_chatter"
	"icon_emoji" = ":bomb:"
	"text" = "This is my message. Hello there!"
	"username" = "Mr. Robot"
}

Invoke-WebRequest `
	-Body (ConvertTo-Json -Compress -InputObject $payload) `
	-Method Post `
	-Uri "https://hooks.slack.com/services/my_slack_uri_here" | Out-Null

以上是关于powershell 使用Powershell发送Slack消息的主要内容,如果未能解决你的问题,请参考以下文章

powershell 使用PowerShell发送电子邮件

powershell 使用Powershell发送Microsoft Teams消息

使用 PowerShell 向窗口控件发送消息

powershell 使用SendGrid从PowerShell发送电子邮件(在Azure中)

PowerShell小技巧:通过Powershell 发送消息给企业微信机器人

使用 PowerShell 发送电子邮件时是不是可以指定 Exchange 版本?