WCOMAB / PRTGSlackNotification

PowerShell script for sending PRTG notifications to Slack channel
MIT License
40 stars 13 forks source link

Unable to test message #1

Closed Zxurian closed 8 years ago

Zxurian commented 9 years ago

I've followed the walkthrough, but trying to test the notification isn't sending out any messages to my slack channel. The .ps1 file is copied, and the PRTG notifications are setup as such, however when hitting the "test" button for the notification, no messages arrive in the appropriate slack channel.

Any ideas?

img1 img2

devlead commented 9 years ago

By default the script will output log to slack.log

Usually the working directory for PRTG processes is C:\Windows\System32 so check if there's a log at C:\Windows\System32\slack.log

Test message doesn't set all parameters, but test message should still work though.

Zxurian commented 9 years ago

was a permission issue.

Awesome powershell script!

devlead commented 9 years ago

Excellent that the issue was resolved. :+1: Thanks for the kind words, please let us know if you have any other feedback.

Dirk23 commented 8 years ago

Sorry for reopen, but i have almost the same problem. After allowing unsigned Shellscripts with set-executionpolicy remotesigned and right-click on the .ps1 file under 'properties' klicking 'allow' the script can be executed in Powershell. But i get no message in Slack. The Logfile is created in the same folder the .ps1 is located but i cant get it to send a message to slack when i call it in Powershell with:

PS C:\Program Files (x86)\PRTG Network Monitor\Notifications\EXE> .\PRTGSlackNotification.ps1 -SlackToken 'mysecretslacktoken' -SlackChannel '#general' -SiteName '%sitename' -Device '%device' -Name '%name' -Status '%status' -Down '%down' -DateTime '%datetime' -LinkDevice '%linkdevice' -Message '%message'

The Errormessage says that it cant find the cmdlet Invoke-RestMethod. If you understand German:

Die Benennung "Invoke-RestMethod" wurde nicht als Name eines Cmdlet, einer Funktion, einer Skriptdatei oder eines ausführbaren Programms erkannt. Überprüfen Sie die Schreibweise des Namens, oder ob der Pfad korrekt ist (sofern enthalten), und wiederholen Sie den Vorgang.
Bei C:\Program Files (x86)\PRTG Network Monitor\Notifications\EXE\PRTGSlackNotification.ps1:34 Zeichen:18
+ Invoke-RestMethod <<<<  -Uri https://slack.com/api/chat.postMessage -Body $postSlackMessage | Out-File -FilePath slack.log -Append
    + CategoryInfo          : ObjectNotFound: (Invoke-RestMethod:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

A Logfile i created with:


Name                           Value                                                                                   
----                           -----                                                                                   
icon_url                       http://www.paessler.com/static/banner_1s_80x80.png                                      
text                           *%sitename*...                                                                          
channel                        #general                                                                                 
token                          mysecretslacktoken                                       
unfurl_links                   true                                                                                    
username                       PRTG                                                                                    

*%sitename*
<%linkdevice|%device %name>
Status %status %down on %datetime
```%message```

But when i invole the Test in PRTG, no logfile is created. Even testing a Sensor with that notification wont invoke a message or creation of that logfile. There is also no logfile under c:\windows\system32

What am i doint wrong?

Regards

Dirk

Dirk23 commented 8 years ago

i found out that the "Invoke-RestMethod" was introduced to PowerShell 3. Seems i have PowerShell2 after i executed $PSVersionTable.PSVersion i could see:

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1
devlead commented 8 years ago

@Dirk23 correct Dirk Invoke-RestMethod was introduced with PowerShell 3, I could probably convert it to PowerShell 2, I'll take a look.

Dirk23 commented 8 years ago

i could install powershell 4

devlead commented 8 years ago

PowerShell 5 is available too now, V2 support should be possible too though. https://www.microsoft.com/en-us/download/details.aspx?id=50395

devlead commented 8 years ago

@Dirk23 there's now a PowerShell V2 version avail here: https://github.com/WCOMAB/PRTGSlackNotification/blob/master/PRTGSlackWebHookNotificationPSv2.ps1

Dirk23 commented 8 years ago

nice. thank you. Could you do the same for the PRTGSlackNotification.ps1?

Dirk23 commented 8 years ago

So i tried to execute your ps1 on a Server 2012 with PowerShell 4 but there is no Message apearing in Slack nor the logfile is created. Seems it needs to run as Admin, because executing the script in PowerShall as Admin works fine!! What do i have to do?

devlead commented 8 years ago

@Dirk23 hmm I didn't run as admin could it be execution policy the prevents it? If you type Get-ExecutionPolicy what do you get? I've got mine set to RemoteSigned.

You can start an admin prompt and type Set-ExecutionPolicy RemoteSigned if it's blocket.

Try running script manually from non admin prompt and see what happens.

.\PRTGSlackWebHookNotificationPSv2.ps1 -SlackWebHook 'https://hooks.slack.com/services/...' -SlackChannel '#prtg' -SiteName '%sitename' -Device '%device' -Name '%name' -Status '%stat
us' -Down '%down' -DateTime '%datetime' -LinkDevice '%linkdevice' -Message '%message'

Also make sure you post to an existing channel, to can test @your_username to get an message from the slackbot,

devlead commented 8 years ago

Some PRTG instances run in System32, you can try setting a different path in the script for the log that everyone has permission to write to.

Dirk23 commented 8 years ago

PS C:\Users\user> Get-ExecutionPolicy Unrestricted

devlead commented 8 years ago

@Dirk23 ok, perhaps the assembly loading perhaps .Net 3.5 isn't installed, posted a v2 version of non web hook version too https://github.com/WCOMAB/PRTGSlackNotification/blob/master/PRTGSlackNotificationPSv2.ps1

It doesn't use 3.5 assemblies to that might work.

Dirk23 commented 8 years ago

is it posible to disable the log? I dont need it at all.

devlead commented 8 years ago

Actually the script ( PRTGSlackNotificationPSv2.ps1 ) using token has no logging so you could test if that works.

Dirk23 commented 8 years ago

PRTGSlackNotificationPSv2.ps1 runs in Powershell and posts a Message to the channel, but triggert by PRTG it doesnt...... I'm a bit confused now!

devlead commented 8 years ago

Could it be that it PRTG service is running as another user and it's got a different execution policy?

Dirk23 commented 8 years ago

It runs as "Local System"

devlead commented 8 years ago

Don't know it this still is the case, but apparently PRTG runs x86 version of PowerShell so when setting policy that's what needs to be used, info on PRTG kb https://kb.paessler.com/en/topic/43543-problems-with-powershell-script

Dirk23 commented 8 years ago

Yes, PRTG still uses x86 Powershell ad anll i needed to do is start Windows PowerShell (x86) and Set-ExecutionPolicy Unrestricted. Works now on all Servers. Thank you for your patience

devlead commented 8 years ago

@Dirk23 superb :+1: