1
1
function Publish-PSMDScriptFile
2
2
{
3
- <#
3
+ <#
4
4
. SYNOPSIS
5
5
Packages a script with all dependencies and "publishes" it as a zip package.
6
-
6
+
7
7
. DESCRIPTION
8
8
Packages a script with all dependencies and "publishes" it as a zip package.
9
9
By default, it will be published to the user's desktop.
12
12
- Modules that are installed in the Windows folder (such as the ActiveDirectory module or other modules associated with server roles) will be ignored.
13
13
- PSSnapins will be ignored
14
14
- 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
+
16
16
If needed, the scriptfile will be modified to add the new modules folder to its list of known folders.
17
17
(The source file itself will never be modified)
18
-
18
+
19
19
Use Set-PSMDStagingRepository to create / use a local path for staging modules to provide that way.
20
20
This gives you better control over the versions used and better performance.
21
21
Also the ability to use this with non-public modules.
22
22
Use Publish-PSMDStagedModule to transfer modules from path or another repository into your registered staging repository.
23
-
23
+
24
24
. PARAMETER Path
25
25
Path to the scriptfile to publish.
26
26
The scriptfile is expected to be UTF8 encoded with BOM, otherwise some characters may end up broken.
27
-
27
+
28
28
. PARAMETER OutPath
29
29
The path to the folder where the output zip file will be created.
30
30
Defaults to the user's desktop.
31
-
31
+
32
32
. PARAMETER EnableException
33
33
This parameters disables user-friendly warnings and enables the throwing of exceptions.
34
34
This is less user friendly, but allows catching exceptions in calling scripts.
35
-
35
+
36
36
. EXAMPLE
37
37
PS C:\> Publish-PSMDScriptFile -Path 'C:\scripts\logrotate.ps1'
38
-
38
+
39
39
Creates a delivery package for the logrotate.ps1 scriptfile and places it on the desktop
40
40
#>
41
41
[CmdletBinding ()]
44
44
[PsfValidateScript (' PSModuleDevelopment.Validate.File' , ErrorString = ' PSModuleDevelopment.Validate.File' )]
45
45
[string ]
46
46
$Path ,
47
-
47
+
48
48
[PsfValidateScript (' PSModuleDevelopment.Validate.Path' , ErrorString = ' PSModuleDevelopment.Validate.Path' )]
49
49
[string ]
50
50
$OutPath = (Get-PSFConfigValue - FullName ' PSModuleDevelopment.Script.OutPath' ),
51
-
51
+
52
52
[switch ]
53
53
$EnableException
54
54
)
55
-
55
+
56
56
begin
57
57
{
58
58
# region Utility Functions
64
64
[string ]
65
65
$Path
66
66
)
67
-
67
+
68
68
$help = Get-Help $Path
69
69
$modifiers = $help.alertSet.alert.Text -split " `n " | Where-Object { $_ -like " PSMD: *" } | ForEach-Object { $_ -replace ' ^PSMD: ' }
70
-
70
+
71
71
foreach ($modifier in $modifiers )
72
72
{
73
73
$operation , $values = $modifier -split " :"
106
106
}
107
107
}
108
108
}
109
-
109
+
110
110
function Add-PSModulePath
111
111
{
112
112
[CmdletBinding ()]
113
113
param (
114
114
[string ]
115
115
$Path
116
116
)
117
-
117
+
118
118
$psmodulePathCode = @'
119
119
120
120
# Ensure modules are available
@@ -123,7 +123,7 @@ if (-not $env:PSModulePath.Contains($modulePath)) { $env:PSModulePath = "$($env:
123
123
124
124
125
125
'@
126
-
126
+
127
127
$parsedFile = Read-PSMDScript - Path $Path
128
128
$assignment = $parsedFile.Ast.FindAll ({
129
129
$args [0 ] -is [System.Management.Automation.Language.AssignmentStatementAst ] -and
@@ -155,11 +155,11 @@ if (-not $env:PSModulePath.Contains($modulePath)) { $env:PSModulePath = "$($env:
155
155
}
156
156
}
157
157
# endregion Utility Functions
158
-
158
+
159
159
$modulesToProcess = @ {
160
160
IgnoreCommand = @ ()
161
- Include = @ ()
162
- Exclude = @ ()
161
+ Include = @ ()
162
+ Exclude = @ ()
163
163
}
164
164
}
165
165
process
@@ -171,7 +171,7 @@ if (-not $env:PSModulePath.Contains($modulePath)) { $env:PSModulePath = "$($env:
171
171
{
172
172
$modulesToProcess .$ ($modifier.Type ) += $modifier.Name
173
173
}
174
-
174
+
175
175
# Detect modules needed and store them
176
176
try { $parsedCommands = Get-PSMDFileCommand - Path $Path - EnableException }
177
177
catch
@@ -183,7 +183,7 @@ if (-not $env:PSModulePath.Contains($modulePath)) { $env:PSModulePath = "$($env:
183
183
{
184
184
Write-PSFMessage - Level Verbose - String ' Publish-PSMDScriptFile.Script.Command' - StringValues $command.Name , $command.Count , $command.Module
185
185
if ($modulesToProcess.IgnoreCommand -contains $command.Name ) { continue }
186
-
186
+
187
187
if (-not $command.Module -and -not $command.Internal )
188
188
{
189
189
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:
192
192
if ($modulesToProcess.Exclude -contains " $ ( $command.Module ) " ) { continue }
193
193
if ($modulesToProcess.Include -contains " $ ( $command.Module ) " ) { continue }
194
194
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 }
197
197
$modulesToProcess.Include += " $ ( $command.Module ) "
198
198
}
199
-
199
+
200
200
$tempPath = Get-PSFPath - Name Temp
201
201
$newPath = New-Item - Path $tempPath - Name " PSMD_$ ( Get-Random ) " - ItemType Directory - Force
202
202
$modulesFolder = New-Item - Path $newPath.FullName - Name ' Modules' - ItemType Directory - Force
203
-
203
+
204
204
foreach ($moduleLabel in $modulesToProcess.Include | Select-Object - Unique)
205
205
{
206
206
if (-not $moduleLabel ) { continue }
@@ -210,13 +210,13 @@ if (-not $env:PSModulePath.Contains($modulePath)) { $env:PSModulePath = "$($env:
210
210
if (Test-PSFFunctionInterrupt ) { return }
211
211
}
212
212
# endregion Prepare required Modules
213
-
213
+
214
214
# Copy script file
215
215
$newScript = Copy-Item - Path $Path - Destination $newPath.FullName - PassThru
216
-
216
+
217
217
# Update script to set PSModulePath
218
218
Add-PSModulePath - Path $newScript.FullName
219
-
219
+
220
220
# Zip result & move to destination
221
221
Compress-Archive - Path " $ ( $newPath.FullName ) \*" - DestinationPath (' {0}\{1}.zip' -f $OutPath , $newScript.BaseName ) - Force
222
222
Remove-Item - Path $newPath.FullName - Recurse - Force - ErrorAction Ignore
0 commit comments