2012-02-06 8 views
3

リモートマシン上でプログラムを実行するプログラムを作成しています。 私はwindows sdkをインストールし、ビット でファイルを正常にアップロードしました。リモートで変更ディレクトリを実行し、 パワーシェルを使用してコマンドを実行したいと思います。Powershell Runspace Enter-PSSession

プログラムをコンパイルして実行されますが、回答変数で、それは言う:「メソッドまたは操作が実装されていません\ rを\ n」は、私がパイプラインで別のコマンドを使用しようとした

// create Powershell runspace 
Runspace runspace = RunspaceFactory.CreateRunspace(); 
// open it 
runspace.Open(); 
// create a pipeline and feed it the script text 
Pipeline pipeline = runspace.CreatePipeline(); 
pipeline.Commands.AddScript("Enter-PSSession " + host + Environment.NewLine + "cd " + uploadtodir + Environment.NewLine + command + Environment.NewLine + "exit" + Environment.NewLine + "exit" + 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(); 

が、それも失敗しますが、エラー出力はありません。

**pipeline.Commands.AddScript("$sessions = New-PSSession -ComputerName " + host + Environment.NewLine + "Invoke-Command -session $sessions -ScriptBlock {cd " + uploadtodir+"}" + Environment.NewLine+"Invoke-Command -session $sessions -ScriptBlock {"+command+ "}" + Environment.NewLine + "Remove-PSSession -Session $sessions" + Environment.NewLine + "exit" + Environment.NewLine + "exit" + Environment.NewLine);** 
+0

FYI、 –

+0

二piplineコマンドを修正しましたhttp://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts/見ますコマンド自体にメモリ不足がありました –

+0

WSMan:\ localhost \ Shell \ MaxMemoryPerShellMB 1000で再起動 - サービスwinrm –

答えて

3

この行が修正されました。

pipeline.Commands.AddScript("$sessions = New-PSSession -ComputerName " + host + Environment.NewLine 
+ "Invoke-Command -session $sessions -ScriptBlock {cd " + uploadtodir+"}" + Environment.NewLine 
+"Invoke-Command -session $sessions -ScriptBlock {"+command+ "}" + Environment.NewLine 
+ "Remove-PSSession -Session $sessions" + Environment.NewLine 
+ "exit" + Environment.NewLine + "exit" + Environment.NewLine); 
0

それはCDが別名であるので、あなたはセット場所(フルコマンドレット)を使用して試すことができ、それが助けかどうかを確認している可能性があります。私は、その方法でパイプラインを作成するときにどのような環境がセットアップされるのかわからないので、エイリアスを使用しないことに誤りがあります。

+0

私はセット位置を試しましたが、助けのための変更感謝はありませんでしたが、 –

+0

助けてくれてありがとう –

関連する問題