[SOLVED] Windows qm guest exec powershell got timeout

rml

Member
Apr 24, 2019
31
0
11
46
Think I'm being stupid with syntax here, fresh install with Serial drivers and qm guest agents installed

I'm having trouble getting powershell commands working

e.g.
Code:
oot@pve:~# qm guest exec 108 -- "C:\WINDOWS\system32\cmd.exe" "/c"  "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "dir"
VM 108 qmp command 'guest-exec-status' failed - got timeout

root@pve:~# qm guest exec 108 -- "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "dir"
VM 108 qmp command 'guest-exec-status' failed - got timeout

root@pve:~# qm guest exec 108 -- C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command dir
Agent error: Guest agent command failed, error was 'Failed to execute child process (No such file or directory)'

Powershell -Command dir works from the guest machine

Thanks in advance

EDITS: rewrote question after solving original cmd.exe problem

I've managed to cmd working
e.g.
qm guest exec 108 -- "C:\WINDOWS\system32\cmd.exe" "/c" "dir"

for me next time:
note the quotes of all all arguments, I did that because I didn't appreciate what the -- was doing so may not be totally necessary but worked
https://unix.stackexchange.com/questions/11376/what-does-double-dash-mean
 
Last edited:
hi,

this works here:
Code:
qm guest exec 2000 "powershell.exe" "whoami"
{
   "exitcode" : 0,
   "exited" : 1,
   "out-data" : "nt authority\\system\r\n"
}

qm guest exec 2000 "powershell.exe" "ls /users/"
{
   "exitcode" : 0,
   "exited" : 1,
   "out-data" : "\r\n\r\n    Directory: C:\\users\r\n\r\n\r\nMode                 LastWriteTime         Length Name                                                                 \r\n----                 -------------         ------ ----                                                                 \r\nd-r---         8/11/2020  12:52 PM                Public                                                               \r\nd-----         4/15/2021  11:31 AM                user                                                                 \r\n\r\n\r\n"
}

no need to separate with -c or /c in powershell case

for cmd the following syntax works:

Code:
qm guest exec 2000 "cmd" "/c whoami"
qm guest exec 2000 "cmd" "/c whoami /all"

hope this helps
 
Last edited:
Clearly overcomplicating it as always, that's really helpful, thanks Oguz
 
just fyi, even easier is it with the '--' separator

Code:
qm guest exec 2000 -- cmd /c whoami

no quoting of the arguments necessary
 
  • Like
Reactions: rml
#######################################################
########## API : Execution on the server with cmd ##########


$FQDN = "myserver"
$TokenID = "root@pam!blabla"
$Secret = "fa57313e-878d-4c49-9dd1-7faab4837c55"
# Construire l'URL du serveur Proxmox
$ProxmoxServer = "https://"+$FQDN+":8006"
$headers = @{ "Authorization" = "PVEAPIToken $TokenID=$Secret" }


$vmid = "100"

# Your Windows command
$winCmd = "Get-ComputerInfo"

# Build the body (x-www-form-urlencoded format)
$body = "command=powershell&command=-Command&command=$winCmd"

# Send request
$execRun = Invoke-RestMethod -Uri "$ProxmoxServer/api2/json/nodes/$node/qemu/$vmid/agent/exec" `
-Headers $headers -Method POST -Body $body `
-ContentType "application/x-www-form-urlencoded" -SkipCertificateCheck

$pidpve = $execRun.data.pid

######################################################
########## Retrieval using the shell PID ##########

# Wait a little bit for Windows to process the request
Start-Sleep 5

# Query the PID status
$statusUri = "$ProxmoxServer/api2/json/nodes/$node/qemu/$vmid/agent/exec-status?pid=$pidpve"
$status = Invoke-RestMethod -Uri $statusUri -Headers $headers -Method GET -SkipCertificateCheck

$status.data.'out-data'