https://github.com/mmaitre314/PowerShellGet-Test-Binary-Module
Example of PowerShellGet Binary Module
https://github.com/mmaitre314/PowerShellGet-Test-Binary-Module
Last synced: 5 months ago
JSON representation
Example of PowerShellGet Binary Module
- Host: GitHub
- URL: https://github.com/mmaitre314/PowerShellGet-Test-Binary-Module
- Owner: mmaitre314
- License: apache-2.0
- Created: 2016-03-21T07:10:13.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-03-21T17:39:26.000Z (about 9 years ago)
- Last Synced: 2024-08-03T22:17:18.507Z (8 months ago)
- Language: C#
- Homepage:
- Size: 19.5 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - mmaitre314/PowerShellGet-Test-Binary-Module - Example of PowerShellGet Binary Module (C# #)
README
[](https://www.powershellgallery.com/packages/PowerShellGet-Test-Binary-Module/)
A minimalist example of [PowerShell binary module](https://msdn.microsoft.com/en-us/library/dd878342(v=vs.85).aspx) (.NET DLL) that can be published to the [PowerShell gallery](https://www.powershellgallery.com/) as [PowerShellGet](https://technet.microsoft.com/library/dn807169.aspx) package (aka NuGet).
To install the package, run:
```
Install-Module -Name PowerShellGet-Test-Binary-Module -Scope CurrentUser
```The module contains a single `Write-HelloWorld` cmdlet, whose name is self-explanatory:
```
using System.Management.Automation;[Cmdlet(VerbsCommunications.Write, "HelloWorld")]
public class HelloWorldCmdlet : Cmdlet
{
protected override void BeginProcessing()
{
WriteObject("Hello, World!");
}
}
```(the `System.Management.Automation` reference is found at `c:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll`)
The module manifest stores NuGet-style package info:
```
@{
RootModule = 'PowerShellGet-Test-Binary-Module.dll'
ModuleVersion = '1.0.0.3'
CmdletsToExport = '*'
GUID = '95ee4cf1-d508-45a8-9680-203b71453f98'
DotNetFrameworkVersion = '4.5.1'
Author = 'Matthieu Maitre'
Description = 'Example of PowerShellGet Binary Module'
CompanyName = 'None'
Copyright = '(c) 2016 Matthieu Maitre. All rights reserved.'
PrivateData = @{
PSData = @{
ProjectUri = 'https://github.com/mmaitre314/PowerShellGet-Test-Binary-Module'
LicenseUri = 'https://github.com/mmaitre314/PowerShellGet-Test-Binary-Module/blob/master/LICENSE'
ReleaseNotes = ''
}
}
}
```And a couple of scripts (`Publish-ToTestRepo.ps1`, `Publish-ToPowerShellGallery.ps1`) automate publication.
Unit-testing cmdlets requires a couple of tricks:
- The PowerShell host needs to be mocked (`ICommandRuntime`)
- Protected methods need to be accessed using `PrivateObject````
[TestMethod]
public void Test()
{
var mock = new Mock();var cmdlet = new HelloWorldCmdlet
{
CommandRuntime = mock.Object
};
new PrivateObject(cmdlet).Invoke("BeginProcessing");mock.Verify(runtime => runtime.WriteObject("Hello, World!"), Times.Once);
}
```This code was heavily inspired from [AnPur's PowerShellGet-Module](https://github.com/anpur/powershellget-module).