|
| 1 | +function Publish-PSBuildModule { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Publishes a module to the defined PowerShell repository. |
| 5 | + .DESCRIPTION |
| 6 | + Publishes a module to the defined PowerShell repository. |
| 7 | + .PARAMETER Path |
| 8 | + The path to the module to publish. |
| 9 | + .PARAMETER Version |
| 10 | + The version of the module to publish. |
| 11 | + .PARAMETER Repository |
| 12 | + The PowerShell repository name to publish to. |
| 13 | + .PARAMETER ApiKey |
| 14 | + The API key to use to authenticate to the PowerShell repository with. |
| 15 | + .PARAMETER Credential |
| 16 | + The credential to use to authenticate to the PowerShell repository with. |
| 17 | + .EXAMPLE |
| 18 | + PS> Publish-PSBuildModule -Path .\Output\0.1.0\MyModule -Version 0.1.0 -Repository PSGallery -ApiKey 12345 |
| 19 | +
|
| 20 | + Publish version 0.1.0 of the module at path .\Output\0.1.0\MyModule to the PSGallery repository using an API key. |
| 21 | + .EXAMPLE |
| 22 | + PS> Publish-PSBuildModule -Path .\Output\0.1.0\MyModule -Version 0.1.0 -Repository PSGallery -Credential $myCred |
| 23 | +
|
| 24 | + Publish version 0.1.0 of the module at path .\Output\0.1.0\MyModule to the PSGallery repository using a PowerShell credential. |
| 25 | + #> |
| 26 | + [cmdletbinding(DefaultParameterSetName = 'ApiKey')] |
| 27 | + param( |
| 28 | + [parameter(Mandatory)] |
| 29 | + [ValidateScript({ |
| 30 | + if (-not (Test-Path -Path $_ )) { |
| 31 | + throw 'Folder does not exist' |
| 32 | + } |
| 33 | + if (-not (Test-Path -Path $_ -PathType Container)) { |
| 34 | + throw 'The Path argument must be a folder. File paths are not allowed.' |
| 35 | + } |
| 36 | + $true |
| 37 | + })] |
| 38 | + [System.IO.FileInfo]$Path, |
| 39 | + |
| 40 | + [parameter(Mandatory)] |
| 41 | + [string]$Version, |
| 42 | + |
| 43 | + [parameter(Mandatory)] |
| 44 | + [string]$Repository, |
| 45 | + |
| 46 | + [parameter(Mandatory, ParameterSetName = 'ApiKey')] |
| 47 | + [string]$ApiKey, |
| 48 | + |
| 49 | + [parameter(Mandatory, ParameterSetName = 'Credential')] |
| 50 | + [pscredential]$Credential |
| 51 | + ) |
| 52 | + |
| 53 | + Write-Verbose "Publishing version [$Version] to repository [$Repository]..." |
| 54 | + |
| 55 | + $publishParams = @{ |
| 56 | + Path = $Path |
| 57 | + Repository = $Repository |
| 58 | + WhatIf = $true |
| 59 | + } |
| 60 | + switch ($PSCmdlet.ParameterSetName) { |
| 61 | + 'Credential' { $publishParams.Credential = $Credential } |
| 62 | + 'ApiKey' { $publishParams.NuGetApiKey = $ApiKey } |
| 63 | + } |
| 64 | + Publish-Module @publishParams |
| 65 | +} |
0 commit comments