From 96be33cc66e2d3d9a0c57d545768f7037ab09c8c Mon Sep 17 00:00:00 2001 From: rulasg Date: Tue, 23 May 2023 14:29:58 +0200 Subject: [PATCH 1/2] publish.ps1 --- Docs.psd1 | 12 +++---- Docs.psm1 | 21 +++++++++-- DocsTest/DocsTest.psm1 | 4 +-- publish.ps1 | 79 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 publish.ps1 diff --git a/Docs.psd1 b/Docs.psd1 index 664dc05..01b7638 100755 --- a/Docs.psd1 +++ b/Docs.psd1 @@ -12,7 +12,7 @@ RootModule = 'Docs.psm1' # Version number of this module. -ModuleVersion = '0.1' +ModuleVersion = '2.0.0' # Supported PSEditions # CompatiblePSEditions = @() @@ -69,16 +69,16 @@ Description = 'Module to manage documents' # NestedModules = @() # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. -FunctionsToExport = '*' +# FunctionsToExport = '*' # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. -CmdletsToExport = '*' +# CmdletsToExport = '*' # Variables to export from this module -VariablesToExport = '*' +# VariablesToExport = '*' # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. -AliasesToExport = '*' +# AliasesToExport = '*' # DSC resources to export from this module # DscResourcesToExport = @() @@ -110,7 +110,7 @@ PrivateData = @{ # ReleaseNotes = '' # Prerelease string of this module - # Prerelease = '' + Prerelease = 'alpha' # Flag to indicate whether the module requires explicit user acceptance for install/update/save # RequireLicenseAcceptance = $false diff --git a/Docs.psm1 b/Docs.psm1 index ed9f124..3934a8f 100755 --- a/Docs.psm1 +++ b/Docs.psm1 @@ -14,7 +14,6 @@ CREATED: 05/26/2021 Write-Host "Loading Docs ..." -ForegroundColor DarkCyan - # Script Variables $script:StoresList = @() @@ -1146,4 +1145,22 @@ function Format-DocsName { $input | ForEach-Object { $_.Name() } } # Export-ModuleMember -Function Format-DocsName -Alias "fname" -#endregion Formats \ No newline at end of file +#endregion Formats + +#region Utils +function Add-MyMember +{ + [CmdletBinding()] + param( + [parameter(Mandatory,ValueFromPipeline)][Object] $object, + [parameter(Mandatory)][string]$NotePropertyName, + [parameter(Mandatory)][string]$NotePropertyValue + ) + + if ($Object.$NotePropertyName) { + $Object.$NotePropertyName = $NotePropertyValue + } else { + $object | Add-Member -NotePropertyName $NotePropertyName -NotePropertyValue $NotePropertyValue + } +} +#endregion Utils \ No newline at end of file diff --git a/DocsTest/DocsTest.psm1 b/DocsTest/DocsTest.psm1 index 3f6a9c2..b5dc6fd 100755 --- a/DocsTest/DocsTest.psm1 +++ b/DocsTest/DocsTest.psm1 @@ -1830,8 +1830,6 @@ function DocsTest_GetDocsName_Simple{ Assert-NotImplemented } -Export-ModuleMember -Function DocsTest_* - function SetupScenario1 () { $Evidence = New-Object 'System.Collections.Generic.Dictionary[[string],[string]]' @@ -2027,3 +2025,5 @@ function CheckDocName{ Write-AssertionDot -Color Yellow } + +Export-ModuleMember -Function DocsTest_* \ No newline at end of file diff --git a/publish.ps1 b/publish.ps1 new file mode 100644 index 0000000..1a8d30c --- /dev/null +++ b/publish.ps1 @@ -0,0 +1,79 @@ +<# +.SYNOPSIS + Publish the Module to PSGallery +.DESCRIPTION + This script will publish the module to the PSGallery +.NOTES + You will need to create a NuGet API Key for the PSGallery at https://www.powershellgallery.com/account/apikeys +.LINK + https://raw.githubusercontent.com/rulasg/DemoPsModule/main/publish.ps1 +.EXAMPLE + # Publish the module to the PSGallery without prompting + + > Publish.ps1 -Force -NuGetApiKey """ +.EXAMPLE + # Publish the module to the PSGallery using PAT on enviroment variable + + > $env:NUGETAPIKEY = + > ./publish.ps1 +#> + +[CmdletBinding( + SupportsShouldProcess, + ConfirmImpact='High' + )] +param( + # The NuGet API Key for the PSGallery + [Parameter(Mandatory=$false)] [string]$NuGetApiKey, + # Force the publish without prompting for confirmation + [Parameter(Mandatory=$false)] [switch]$Force, + # Force publishing package to the gallery. Equivalente to Import-Module -Force + [Parameter(Mandatory=$false)] [switch]$ForcePublish +) + +# check that $NuggetApiKey is null or whitespace +# If it is use environment variable $env:NugetApiKey +if ( [string]::IsNullOrWhiteSpace($NuGetApiKey) ) { + + if ( [string]::IsNullOrWhiteSpace($env:NUGETAPIKEY) ) { + Write-Error -Message '$Env:NUGETAPIKEY is not set. Try running `$Env:NUGETAPIKEY = (Find-DocsFile nugetapikey | rsk | Get-SecretData).Get()`' + return + } + + $NuGetApiKey = $env:NUGETAPIKEY +} + +# look for psd1 file on the same folder as this script +$psdPath = Get-ChildItem -Path $PSScriptRoot -Filter *.psd1 + +# check if $psd is set +if ( $null -eq $psdPath ) { + Write-Error -Message 'No psd1 file found' + return +} + +# check if $psd is a single file +if ( $psdPath.Count -gt 1 ) { + Write-Error -Message 'More than one psd1 file found' + return +} + +# Display Module Information +$psd1 = Import-PowerShellDataFile -Path $psdPath +$psd1 +$psd1.PrivateData.PSData + + +# Confirm if not forced +if ($Force -and -not $Confirm){ + $ConfirmPreference = 'None' +} + +# Publish the module with ShouldProcess (-whatif, -confirm) +if ($PSCmdlet.ShouldProcess($psdPath, "Publish-Module")) { + $message ="Publishing {0} {1} {2} to PSGallery ..." -f $($psdPath.Name), $($psd1.ModuleVersion), $($psd1.PrivateData.pSData.Prerelease) + # show an empty line + Write-Information -InformationAction Continue -Message "" + Write-Information -InformationAction Continue -Message $message + Publish-Module -Name $psdPath -NuGetApiKey $NuGetApiKey -Force:$ForcePublish +} \ No newline at end of file From 84c9a21a1e1c9105c8655e5f2cb7ee9ee8a9725d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Gonz=C3=A1lez?= Date: Wed, 24 May 2023 09:57:18 +0200 Subject: [PATCH 2/2] Security warning Add-MyMember --- Docs.psm1 | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Docs.psm1 b/Docs.psm1 index 3934a8f..9b52c0e 100755 --- a/Docs.psm1 +++ b/Docs.psm1 @@ -1156,11 +1156,13 @@ function Add-MyMember [parameter(Mandatory)][string]$NotePropertyName, [parameter(Mandatory)][string]$NotePropertyValue ) - - if ($Object.$NotePropertyName) { - $Object.$NotePropertyName = $NotePropertyValue - } else { - $object | Add-Member -NotePropertyName $NotePropertyName -NotePropertyValue $NotePropertyValue - } + process { + + if ($Object.$NotePropertyName) { + $Object.$NotePropertyName = $NotePropertyValue + } else { + $object | Add-Member -NotePropertyName $NotePropertyName -NotePropertyValue $NotePropertyValue + } + } } -#endregion Utils \ No newline at end of file +#endregion Utils