は、なぜあなたは明示的にどのような機能をどのようなスクリプトとを呼び出すために言って、あなたのメインスクリプトの設定ファイルを使用していないだろうか?このような何か(警告:これはコピー/貼り付け、私が書いた何かからコードを適合されているが、いくつかの不具合が含まれているかもしれませんが、これはあなたの一般的な考え方います):あなたのメインスクリプトで
<?xml version="1.0"?>
<configuration>
<Plugins>
<Plugin Path="c:\blah\plugin1.ps1" PowerShellFunction="My-Plugin-Function" />
</Plugins>
</configuration>
:
function Load-Plugins
{
param (
[parameter(Mandatory = $true)][xml] $config,
[parameter(Mandatory = $true)][string] $nodeType
)
$plugins = @{}
foreach ($pluginNode in $config.SelectNodes($nodeType))
{
if ($pluginNode)
{
$Path = $pluginNode.Path
$powerShellFunction = $pluginNode.PowerShellFunction
$plugin = New-Object Object |
Add-Member -MemberType NoteProperty -Name "Path" -Value $Path -PassThru |
Add-Member -MemberType NoteProperty -Name "PowerShellFunction" -Value $powerShellFunction -PassThru
$plugins[$Path] = $plugin
}
}
return $plugins
}
function Execute-Plugins
{
param (
[parameter(Mandatory = $true)][hashtable] $plugins
)
$Error.Clear()
if (!$plugins.Values)
{ return }
foreach ($plugin in $plugins.Values)
{
& .\$plugin.Path
Invoke-Expression "$($plugin.PowerShellFunction)"
}
}
function Load-Script-Config
{
param (
[parameter(Mandatory = $false)][string] $configFile
)
if (!$configFile)
{ $configFile = (Get-PSCallStack)[1].Location.Split(':')[0].Replace(".ps1", ".config") }
return [xml](Get-Content $configFile)
}
$pluginConfig = Load-Script-Config
$plugins = Load-Plugins $config "configuration/Plugins/Plugin"
Execute-Plugins $plugins
を
私のプロトタイプをテストし続けると少し難しいことの1つは、衝突が起こる可能性があるため、関数の命名です...また、関数をロードし、スクリプトファイルに "フリー"なコードを実行しない方が望ましいです。 – carlpett