2

我一直在尝试为 powershell 编写一个 cmd-let,它允许我运行它并将多个模块一次导入到我正在运行的任何脚本中。我以为我让它工作了,但现在它似乎停止了。我只是想知道这是否可能,似乎是这样。

为导入运行的命令:

Import-Module .\Tools\Import-Tools.ps1 
$Tools = Import-Tools -ToolsDirectory \$PathToToolsDirectory
$Tools 

导入功能:

Function Import-Tools
{
    param 
    (
        [Parameter(Mandatory=$true)]
        [string]$ToolsDirectory
    ) 

    # Make sure the path is absolute (Borrowed from Michael Wanke)
    if (!(Split-Path $ToolsDirectory -IsAbsolute))
    {
        $ToolsDirectory = Join-Path $pwd $ToolsDirectory
    }


    #Verifies that the tools directory exists.
    while ((Test-Path -Path $ToolsDirectory) -eq $False)
    {
        $ToolsDirectory = Read-Host "Incorrect tool path entered. Please enter one now or exit"
    }

    #Create $Tools variable containing multiple lines for import.
    foreach ($Tool in Get-Childitem $ToolsDirectory -Name -Filter "*.ps1")
    {
        [string] $Tools= $Tools + "`r`nImport-Module $ToolsDirectory\$Tool"
    }

        Write-Output $Tools
        return $Tools

}

任何帮助或建议将不胜感激。

4

2 回答 2

3

您可以使用Import-Modulecmdlet 导入多个模块:

# import multiple modules by using the name of the module
Import-Module -Name WebAdministration, ActiveDirectory

# import multiple modules by using a path
Import-Module -Name c:\moduleA.psm1, c:\moduleB.psm1
于 2012-08-11T11:14:16.237 回答
0

这段代码对我有用:

foreach ($module in Get-Childitem $modulesFolder -Name -Filter "*.psm1")
{
    Import-Module $modulesFolder\$module
}
于 2018-08-30T22:28:23.437 回答