การสร้างรูปแบบการเริ่มต้นโมดูล PowerShell
ในช่วงสัปดาห์ที่ผ่านมา ฉันได้แชร์ฟังก์ชันและสคริปต์ของ PowerShell สำหรับการทำงานกับฟังก์ชันและสคริปต์ของ PowerShell ฉันแสดงฟังก์ชัน PowerShell เพื่อส่งออกฟังก์ชันไปยังไฟล์สคริปต์และโค้ดเพื่อแปลงสคริปต์เป็นฟังก์ชัน ทั้งหมดนี้มีลักษณะเหมือนการเริ่มต้นอย่างมาก เพื่อสรุปทั้งหมดนี้ ฉันคิดว่าฉันจะแชร์สคริปต์ PowerShell ที่ฉันเขียนเพื่อแปลงโค้ดทั้งหมดที่ฉันแชร์เป็นโมดูลใหม่ ในที่สุดฉันจะเผยแพร่ PSFunctionTools ไปยัง PowerShell Gallery และฉันไม่เห็นเหตุผลว่าทำไมจึงไม่ทำงานใน Windows PowerShell หรือ PowerShell 7 แม้แต่ข้ามแพลตฟอร์ม
สคริปต์อาจเป็นพื้นฐานสำหรับฟังก์ชันเพิ่มเติมด้วยซ้ำ หรือฉันอาจรวมไว้เป็นตัวอย่างอ้างอิงในโมดูลใหม่ สิ่งที่ทำให้ฉันคิดผิดคือฉันใช้ฟังก์ชันในโค้ดกับตัวเองเพื่อสร้างสิ่งใหม่ ฉันไม่คาดหวังว่าคุณจะสามารถเรียกใช้สคริปต์นี้ได้เนื่องจากมันถูกเขียนขึ้นสำหรับสภาพแวดล้อมและไฟล์ของฉัน ฉันโพสต์โค้ดทั้งหมดแล้วเมื่อสัปดาห์ที่แล้ว แต่ไฟล์ของฉันอาจแตกต่างจากของคุณ
สคริปต์นี้อิงจากสคริปต์ที่ฉันโพสต์เป็นการพิสูจน์แนวคิดสำหรับการแปลงสคริปต์เป็นโมดูล PowerShell สคริปต์ของฉันจะเริ่มต้นโปรเจ็กต์ใหม่ใน git และสร้างเอกสารช่วยเหลือเบื้องต้นโดยใช้โมดูล Platyps
#requires -version 5.1
#requires -module Platyps
#Export functions from files and create a new module
[cmdletbinding(SupportsShouldProcess)]
Param(
[Parameter(position = 0,HelpMessage = "What is the name of the new module?")]
[ValidateNotNullOrEmpty()]
[string]$NewModuleName = "PSFunctionTools",
[Parameter(Position = 1,HelpMessage = "What is the parent path for the new module?")]
[ValidateNotNullOrEmpty()]
[ValidateScript({Test-Path $_})]
[string]$ParentPath = "C:\Scripts",
[Parameter(HelpMessage = "Enter an module description.")]
[string]$Description = "A set of PowerShell commands for working with PowerShell scripts and functions.",
[Parameter(HelpMessage ="PowerShell script files with functions to export.")]
[ValidateNotNullOrEmpty()]
[ValidateScript({Test-Path $_})]
[string[]]$Files = $(
'C:\scripts\Convert-FunctionToFile.ps1',
'C:\scripts\new-folderlayout.ps1',
'C:\scripts\dev-scripttofunction.ps1'
)
)
Write-Verbose "Starting $($MyInvocation.MyCommand)"
<#
dot source the conversion functions
C:\scripts\Convert-FunctionToFile.ps1
Test-FunctionName
Get-FunctionName
Get-FunctionAlias
Export-FunctionFromFile
C:\scripts\New-FolderLayout.ps1
Import-ModuleLayout
Export-ModuleLayout
#>
. C:\scripts\Convert-FunctionToFile.ps1
. C:\scripts\New-FolderLayout.ps1
#the new module location
$path = Join-path -Path $ParentPath -ChildPath $NewModuleName
$export = [System.Collections.Generic.list[object]]::New()
$aliases = @()
#the layout was created using Export-ModuleLayout
$layout = "C:\scripts\ModuleLayout.json"
Write-Verbose "Creating the module structure"
Import-ModuleLayout -Name $NewModuleName -ParentPath $ParentPath -Layout $layout
#I removed the parameter validation on the target path
$functionFiles = $files | ForEach-Object {
Write-Verbose "Processing $_"
Export-FunctionFromFile -Path $_ -OutputPath $Path\functions\public -All -Passthru
#get aliases
if ($pscmdlet.ShouldProcess($_,"Getting function aliases")) {
$aliases += Get-FunctionAlias -path $_ | Select-Object -ExpandProperty alias
}
}
if ($functionFiles) {
$export.AddRange($functionFiles.baseName)
}
#create the root module
$psm1 = @"
Get-Childitem `$psscriptroot\functions\*.ps1 -recurse |
Foreach-Object {
. `$_.FullName
}
"@
Write-Verbose "Creating root module $path\$newmodulename.psm1"
$psm1 | Out-File "$path\$newmodulename.psm1"
#create the module manifest
$splat = @{
Path = "$path\$newmodulename.psd1"
RootModule = "$newmodulename.psm1"
ModuleVersion = "0.1.0"
Author = "Jeff Hicks"
CompanyName = "JDH Information Technology Solutions, Inc."
Copyright = "(c) 2021 JDH Information Technology Solutions, Inc."
Description = $Description
CmdletsToExport = @()
VariablesToExport = @()
FunctionsToExport = $Export
AliasesToExport = $aliases
PowerShellVersion = "5.1"
CompatiblePSEditions = "Desktop","Core"
}
Write-Verbose "Creating module manifest $($splat.path)"
New-ModuleManifest @splat
#this requires the Platyps module
Write-Verbose "Creating module help files"
if ($PSCmdlet.ShouldProcess("docs","create markdown help files")) {
Import-Module $splat.path
New-MarkdownHelp -Module $NewModuleName -OutputFolder $path\docs
New-ExternalHelp -Path $path\docs -OutputPath $path\en-us
}
Write-Verbose "Initializing git"
if ($PSCmdlet.ShouldProcess($path, "git initialize")) {
Set-Location $path
git init
git add .
git commit -m "initial files"
git checkout -b $splat.ModuleVersion
}
if (-not $WhatIfPreference) {
Get-ChildItem $path -Recurse
Try {
[void](Get-Command -name code.cmd -ErrorAction stop)
Write-Verbose "Opening module in VSCode"
code $path
}
Catch {
Write-Warning "VS Code not found."
}
}
Write-Verbose "Ending $($MyInvocation.MyCommand)"ฉันได้กำหนดพารามิเตอร์ของสคริปต์ด้วยแนวคิดที่ว่าฉันสามารถเปลี่ยนสิ่งนี้ให้เป็นเครื่องมือที่นำมาใช้ซ้ำได้ แต่สำหรับตอนนี้ ค่าพารามิเตอร์เป็นแบบฮาร์ดโค้ดเพื่อช่วยฉันในการพิมพ์
สคริปต์มีหลายขั้นตอน ขั้นแรก สร้างโครงสร้างโมดูลโดยใช้คำสั่งที่ฉันแชร์เพื่อส่งออกและนำเข้าโครงสร้างไดเร็กทอรีโมดูลโมเดล ประการที่สอง สคริปต์ใช้อาร์เรย์ของไฟล์และส่งออกฟังก์ชันทั้งหมดเพื่อแยกไฟล์ ประการที่สาม สร้างโมดูลรูท และรายการโมดูลโดยใช้ข้อมูลจากฟังก์ชันที่ส่งออก จากนั้น สคริปต์จะสร้างเอกสารวิธีใช้และเริ่มต้นคอมไพล์ในที่สุด ถ้าฉันไม่ได้รันสคริปต์ด้วย -WhatIf โมดูลใหม่จะถูกเปิดใน VSCode
ฉันได้รันสคริปต์นี้แล้วและเริ่มทำงานกับโมดูลนี้ แต่ฉันต้องการให้คุณเห็นผลลัพธ์ ดังนั้นนี่คือการใช้โฟลเดอร์ชั่วคราวและ -Whatif
ที่นี่คุณสามารถดูผลลัพธ์ใน VSCode กระบวนการทั้งหมดใช้เวลาประมาณ 3 วินาที ฉันจะพัฒนาแนวคิดนี้และใช้มันเองอย่างแน่นอน ฉันมักจะเริ่มต้นด้วยไฟล์สคริปต์แบบสแตนด์อโลนซึ่งในที่สุดฉันก็ตัดสินใจเปลี่ยนเป็นโมดูล คงจะดีถ้ามีชุดเครื่องมือเพื่อเร่งกระบวนการนั้น
ในระหว่างนี้ คุณสามารถใช้โค้ดใดก็ได้ที่ฉันแชร์เพื่อสร้างเครื่องมือ PowerShell ของคุณเองสำหรับการสร้างเครื่องมือ PowerShell


