【问题标题】:Powershell Script to Split Module to Functions将模块拆分为函数的 Powershell 脚本
【发布时间】:2016-10-26 16:02:27
【问题描述】:

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

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

【问题讨论】:

    标签: powershell


    【解决方案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
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      相关资源
      最近更新 更多