Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用任务并行库在C#中执行PowerShell脚本_C#_.net_Powershell_Task Parallel Library - Fatal编程技术网

如何使用任务并行库在C#中执行PowerShell脚本

如何使用任务并行库在C#中执行PowerShell脚本,c#,.net,powershell,task-parallel-library,C#,.net,Powershell,Task Parallel Library,我目前正在执行PowerShell脚本,如下所示: internal void RunScript(PowerShell powerShell, Runspace runspace, Script script, IDictionary parameters = null, Action<ICollection<PSObject>> callback = null) { SetupScriptRun(powerShell, runspace, sc

我目前正在执行PowerShell脚本,如下所示:

internal void RunScript(PowerShell powerShell, Runspace runspace, Script script, IDictionary parameters = null, Action<ICollection<PSObject>> callback = null)
    {
        SetupScriptRun(powerShell, runspace, script, parameters);

        powerShell.BeginInvoke(new PSDataCollection<PSObject>(), null, psCallback =>
        {
            ICollection<PSObject> results = powerShell.EndInvoke(psCallback);
            LogWarningsAndErrors(powerShell, runspace, script, parameters);
            if (callback != null)
            {
                callback(results);
            }
        }, null);
    }
internal Task RunScript(PowerShell powerShell, Runspace runspace, Script script, IDictionary parameters = null, Action<ICollection<PSObject>> callback = null, object state = null)
    {
        SetupScriptRun(powerShell, runspace, script, parameters);

        return Task.Factory.StartNew<ICollection<PSObject>>((o) =>
            {
                var results = powerShell.Invoke();
                LogWarningsAndErrors(powerShell, runspace, script, parameters);

                if (callback != null)
                {
                    callback(results);
                }

                return results;
            }, state);
    }
内部无效运行脚本(PowerShell PowerShell、运行空间运行空间、脚本脚本、IDictionary参数=null、操作回调=null)
{
SetupScriptRun(powerShell、运行空间、脚本、参数);
powerShell.BeginInvoke(新PSDataCollection(),null,psCallback=>
{
ICollection results=powerShell.EndInvoke(psCallback);
LogWarningAndErrors(powerShell、运行空间、脚本、参数);
if(回调!=null)
{
回调(结果);
}
},空);
}
我想将此异步编程模型(APM)代码包装到任务中,如下所述:

我已尝试使用以下方法创建任务,但无法确定参数应该是什么:

var task = System.Threading.Tasks.Task.Factory.FromAsync<PSDataCollection<PSObject>>(???);
var task=System.Threading.Tasks.task.Factory.fromsync(???);

如何在任务中包装BeginInvoke/EndInvoke方法?

我最终使用了非异步Powershell Invoke(),如下所示:

internal void RunScript(PowerShell powerShell, Runspace runspace, Script script, IDictionary parameters = null, Action<ICollection<PSObject>> callback = null)
    {
        SetupScriptRun(powerShell, runspace, script, parameters);

        powerShell.BeginInvoke(new PSDataCollection<PSObject>(), null, psCallback =>
        {
            ICollection<PSObject> results = powerShell.EndInvoke(psCallback);
            LogWarningsAndErrors(powerShell, runspace, script, parameters);
            if (callback != null)
            {
                callback(results);
            }
        }, null);
    }
internal Task RunScript(PowerShell powerShell, Runspace runspace, Script script, IDictionary parameters = null, Action<ICollection<PSObject>> callback = null, object state = null)
    {
        SetupScriptRun(powerShell, runspace, script, parameters);

        return Task.Factory.StartNew<ICollection<PSObject>>((o) =>
            {
                var results = powerShell.Invoke();
                LogWarningsAndErrors(powerShell, runspace, script, parameters);

                if (callback != null)
                {
                    callback(results);
                }

                return results;
            }, state);
    }
内部任务运行脚本(PowerShell PowerShell、运行空间运行空间、脚本脚本、IDictionary参数=null、操作回调=null、对象状态=null)
{
SetupScriptRun(powerShell、运行空间、脚本、参数);
返回Task.Factory.StartNew((o)=>
{
var results=powerShell.Invoke();
LogWarningAndErrors(powerShell、运行空间、脚本、参数);
if(回调!=null)
{
回调(结果);
}
返回结果;
},国家);
}

脚本是什么类型的?以及SetupScriptRun和LogWarningAndErrors方法?