【发布时间】:2012-02-06 17:21:44
【问题描述】:
大家好,我想将文件复制到计算机并远程进入所述计算机 并运行一个命令,然后收集该命令的输出文件。我有 受命使用 Powershell 复制和远程进入机器。 我使用此页面来帮助我开始: http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C 我正在使用的程序是 .net 4 并且必须保持这种状态。我首先安装了 windows sdk 3.5 然后当代码不起作用时我安装了 sdk 4.0 但我仍然得到 我的答案字符串中出现相同的错误:“混合模式程序集是针对运行时版本‘v2.0.50727’构建的,如果没有其他配置信息,则无法在 4.0 运行时中加载。\r\n” 谢谢您的帮助 凯西
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Import-Module BitsTransfer" + Environment.NewLine + "Start-BitsTransfer -Source " + filename + " -Destination \\\\" + host + "\\" + uploadtodir + "\\"+Environment.NewLine);
// add an extra command to transform the script output objects into nicely formatted strings
// remove this line to get the actual objects that the script returns. For example, the script
// "Get-Process" returns a collection of System.Diagnostics.Process instances.
pipeline.Commands.Add("Out-String");
// execute the script
Collection<PSObject> results = new Collection<PSObject>();
try
{
results = pipeline.Invoke();
}
catch (Exception ex)
{
results.Add(new PSObject((object)ex.Message));
}
// close the runspace
runspace.Close();
// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
string answer= stringBuilder.ToString();
【问题讨论】:
标签: c# windows powershell sdk