How To Run PoshBot As A Windows Service

It’s not quite as easy as you’d expect to create a Windows service for a PowerShell project. Since PoshBot is a lot of PowerShell, Brandon walks us through it so we no longer have to depend on some interactive console being up all the time.

This techsnip will show you how to set up poshbot to run as a Windows service. Let's get started. In the how to install and run posh but Snip I showed you how to run poshbot interactively. That method is great for quickly getting started in poshbot and it's also useful during development of Plugins. But if you're running poshbot in production you’ll want this running as a Windows service so it's in the background and will start automatically after a reboot. The first thing we need to do to set up the windows services is download NSSM or the non sucking service manager. This utility is a service helper, which makes it very easy to set up applications or run scripts as a service. Using chocolaty you can install applications using the choco install command. So in the case of NSSM we’ll run choco install NSSM and will pass in the -y switch, which will auto confirm the install. To install applications with Chocolaty and to install windows services make sure you're running power shell as an administrator. Now let's go ahead and run choco install. If you want to or need to download NSSM manually then you can follow the link at the bottom of the screen. OK, now that NSSM is installed, we can go ahead, and set up our poshbot configuration. OK, now we need a poshbot configuration file that poshbot can load when the service starts up. To do that we’ll go ahead and import poshbot. Next we’ll create a hash table with parameters that will pass into the new poshbot configuration command. There's a bunch more parameters available in new poshbot configuration, but this will be fine for now. Notice we have a user name in the bot admin ski. This user will have admin privileges in posh bot. Next we set some directories the possible will use for its configuration logs an plugins. Monty set the C Colon Slash Tech snips next we have our bot API token. You can check out the how to install and run posh. Bop video for more details on how to get that from slack and now will splat this hashtable to new posh Bach configuration. This will return us a fully populated configuration object and then will save this configuration to disk by piping it to save possible configuration and passing in a file path. Now let's check out the startpos bot script. This is what will configured to run as a Windows service. The first thing we do is import the possible module then import the configuration that we just created using get possible configuration. Next we'll need to create a slack back end instance using the new pots pots lack back end command. Backends are the actual objects that communicate with the chat network. This function just needs the back end configuration section of our main bot configuration will then create a new instance of the bot using new posh spot instance, passing in the configuration an back end objects. Then we'll enter a while true loop to ensure the script never actually finishes. Inside that we have a try catch block or repipe. Arbot instance to start push button and sending any errors to the air variable if possible, has any errors or exceptions were going to throw those so that the cats block here can write them out to a file in the directory that we've configured. And that's it for the startup script now want to actually installing this as a service. A nice way to get commands that are in your path environment. Variable is using git CMD. We're going to grab the path to the NSM binary that we just installed with Chocolaty. Next will create strings for the path to our start POS. Bot script and for the name of the windows servers that we're going to create. We can use get command again to return the path to the power shell executable in this case, we're using power. Shell version 6. So the executable is called PWSH, but you can also substitute this with power shell dot EXE for power shell version 5. Now will create a string of arguments that will be passed to the power shell executable, including the path to the start posh posh script. And finally we can call the NSM Binary in passing the install argument to tell it to install as a service and also our service. Name The Path to the executable, which in our case is power Shell and lastly argument string. After NSM has created the service will modify this service and set the working directory to C Colon slash text nips. We can verify this service is installed using the NSM Status Command or using get service. And now we'll start the service up using start service. And we can verify that the service is now started. Let's hop on over to slack now and confirm possible is running. We can see that possibly is connected by its status here showing his green. Let's run the built-in status command to verify pashby is functioning. Looks like it's up and running and responding to commands. And that's how you can run posh spa as a Windows service.