4

リモートマシンでプログラムを実行するプログラムを書いています。Windows SDKをインストールし、ビットを含むファイルを正常にアップロードしました。リモートで変更ディレクトリを実行し、PowerShellを使用してコマンドを実行したいと思います。

プログラムはコンパイルされて実行されますが、回答変数には「メソッドまたは操作は実装されていません。\ 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);**
4

2 に答える 2

4

この行はそれを修正しました。

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);
于 2012-02-07T15:21:58.613 に答える
0

CDはエイリアスであるため、Set-Location(完全なCMDlet)を使用してみて、それが役立つかどうかを確認できる可能性があります。そのメソッドを介してパイプラインを作成するときにどの環境がセットアップされるかわからないため、エイリアスを使用しないことに誤りがあります。

于 2012-02-06T18:56:00.567 に答える