Proxmox API file-write special characters in content

Feb 24, 2022
28
0
6
34
Hi,

I-m trying to copy the content of script through the Proxmox guest agent to a windows client in Powershell. However, when I have a curly brackets "{" in the script the file-write will fail with the error {}":"property is not defined in schema and the schema does not allow additional properties because it is probably interpreted as Json. how do I "whitelist" these characters for the write-file.

I'm accessing the API via Powershells Invoke-RestMethod. (powershell is the only option we have so we can't change to a different languages or to pvesh)

Code:
$source = "      ....some code...  &  {$var}"
 
  $destination= "C:\Users\user\Desktop\script.ps1"


 Invoke-RestMethod -Uri ("https://$ip:8006/api2/json/nodes/pvetest/qemu/101/agent/file-write?content=$source&file=$destination") -Method Post -SkipCertificateCheck -Headers $headers

the two curly Brackets at the end throw the error. If i remove only that part of the script it works.

thanks in advance
 
I don't understand, does your local script write the file or does the "remote" host? Also its not clear to me whether the "Invoke-RestMethod" causes the problem or if its somewhere else.
the file-write will fail
Please elaborate what you mean by "file-write" exactly.

If its the former, (and if you are running PowerShell version <6.0.0) then you might be able to pass the parameter -UseBasicParsing to avoid the command parameters to be interpreted.
Alternatively, have you tried explicitely escaping the curly brackets? I.e. something like ....some code... & `{$var`}"
 
Hi, thanks for your answer. I was able to get it working by url encoding the $source so it doesn't get interpreted as parts of the api request. However I have a different problem now.

When I try to execute a command on a remote windows vm via the guest agent I can only get the process but it doesn't appear in the user session. The API requests are made in powershell on a different physical PC not the remote VM or Hypervisor

Code:
Invoke-RestMethod -Uri ("$https://$ip:8006/api2/json/nodes/pvetest/qemu/101/agent/exec?command=powershell.exe&input-data=command") -Method Post -SkipCertificateCheck -Headers $headers

the powershell command appears in the task manager(see screenshot) but I don't see it in the users session. Have already tried changing the log on user of the qemu guest service to that same local account

How are these commands execuded by the guest agent? in cmd or Powershell?

thanks

1649746619476.png
 
but I don't see it in the users session
What do you mean by user session, the command line window? I am not 100% sure how Windows handles that, but your script is launched by the Qemu-Guest agent and therefore the script is running as a child of the agent process. For the command line GUI windows to appear you probably would have to launch it "as the user".

How are these commands execuded by the guest agent? in cmd or Powershell?
According to your example Invoke-RestMethod you tell Windows to run it in a powershell: .../agent/exec?command=powershell.exe&input-data=command") -Meth... you could alternatively invoke it in the normal command line via changing that to cmd.exe
I don't think this will fix your problem though, as the script will still run as a subprocess of the guest agent.

If it's only the script output you are after, you can output what your script prints into a log file and access that. Otherwise you probably would have to implement some kind of GUI to interact with your script. Why exactly do you need the GUI window?

I am not aware of any way to automatically attach a script GUI to an arbitrary user session. Maybe there is a way to launch the script directly as a user rather than it being a subprocess of the guest agent? That might bring some problems with it though, e.g. if the user is not logged in...
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!