[玩转系统] 将 PowerShell 函数导出到文件
作者:精品下载站 日期:2024-12-14 08:04:52 浏览:13 分类:玩电脑
将 PowerShell 函数导出到文件
当我编写 PowerShell 模块时,它通常包含多个导出函数。在哪里存储模块函数是一个很好的讨论话题,我认为不一定有适合每个人的最佳实践。我认为这可能取决于功能的数量和复杂性。其他人是否向该模块贡献代码?你如何使用 Pester?这些只是需要考虑的几个问题。在我的工作中,有时函数位于 .psm1 文件中。有些项目有一个具有多种功能的 public.ps1 文件。在更复杂的项目中,例如我的 PSScriptTools 模块,每个函数都有一个单独的文件。
当我决定将单个文件中定义的所有函数分开时,我的挑战就出现了。我希望每个函数都驻留在自己的文件中,使用函数名作为文件名。我厌倦了手动执行此操作,因此编写了一个 PowerShell 函数来帮我完成这项工作。
使用 AST
PowerShell 使用称为“抽象语法树”或 AST 的编码概念。 AST 使得解析 PowerShell 表达式甚至文件成为可能。使用 AST 绝对是高级 PowerShell 脚本技巧。
New-Variable astTokens -Force
New-Variable astErr -Force
$AST = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$astTokens, [ref]$astErr)
根据您要抽象的内容,您也许能够从立即解析中准确地获得您想要的内容。就我而言,我需要在 AST 结果中搜索特定类型的命令元素。
$functions = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true)
在我的示例中,我在文件中找到了这些函数。
我会指出,不遵循动词-名词命名约定的函数名称是类构造函数。尽管它们也可能是私有辅助函数,通常使用非标准名称,因为它们从未公开暴露。更多关于我如何处理这个问题。
每个功能元素都是它自己的对象。
正如您所看到的,获取函数文本非常容易。如果我使用 ToString() 方法,那就更容易了。
Set-Content -path "d:\temp$($functions[-1].name).ps1" -Value $functions[-1].ToString()
测试函数名称
当我构建导出工具时,我意识到我需要一种方法来跳过可能永远不会公开暴露的功能。所以我写了一个简单的函数来测试函数名。
Function Test-FunctionName {
[CmdletBinding()]
[OutputType("boolean")]
Param(
[Parameter(Position = 0,Mandatory,HelpMessage = "Specify a function name.")]
[ValidateNotNullOrEmpty()]
[string]$Name
)
Write-Verbose "Validating function name $Name"
#Function name must first follow Verb-Noun pattern
if ($Name -match "^\w+-\w+$") {
#validate the standard verb
$verb = ($Name -split "-")[0]
Write-Verbose "Validating detected verb $verb"
if ((Get-Verb).verb -contains $verb ) {
$True
}
else {
Write-Verbose "$($Verb.ToUpper()) is not an approved verb."
$False
}
}
else {
Write-Verbose "$Name does not match the regex pattern ^\w+-\w+$"
$False
}
}
我知道这可能是一个单行命令,但我想添加一个关于函数名称失败原因的解释。该函数接受一个名称,并首先测试它是否与“Word-Word”模式匹配。如果通过,则该函数测试“动词”是否是标准动词。如果是这样,则该名称通过测试。
导出函数
是时候把这一切放在一起了。
Function Export-FunctionFromFile {
[cmdletbinding(SupportsShouldProcess)]
[alias("eff")]
[OutputType("None", "System.IO.FileInfo")]
Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "Specify the .ps1 or .psm1 file with defined functions.")]
[ValidateScript({
If (Test-Path $_ ) {
$True
}
Else {
Throw "Can't validate that $_ exists. Please verify and try again."
$False
}
})]
[ValidateScript({
If ($_ -match "\.ps(m)?1$") {
$True
}
Else {
Throw "The path must be to a .ps1 or .psm1 file."
$False
}
})]
[string]$Path,
[Parameter(HelpMessage = "Specify the output path. The default is the same directory as the .ps1 file.")]
[ValidateScript({ Test-Path $_ })]
[string]$OutputPath,
[Parameter(HelpMessage = "Export all detected functions.")]
[switch]$All,
[Parameter(HelpMessage = "Pass the output file to the pipeline.")]
[switch]$Passthru
)
Write-Verbose "Starting $($MyInvocation.MyCommand)"
#always create these variables
New-Variable astTokens -Force -WhatIf:$False
New-Variable astErr -Force -WhatIf:$False
if (-Not $OutputPath) {
#use the parent path of the file unless the user specifies a different path
$OutputPath = Split-Path -Path $Path -Parent
}
Write-Verbose "Processing $path for functions"
#the file will always be parsed regardless of WhatIfPreference
$AST = [System.Management.Automation.Language.Parser]::ParseFile($Path, [ref]$astTokens, [ref]$astErr)
#parse out functions using the AST
$functions = $ast.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $true)
if ($functions.count -gt 0) {
Write-Verbose "Found $($functions.count) functions"
Write-Verbose "Creating files in $outputpath"
Foreach ($item in $functions) {
Write-Verbose "Detected function $($item.name)"
#only export functions with standard namees or if -All is detected.
if ($All -OR (Test-FunctionName -name $item.name)) {
$newfile = Join-Path -Path $OutputPath -ChildPath "$($item.name).ps1"
Write-Verbose "Creating new file $newFile"
Set-Content -Path $newFile -Value $item.ToString() -Force
if ($Passthru -AND (-Not $WhatIfPreference)) {
Get-Item -Path $newfile
}
}
else {
Write-Verbose "Skipping $($item.name)"
}
} #foreach item
}
else {
Write-Warning "No functions detected in $Path."
}
Write-Verbose "Ending $($MyInvocation.MyCommand)"
} #end function
默认情况下,该函数将仅导出具有标准名称的函数。
如果仔细观察详细输出,您会发现检测到的非标准函数被跳过。尽管如果需要的话我可以包含所有检测到的功能。
该函数允许我为新文件指定备用位置,尽管默认位置是源文件的父目录。
源文件不受影响,尽管我想我可以修改函数以从文件中删除代码。现在,我将手动更新源文件和任何其他模块文件以反映新的函数文件。
尝试一下
我在同一个文件中拥有这两个函数,我点了源代码。尽管我可能会在将来的某个时候将这些函数添加到 PSScriptTools 模块中。导出功能假定文件是 .ps1 或 .psm1 文件。您可以看到我在代码中使用的参数验证。尝试一下,让我知道你的想法。该代码应该可以在 Windows PowerShell 和 PowerShell 7.x 中运行。
猜你还喜欢
- 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