我正在尝试找出一种为 20-30 个文件异步调用 Powershell cmdlet 的有效方法。尽管下面的代码可以正常工作,但 Import-Module 步骤会针对每个处理的文件运行。不幸的是,这个模块需要 3 到 4 秒才能导入。
在网上搜索我可以找到对 RunspacePools 和 InitialSessionState 的引用,但在尝试创建 CreateRunspacePool 重载中所需的 PSHost 对象时遇到了问题。
任何帮助,将不胜感激。
谢谢
加文
.
.
我的应用程序中的代码示例:
我正在使用 Parallel ForEach 在线程之间分配文件。
Parallel.ForEach(files, (currentFile) =>
{
ProcessFile(currentfile);
});
private void ProcessFile(string filepath)
{
//
// Some non powershell related code removed for simplicity
//
// Start PS Session, Import-Module and Process file
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("param($path) Import-Module MyModule; Process-File -Path $path");
PowerShellInstance.AddParameter("path", filepath);
PowerShellInstance.Invoke();
}
}