我一直在尝试为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
}
任何帮助或建议都将不胜感激。
答案 0 :(得分:3)
您可以使用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
答案 1 :(得分:0)
此代码对我有用:
foreach ($module in Get-Childitem $modulesFolder -Name -Filter "*.psm1")
{
Import-Module $modulesFolder\$module
}