Extend using module
It is recommended to use extensions instead of creating your own PowerShell module. Extensions are available in go-c8y-cli >= 2.30.0 and provide a more portable/native experience. They are just as easy to install and share, and can be used in any shell.
Or just check out the Tutorials.
An example how to extend the PSc8y
PowerShell module using another PowerShell module is shown in the following demo project:
The full instructions can be viewed from the above project link, however the workflow for using the project would be as follows:
Open a PowerShell (pwsh) console
pwsh
# Or if you are using PowerShell 5.1 on windows
powershellClone the project (requires git)
git clone https://github.com/reubenmiller/PSc8y.example.git
cd PSc8y.exampleImport the PowerShell module
Import-Module ./ -Force
Note: The
-Force
parameter is important when re-importing a module from a directory. If it is not used and the module has already been imported once, then any new functions or changes will not be loaded!Show a list of the commands in the module
Get-Command -Module PSc8y.example
Get help for a specific module
Get-Help Clear-OperationCollection -Full
Adding your own functions​
Add your own PowerShell functions into the
public
folder.Note: The name of the function needs to be the same as the file name!
For example: If you add a file called
Get-MyDeviceCollection.ps1
into thepublic
folder, then the file should contain the following contents:Function Get-MyDeviceCollection {
# insert your function body here
}Re-Import the module into your PowerShell session
Import-Module ./ -Force
Use your new function
Creating a new PowerShell module from the example project​
The module can be used as a template for your own PSc8y extension module. For convenience there is a script to rename the module and be used as follows:
Run the rename module script
./scripts/Invoke-RenameModule.ps1 -Name "PSc8y.mymodule" -OutputFolder "../"
Notes
You can change the -Name parameter to anything you want. By convention the module name should start with
PSc8y.
to show that the module is an extension ofPSc8y
, however this convention is not enforced.Change directory to the new module output folder, and import it into your current PowerShell session
cd "../PSc8y.mymodule"
Import-Module ./ -Force
Importing your custom module automatically when loading PowerShell​
If you want to import your module automatically, then you can add the Import-Module
statement into your profile.ps1 file. The instructions below show how to do this:
Find out where your PowerShell profile file is
$PROFILE
Edit the file in an editor.
# If you using VS Code, open it with
code $PROFILEOr just open the path from step 1 in your preferred text editor.
Add the following import statement
# import custom module (please change the path to the relevant path of your module!!!)
Import-Module "$Env:HOME/path/to/module/PSc8y.mymodule" -ForceYou need to pass the path to the folder where you saved your module.
Start a new PowerShell session
Use a command from your module