[玩转系统] 构建 Inception 风格的 PowerShell 模块
作者:精品下载站 日期:2024-12-14 08:05:13 浏览:15 分类:玩电脑
构建 Inception 风格的 PowerShell 模块
在过去一周左右的时间里,我一直在分享用于使用 PowerShell 函数和脚本的 PowerShell 函数和脚本。我展示了将函数导出到脚本文件的 PowerShell 函数以及将脚本转换为函数的代码。这一切都非常像 Inception。总而言之,我想分享一个我编写的 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)"
我对脚本进行了参数化,以便将其变成可重用的工具。但目前,参数值是硬编码的,以节省我的打字时间。
该脚本有几个阶段。首先,它使用我共享的命令创建模块结构,以导出和导入模型模块目录结构。其次,该脚本获取文件数组并将所有函数导出到单独的文件中。第三,它创建一个根模块,并且该模块清单使用导出函数中的信息。接下来,该脚本创建帮助文档并最终初始化 git。如果我没有使用 -WhatIf 运行脚本,新模块将在 VSCode 中打开。
我已经运行了这个脚本并开始处理该模块,但我希望您看到输出,所以这是使用临时文件夹和 -Whatif。
在这里你可以看到 VSCode 中的结果。整个过程持续了大约3秒。我肯定会开发这个想法并亲自使用它。我经常从独立的脚本文件开始,最终我决定将其转换为模块。如果有一套工具来加速这一过程,那就太好了。
同时,请随意使用我分享的任何代码来构建您自己的 PowerShell 工具来构建 PowerShell 工具。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag