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