fix: deliver Invoke-PveVmGuestExec -Args to the guest as argv

ExecuteGuestCommand JSON-serialized the args array into the agent/exec
'input-data' field — which is the process's STDIN, not its arguments. So
guest commands ran with no argv: cmd.exe started interactively and the
JSON blob ['/c','echo',...] arrived at its prompt.

PVE's agent/exec 'command' parameter is itself an array (element 0 = the
executable, the rest = argv) sent as repeated form keys. The low-level
client couldn't express repeated keys (Dictionary<string,string> only),
so:

- Add PostAsync(string, IEnumerable<KeyValuePair<string,string>>) to
  IPveHttpClient/PveHttpClient; BuildFormContent now emits one key=value
  field per pair, so a key may repeat.
- ExecuteGuestCommand builds command = [exe] + args as repeated 'command'
  fields and no longer touches input-data.

Tests: form-encoder repeated-key + per-value encoding cases; VmService
tests asserting the command array, order, and absence of input-data; an
integration regression guard that echoes an arg and checks it round-trips
as stdout.

Tracked as F091. Closes #68.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Clint Branham
2026-05-22 14:32:04 -05:00
parent 5266accfae
commit bc71ed4a12
7 changed files with 199 additions and 10 deletions
+17 -2
View File
@@ -111,6 +111,21 @@ namespace PSProxmoxVE.Core.Client
return await SendAsync(request, resource, "POST").ConfigureAwait(false);
}
/// <summary>
/// POST whose form body may contain repeated keys, for PVE array parameters
/// (e.g. guest-exec "command"). Each pair becomes one key=value field.
/// </summary>
/// <param name="resource">Relative resource path</param>
/// <param name="data">Form fields; a key may appear more than once</param>
/// <returns>Raw JSON response body</returns>
public async Task<string> PostAsync(string resource, IEnumerable<KeyValuePair<string, string>> data)
{
if (data == null) throw new ArgumentNullException(nameof(data));
var request = BuildRequest(HttpMethod.Post, resource, mutating: true);
request.Content = BuildFormContent(data);
return await SendAsync(request, resource, "POST").ConfigureAwait(false);
}
/// <summary>Performs a PUT request against the specified API resource path.</summary>
/// <param name="resource">Relative resource path</param>
/// <param name="data">Form fields to send as application/x-www-form-urlencoded body</param>
@@ -142,7 +157,7 @@ namespace PSProxmoxVE.Core.Client
/// <c>:</c> and <c>!</c> which PVE's internal API consumers (e.g. cluster join)
/// do not properly URL-decode.
/// </summary>
private static StringContent BuildFormContent(Dictionary<string, string> data)
private static StringContent BuildFormContent(IEnumerable<KeyValuePair<string, string>> data)
{
var sb = new StringBuilder();
foreach (var kvp in data)
@@ -191,7 +206,7 @@ namespace PSProxmoxVE.Core.Client
public string Get(string resource) =>
GetAsync(resource).GetAwaiter().GetResult();
/// <summary>Synchronous wrapper for <see cref="PostAsync"/>.</summary>
/// <summary>Synchronous wrapper for <see cref="PostAsync(string, Dictionary{string, string})"/>.</summary>
public string Post(string resource, Dictionary<string, string>? data = null) =>
PostAsync(resource, data).GetAwaiter().GetResult();