【发布时间】:2016-10-26 16:02:27
【问题描述】:
有人可以提供 PowerShell 脚本来将模块 (.psm1) 拆分为函数 (.ps1)。
使用“Get-Content”我可以读取 .psm1 文件的内容,但无法将其导出到函数中。
【问题讨论】:
标签: powershell
有人可以提供 PowerShell 脚本来将模块 (.psm1) 拆分为函数 (.ps1)。
使用“Get-Content”我可以读取 .psm1 文件的内容,但无法将其导出到函数中。
【问题讨论】:
标签: powershell
以下 PowerShell 脚本会将模块 (.psm1) 拆分为函数 (.ps1)。
$ModuleName = 'Module1' #Specify the module name
$SplittedFunctionPath = "D:\SplittedFunction\" #Specify Splitted function path
#Import-Module
Import-Module $ModuleName
#Function to split the module and export it as functions
Function Insert-Content
{
param ( [String]$Path )
process
{
$( ,$_; Get-Content $Path -ea SilentlyContinue) | Out-File $Path
}
}
$FunctionName = Get-Command -Module $ModuleName
$path ="$SplittedFunctionPath"
Foreach ($Function in $FunctionName.Name)
{
(get-command $Function).definition | out-file $Path\$Function.ps1
"Function $Function `n {" | Insert-Content $Path\$Function.ps1
"}" | Add-Content $Path\$Function.ps1
}
【讨论】: