I have been trying to do a lot more with PowerShell DSC of late, including writing my own DSC resources.
Last week, I decided to write some resources for SABnzbd and Sonarr. Why? Well firstly for practice, but also because no one else has looked at building up packages for these apps (there wasnāt even Chocolatey packages).
I am not going to go into detail on implementing classes in PowerShell, instead checkout:
- Lee Holmes - Playing with Classes in PowerShell v5 Preview
- Trevor Sullivan - Implementing a .NET Class in PowerShell v5
- Jacob Benson - Creating a Class based DSC Resource using PowerShell
- David O'Brien - Windows PowerShell DSC - classes - introduction (part 1)
- David O'Brien - Windows PowerShell DSC - classes - resource basics (part 2)
- Microsoft MSDN - Writing a custom DSC resource with PowerShell classes
SABnzbd
The module cSABnzbd contains a single resource: cSABnzbdInstall. This resource specifying:
- Ensure: <String> { Ensure | Absent }
- ServiceCredential: <PSCredential>
If you opt for Ensure to be āPresentā, then the resource will install and update SABnzbd. That is right, as new versions are released, it will be automatically updated.
The ServiceCredential allows for us to control what account the SABnzbd services run as. If not specified, LocalHost will be used.
GetLatestVersion()
SABnzbd hosts both its source code and its binary releases in GitHub. This is great! GitHub provides a simple REST API that allows us to very easily and dynamically get the latest release for a project.
If we want to get information on the latest release for a particular repository, we simply perform a get request on https://api.github.com/repos/{owner}/{repo}/releases/latest. The response will contain things like the version name, the release date and the assets in the release. The GitHub API doesnāt need any authentication credentials or tokens; these requests can be performed anonymously.
Performing the API request for sabnzbd the URL for us would be https://api.github.com/repos/sabnzbd/sabnzbd/releases/latest, and the returned information looks like:
The function is pretty simple:
Get()
The Get() function is quite easy to implement. We simply need to call Get-Package, and sets the result against the ensure parameter. We then return the object as required. The result is:
Test()
The Test() is also pretty simple. We use Get-Package again to see if the package is installed or not.
If Ensure = āPresentā, then we need to check:
- If no package was returned, then return false
- If it is installed, compare the installed version to the version number returned using GetLatestVersion()
If Ensure = āAbsentā, then we simply compare $Package to $null.
Set()
Now the Set() function is more complex.
If Ensure = āPresentā, then we need to:
- Find the latest .exe in the release information using GetLatestVersion()
- Download the file, this is listed under the assets (look for a .exe).
- Silently run setup using the /S switch
- Install and start the services (optionally running under the user account specified with ServiceCredential).
If we donāt want SABnzbd installed, then we simply call the uninstall.exe.
Using and Configuring SABnzbd
You should be able to access SABnzbd via http://localhost:8080. When you connect the first time, you will be stepped through the initial setup wizard. I havenāt included any resources for specifying any settings like servers, scheduling or custom folders, you can do this as you would normally do. For more help, try the User Manual.
Getting the module
The module can be found on:
You will need PowerShell 5 or greater installed due to the use of Classes.
If you encounter any issues, please raise an issue on the GitHub page.
In the next post we will take a look at Sonarr.
Kieran Jacobsen