1

我有一个自定义 dll,现在在我的多线程代码中,我通过以下方式引用此 dll:

……

$threads = 6

for ($i2 = 0; $i2 -lt $threads)
    {
        Copy-Item myCmdlet.dll $i2.dll
        $i2++
    }
for ($i = 0; $i -lt $threads) 
{
    $jobs += Start-job -ScriptBlock $ScriptBlock -ArgumentList ($i)
}

……

$ScriptBlock = {
    param($i)
            Installutil $i.dll
            Get-PSSnapIn -Registered
            Add-PSSnapIn MyCmdletSet

            $result = Get-MyCmdlet
            ....
}

……

这会很烦人,因为我最终可能会得到许多原始 dll 的副本。在多线程机制中调用dll还有其他方法吗?

感谢所有答案!

4

1 回答 1

2

如果您使用的是 PowerShell v2 或更高版本,则不再需要安装/注册管理单元。只需使用Import-Module -Path <path-to-dll>. 所以你的脚本块看起来像:

$ScriptBlock = {
    param($i)

    Import-Module c:\somepath\mycmdlet.dll

    $result = Get-MyCmdlet
    ....
}
于 2013-06-10T17:49:19.697 回答