11function Publish-PSMDScriptFile
22{
3- <#
3+ <#
44 . SYNOPSIS
55 Packages a script with all dependencies and "publishes" it as a zip package.
6-
6+
77 . DESCRIPTION
88 Packages a script with all dependencies and "publishes" it as a zip package.
99 By default, it will be published to the user's desktop.
1212 - Modules that are installed in the Windows folder (such as the ActiveDirectory module or other modules associated with server roles) will be ignored.
1313 - PSSnapins will be ignored
1414 - All other modules determined by the commands used will be provided from a repository, packaged in a subfolder and included in the zip file.
15-
15+
1616 If needed, the scriptfile will be modified to add the new modules folder to its list of known folders.
1717 (The source file itself will never be modified)
18-
18+
1919 Use Set-PSMDStagingRepository to create / use a local path for staging modules to provide that way.
2020 This gives you better control over the versions used and better performance.
2121 Also the ability to use this with non-public modules.
2222 Use Publish-PSMDStagedModule to transfer modules from path or another repository into your registered staging repository.
23-
23+
2424 . PARAMETER Path
2525 Path to the scriptfile to publish.
2626 The scriptfile is expected to be UTF8 encoded with BOM, otherwise some characters may end up broken.
27-
27+
2828 . PARAMETER OutPath
2929 The path to the folder where the output zip file will be created.
3030 Defaults to the user's desktop.
31-
31+
3232 . PARAMETER EnableException
3333 This parameters disables user-friendly warnings and enables the throwing of exceptions.
3434 This is less user friendly, but allows catching exceptions in calling scripts.
35-
35+
3636 . EXAMPLE
3737 PS C:\> Publish-PSMDScriptFile -Path 'C:\scripts\logrotate.ps1'
38-
38+
3939 Creates a delivery package for the logrotate.ps1 scriptfile and places it on the desktop
4040#>
4141 [CmdletBinding ()]
4444 [PsfValidateScript (' PSModuleDevelopment.Validate.File' , ErrorString = ' PSModuleDevelopment.Validate.File' )]
4545 [string ]
4646 $Path ,
47-
47+
4848 [PsfValidateScript (' PSModuleDevelopment.Validate.Path' , ErrorString = ' PSModuleDevelopment.Validate.Path' )]
4949 [string ]
5050 $OutPath = (Get-PSFConfigValue - FullName ' PSModuleDevelopment.Script.OutPath' ),
51-
51+
5252 [switch ]
5353 $EnableException
5454 )
55-
55+
5656 begin
5757 {
5858 # region Utility Functions
6464 [string ]
6565 $Path
6666 )
67-
67+
6868 $help = Get-Help $Path
6969 $modifiers = $help.alertSet.alert.Text -split " `n " | Where-Object { $_ -like " PSMD: *" } | ForEach-Object { $_ -replace ' ^PSMD: ' }
70-
70+
7171 foreach ($modifier in $modifiers )
7272 {
7373 $operation , $values = $modifier -split " :"
106106 }
107107 }
108108 }
109-
109+
110110 function Add-PSModulePath
111111 {
112112 [CmdletBinding ()]
113113 param (
114114 [string ]
115115 $Path
116116 )
117-
117+
118118 $psmodulePathCode = @'
119119
120120# Ensure modules are available
@@ -123,7 +123,7 @@ if (-not $env:PSModulePath.Contains($modulePath)) { $env:PSModulePath = "$($env:
123123
124124
125125'@
126-
126+
127127 $parsedFile = Read-PSMDScript - Path $Path
128128 $assignment = $parsedFile.Ast.FindAll ({
129129 $args [0 ] -is [System.Management.Automation.Language.AssignmentStatementAst ] -and
@@ -155,11 +155,11 @@ if (-not $env:PSModulePath.Contains($modulePath)) { $env:PSModulePath = "$($env:
155155 }
156156 }
157157 # endregion Utility Functions
158-
158+
159159 $modulesToProcess = @ {
160160 IgnoreCommand = @ ()
161- Include = @ ()
162- Exclude = @ ()
161+ Include = @ ()
162+ Exclude = @ ()
163163 }
164164 }
165165 process
@@ -171,7 +171,7 @@ if (-not $env:PSModulePath.Contains($modulePath)) { $env:PSModulePath = "$($env:
171171 {
172172 $modulesToProcess .$ ($modifier.Type ) += $modifier.Name
173173 }
174-
174+
175175 # Detect modules needed and store them
176176 try { $parsedCommands = Get-PSMDFileCommand - Path $Path - EnableException }
177177 catch
@@ -183,7 +183,7 @@ if (-not $env:PSModulePath.Contains($modulePath)) { $env:PSModulePath = "$($env:
183183 {
184184 Write-PSFMessage - Level Verbose - String ' Publish-PSMDScriptFile.Script.Command' - StringValues $command.Name , $command.Count , $command.Module
185185 if ($modulesToProcess.IgnoreCommand -contains $command.Name ) { continue }
186-
186+
187187 if (-not $command.Module -and -not $command.Internal )
188188 {
189189 Write-PSFMessage - Level Warning - String ' Publish-PSMDScriptFile.Script.Command.NotKnown' - StringValues $command.Name , $command.Count
@@ -192,15 +192,15 @@ if (-not $env:PSModulePath.Contains($modulePath)) { $env:PSModulePath = "$($env:
192192 if ($modulesToProcess.Exclude -contains " $ ( $command.Module ) " ) { continue }
193193 if ($modulesToProcess.Include -contains " $ ( $command.Module ) " ) { continue }
194194 if ($command.Module -is [System.Management.Automation.PSSnapInInfo ]) { continue }
195- if ($command.Module.ModuleBase -eq ' C:\Windows\System32\WindowsPowerShell\v1.0' ) { continue }
196- if ($command.Module.ModuleBase -eq ' C:\Program Files\PowerShell\7' ) { continue }
195+ if ($command.Module.ModuleBase -like ' C:\Windows\System32\WindowsPowerShell\v1.0* ' ) { continue }
196+ if ($command.Module.ModuleBase -like ' C:\Program Files\PowerShell\7* ' ) { continue }
197197 $modulesToProcess.Include += " $ ( $command.Module ) "
198198 }
199-
199+
200200 $tempPath = Get-PSFPath - Name Temp
201201 $newPath = New-Item - Path $tempPath - Name " PSMD_$ ( Get-Random ) " - ItemType Directory - Force
202202 $modulesFolder = New-Item - Path $newPath.FullName - Name ' Modules' - ItemType Directory - Force
203-
203+
204204 foreach ($moduleLabel in $modulesToProcess.Include | Select-Object - Unique)
205205 {
206206 if (-not $moduleLabel ) { continue }
@@ -210,13 +210,13 @@ if (-not $env:PSModulePath.Contains($modulePath)) { $env:PSModulePath = "$($env:
210210 if (Test-PSFFunctionInterrupt ) { return }
211211 }
212212 # endregion Prepare required Modules
213-
213+
214214 # Copy script file
215215 $newScript = Copy-Item - Path $Path - Destination $newPath.FullName - PassThru
216-
216+
217217 # Update script to set PSModulePath
218218 Add-PSModulePath - Path $newScript.FullName
219-
219+
220220 # Zip result & move to destination
221221 Compress-Archive - Path " $ ( $newPath.FullName ) \*" - DestinationPath (' {0}\{1}.zip' -f $OutPath , $newScript.BaseName ) - Force
222222 Remove-Item - Path $newPath.FullName - Recurse - Force - ErrorAction Ignore
0 commit comments