Skip to content

Latest commit

 

History

History
97 lines (79 loc) · 3.27 KB

readme.md

File metadata and controls

97 lines (79 loc) · 3.27 KB

PowerShell modules and testing

Source code and materials for powershell modules development and testing in powershell. Goal of this workshop is to get introduction how to write reusable powershell scripts, tests and share the code with your team in software development build automation. But the same concepts can be used also by administrators. There are no files with tasks solution, because we are only exploring the module features.

  • Use Run/Debug buttons in VS code to run tests
  • Use Terminal in VS code to run Powershell commands

Tasks

Explore modules

# List packages source, which contains PsGalery online repository
Get-PackageSource | ft -AutoSize
# Find example package
Find-Module posh-git
# Import Git commands support to powershell
Install-Module posh-git
Import-Module posh-git
# List paths where powershell searches for modules - observe the directory structure
$env:PSModulePath
# List commands published by the module
Get-Command -Module posh-git
# Import the module from file
Import-Module .\Hello\Hello.psm1

Create custom module

# Create dynamic in memory module and import it
# To override the already loaded module, restart powershell
# observe its commands
New-Module -Name Hello -ScriptBlock {function Hello {"Hello!"}} | Import-module
# Create new manifest file and edit its content mainly: RootModule, FileList, FunctionsToExport
New-ModuleManifest .\Hello\Hello.psd1
# Edit the file and test the module definition
Test-ModuleManifest .\Hello\Hello.psd1

Write tests

Import-Module pester
Get-Command -Module pester
# Create script with tests creates two ps1 files
New-Fixture
# Invoke the tests from command line
Invoke-Pester
# Play with the asserts and before and after each
# Check the code coverage
Invoke-Pester -CodeCoverage *.*
# Generates NUnitXml file
Invoke-Pester -OutputFile testoutput.xml -Show Summary,Failed

Automate using Psake

# make the psake available
Import-Module psake
# Execute the default script file with "default" task
Invoke-Psake
# Run customized build
Invoke-psake -buildFile .\psakefile.ps1 -task FailBuild

Share your Modules

# Register new repository
New-Item C:\PsRepository -ItemType Directory -Force
Register-PSRepository -Name "Custom" -SourceLocation C:\PsRepository -PublishLocation C:\PsRepository -InstallationPolicy Trusted
# Publish the module module needs to be placed inside directory with the same name as the module script
Publish-Module -Path .\Hello -Repository "Custom"
# observe the repository directory content
# Use all other commands to find, list, install or import your module

Resources

Tools used

Links