Tasks is a project to demonstrate 'good practices' when working with PowerShell. It includes:
- Demonstration of using modules to bootstrap loading of dependencies
- Layout of public and private functions
- Testing using Pester
- Build and test using Github actions
Logging and configuration are also implemented. Refer to the docs pages to learn more about the implementation of these.
Use the ServerTasks
framework to implement your own task-based applications to get the benefits of logging, testing, configuration, and a modular architecture.
Build your Public tasks and wrap them with controller tasks in a \Tasks
folder and then call your tasks via:
- Configured as Windows Scheduled Tasks
- Octopus runbooks
- From a PowerShell console on a host
- From a CI build process
The ServerTasks
module has the following dependencies that need to be installed on your development machine.
Note: Use the scope of CurrentUser to avoid needing administrator privileges when installing modules.
Install-Module Pester -Force -Scope CurrentUser -SkipPublisherCheck
Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser
Refer to the docs for additional information on deploying the ServerTasks module.
The module is covered by unit tests written using Pester. You can invoke the tests from the root folder by running the following command.
Invoke-Pester -Output Detailed
You can also run static code analysis checks locally by running the following command.
Invoke-ScriptAnalyzer -Path .\ServerTasks\ -Recurse
There is a sample named Invoke-MoveDeploymentFolder
that reads from a configuration file and then calls the Move-DeploymentFolder module command.
To use it create a folder at the location specified by SourceFolder in the configuration file
{
"SourceFolder": "\\temp\\source-folder",
"TargetFolder": "\\temp\\target-folder",
"ProcessName": "CalculatorApp"
}
You can then run the following command to invoke the sample
.\Invoke-MoveDeploymentFolder.ps1
After running the command, the SourceFolder
should be copied to the TargetFolder
location.
Test that the TargetFolder
is not created when a given proces is running. Start Calculator
and re-run the command. You should see that the
source folder is not copied while the configured ProcessName
process is running.
Creating and working with modules
- PowerShellGallery Publishing Guidelines and Best Practices
- Understanding and Building PowerShell Modules
Using Pester for testing PS
- Pester on Github (example showing build server integration and code coverage reporting)
- Mocking
- Advanced usage e.g. running Unit, Integration, and Acceptance tests
- Should Operators and Custom Assertions
Useful Github repos