poshbotio / PoshBot

Powershell-based bot framework
MIT License
537 stars 108 forks source link

Execute command on service startup or shutdown. #146

Closed mgeorgebrown89 closed 5 years ago

mgeorgebrown89 commented 5 years ago

I'm running a poshbot as a service, and I'm wondering if I can get it to send a message if it shutdowns or on start up. I've tried editing the script that the service runs, but I haven't had any success. Also, I'm not sure if this is necessarily an issue, but I don't really have anywhere/anyone else to ask, so I apologize if this isn't a strictly appropriate or desired use of this forum.

devblackops commented 5 years ago

@mgeorgebrown89 That's not a feature of PoshBot itself, but you can certain do that. Since you have the bot token (or other credentials depending on your chosen backend), you can use that to POST a message manually before starting the bot, and immediately after it stops. Something like this if you're using Slack and running PoshBot as a service.

Import-Module PoshBot
$pbc = Get-PoshBotConfiguration -Path C:\poshbot\config.psd1

while($true) {
    try {        
        $err = $null

        Send-SlackMessage -Token $pbc.BackendConfiguration.Token -Text 'PoshBot started' -Channel 'General'
        Start-PoshBot -Configuration $pbc -Verbose -ErrorVariable err
        if($err) {
            throw $err
        }
    }
    catch {
        $_ | Format-List -Force | Out-String | Out-File (Join-Path $pbc.LogDirectory Service.Error)
    }
}
Send-SlackMessage -Token $pbc.BackendConfiguration.Token -Text 'PoshBot stopped' -Channel 'General'
mgeorgebrown89 commented 5 years ago

ha. so that kind of worked for me. Only the message just send over and over again until I killed the service. And now the service won't respond in slack. This has actually happened before, where I send a command and it basically kills the service for whatever reason. I remove the service and recreate it and it works again for awhile.

mgeorgebrown89 commented 5 years ago

Yeah, @devblackops , I'm really confused now. This is probably beyond the scope of this disussion, but now when I create a service with nssm based of the script from the docs, it alternates between running and stopping about every 5 seconds.

Edit: I meant to say every 5 seconds. I'm not sure what's wrong, but I'm pretty sure it's how the service is set up, not my configuration. It just happened again.

mgeorgebrown89 commented 5 years ago

Based on the logs, it seems to run into conflicts with existing log files. If I delete the logs and roles, permissions, groups, and plugins psd1 files and then restart the service, it seems to start back up fine.

devblackops commented 5 years ago

@mgeorgebrown89 It sounds like you potentially have multiple instances of the bot running simultaneously.

mgeorgebrown89 commented 5 years ago

@devblackops I considered that, and that may be the case. It could be that I was a bit trigger-happy with restarting it.