こんにちは、コンピュータにファイルをコピーしてリモートのマシン にリモートコピーして、そのコマンドの出力ファイルを収集したいと思います。私は 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はプロジェクトのApp.configファイルで」助けを おかげ ケーシーpowershellを使ったコピーとリモート操作
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();
ありがとう –
App.configファイルを作成する必要がある場合は、プロジェクトを.net 3.5に変更してから4に変更する必要があります。変更が反映されるまでアプリケーションファイルの変更を読み取らないプロジェクトのソリューションが作成されます。 http://msdn.microsoft.com/en-us/library/bb772098%28v=vs.90%29.aspx –