-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathListing 02 - Create PoshAutomator module.ps1
73 lines (69 loc) · 2.33 KB
/
Listing 02 - Create PoshAutomator module.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Listing 2 - Create PoshAutomator module
Function New-ModuleTemplate {
[CmdletBinding()]
[OutputType()]
param(
[Parameter(Mandatory = $true)]
[string]$ModuleName,
[Parameter(Mandatory = $true)]
[string]$ModuleVersion,
[Parameter(Mandatory = $true)]
[string]$Author,
[Parameter(Mandatory = $true)]
[string]$PSVersion,
[Parameter(Mandatory = $false)]
[string[]]$Functions
)
# Do not include the version path since GitHub will take care of the version controls
$ModulePath = Join-Path .\ "$($ModuleName)"
New-Item -Path $ModulePath -ItemType Directory
Set-Location $ModulePath
New-Item -Path .\Public -ItemType Directory
$ManifestParameters = @{
ModuleVersion = $ModuleVersion
Author = $Author
Path = ".\$($ModuleName).psd1"
RootModule = ".\$($ModuleName).psm1"
PowerShellVersion = $PSVersion
}
New-ModuleManifest @ManifestParameters
# Go ahead and autopopulate the functionality to import the function scripts
$File = @{
Path = ".\$($ModuleName).psm1"
Encoding = 'utf8'
}
@'
$Path = Join-Path $PSScriptRoot 'Public'
# Get all the ps1 files in the Public folder
$Functions = Get-ChildItem -Path $Path -Filter '*.ps1'
# Loop through each ps1 file
Foreach ($import in $Functions) {
Try {
Write-Verbose "dot-sourcing file '$($import.fullname)'"
# Execute each ps1 file to load the function into memory
. $import.fullname
}
Catch {
Write-Error -Message "Failed to import function $($import.name)"
}
}
'@ | Out-File @File
$Functions | ForEach-Object {
Out-File -Path ".\Public\$($_).ps1" -Encoding utf8
}
}
# Set the parameters to pass to the function
$module = @{
# The name of your module
ModuleName = 'PoshAutomator'
# The version of your module
ModuleVersion = "1.0.0.0"
# Your name
Author = "YourNameHere"
# The minimum PowerShell version this module supports
PSVersion = '5.1'
# The functions to create blank files for in the Public folder
Functions = 'Get-SystemInfo'
}
# Execute the function to create the new module
New-ModuleTemplate @module