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
@@ -16,6 +16,13 @@ namespace PSProxmoxVE.Core.Client
/// <summary>Performs a POST request against the specified API resource path.</summary>
Task<string> PostAsync(string resource, Dictionary<string, string>? data = null);
/// <summary>
/// Performs a POST request whose form body may contain repeated keys, used for
/// PVE array parameters (e.g. guest-exec "command"). Each pair becomes one
/// <c>key=value</c> field, so a key may appear multiple times.
/// </summary>
Task<string> PostAsync(string resource, IEnumerable<KeyValuePair<string, string>> data);
/// <summary>Performs a PUT request against the specified API resource path.</summary>
Task<string> PutAsync(string resource, Dictionary<string, string>? data = null);
@@ -25,7 +32,7 @@ namespace PSProxmoxVE.Core.Client
/// <summary>Synchronous wrapper for <see cref="GetAsync"/>.</summary>
string Get(string resource);
/// <summary>Synchronous wrapper for <see cref="PostAsync"/>.</summary>
/// <summary>Synchronous wrapper for <see cref="PostAsync(string, Dictionary{string, string})"/>.</summary>
string Post(string resource, Dictionary<string, string>? data = null);
/// <summary>Synchronous wrapper for <see cref="PutAsync"/>.</summary>
+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();
+9 -7
View File
@@ -599,16 +599,18 @@ namespace PSProxmoxVE.Core.Services
IPveHttpClient client = _injectedClient ?? new PveHttpClient(session);
try
{
var data = new Dictionary<string, string>
// PVE's agent/exec "command" is an array: element 0 is the executable and
// each subsequent element is one argv entry. It is sent as repeated form
// keys (command=<exe>&command=<arg1>&...). Do NOT use "input-data" for
// arguments — that is the process's STDIN, not argv.
var data = new List<KeyValuePair<string, string>>
{
["command"] = command
new KeyValuePair<string, string>("command", command)
};
if (args != null && args.Length > 0)
if (args != null)
{
// PVE expects input-data for arguments passed as a JSON-encoded string array
var argsJson = Newtonsoft.Json.JsonConvert.SerializeObject(args);
data["input-data"] = argsJson;
foreach (var arg in args)
data.Add(new KeyValuePair<string, string>("command", arg));
}
var response = client.PostAsync($"nodes/{Uri.EscapeDataString(node)}/qemu/{vmid}/agent/exec", data)