powershell - cmd-let 导入多个模块 powershell

标签 powershell import module

我一直在尝试为 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

}

如有任何帮助或建议,我们将不胜感激。

最佳答案

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

# 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

关于powershell - cmd-let 导入多个模块 powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11907330/

相关文章:

Windows 终端/Powershell 智能感知

powershell - 实现 "Press Enter to skip"

import - Dart:相对导入更改文件夹结构

perl - 如何在 Android (SL4A) 脚本层上安装 Perl 模块?

python - 意外的导入行为,以不同方式处理整数和列表

module - 如何使用模块为 Fortran 程序创建 makefile

html - 如何将自定义字体添加到 ConvertTo-HTML 的 <head> 部分?

.net - 是否有任何关于 VS 中的 Powershell 编程的文档?

python - 为什么我的脚本目录不在 Python sys.path 中?

python - 将 Excel 导入 Panda Dataframe