首页 文章

用于将模块拆分为函数的Powershell脚本

提问于
浏览
0

有人可以提供PowerShell脚本将模块(.psm1)拆分为函数(.ps1) .

使用“Get-Content”我能够读取.psm1文件的内容,但我无法将其导出到函数中 .

1 回答

  • 1

    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
    
    }
    

相关问题