How do I execute a saved script using API SendScript?

E
EddieN
MV Transportation, Inc.

Hello,

I created/saved a Javascript in Script Editor which works fine Send Script window. I'd like to use HTTP Post to call that script. Here is my syntax 

{"Action": "SendScript", 
"ScriptType": "JavaScript",
"ScriptName": "TestScript",  
"QueueMessagesOfflineDevices":"true",
"ReportScriptStatus": "true",
"SendBy":"MobiControl",
"Message":"1'"}

I got 204 response Code but nothing happens on the tablet.

Not sure if "ScriptName" would automatically look for the saved script or do I have to give it full path?

Ref: https://s095155.mobicontrolcloud.com/mobicontrol/api/docs/index/index.html?url=/mobicontrol/api/swagger/v2/swagger.json#:~:text=DeviceMessageBoxType-,SendScriptAction,-%7B

Please advise. Thanks

2 years ago
SOTI MobiControl
ANSWERS
RS
Rafael Schäfer
2 years ago

I haven't used a saved script in API call yet, I provide the script via API call directly.

But i would assume that the value you use in Scriptname must compare the scriptname you used in the console.

But i would do like that:

  1. /devices​/actionScripts to get all scripts available and get the ID of the script you need
  2. ​/devices​/actionScripts​/{id} to send the script to the deviceID

Edit:
I also see an issue in your posted script. In Message you send 1' this is wrong and could causes issues. Remove the ' and try again. But in general I don't know what you have to send as a message here (if you send the script directly as i do, message contains the code of the script), maybe you don't need to because you send the scriptname.

E
EddieN
2 years ago

Great idea, unfortunately I search through my RES API page both V1 and V2 I can't seem to find these methods to retrieve action scripts.

RS
Rafael Schäfer
2 years ago

When i use your provided link i can find "/devices​/actionScripts" there by searching with Browser search on the website.

AW
Adam Williams
2 years ago

Hi Rafael,

/devices/actionScripts/{id} only provide methods for getting, updating or deleting a particular script, not assigning it to a device. 

RS
Rafael Schäfer
2 years ago

Yes, my fault, mixed that up.

So your comment is the only one that can be used and i saw that "message" seem to be needed always and should contain the script itself. Doesn't make sense to me if you point to a script name which should contain exactly this information.

AW
Adam Williams
2 years ago

"Message" is also baffling to me. I haven't done any testing on that part. I suspect its for some "customisable" info in the device log.

I would sincerely hope that the OP isnt failing because "Message" != Script Contents

E
EddieN
2 years ago

Gee, you're totally right. I can find it now on the V2 API. Unfortunately, my ENV is still on V1 and I don't think it is supported. any other ideas?

I typically include SOTI commands into "Message" and I', supposed that I can do the same with javascript and POST it that way, but it will be very messy with all the syntaxes 

AW
Adam Williams
2 years ago

HI Eddie,

Have you seen my suggestion below?

E
EddieN
2 years ago

Yes, I have, I also attached a few screenshots on your reply as well. I tried you suggested payload under my V1 ENV
Here is the actual payload


{
    "Action": "SendScript",
    "Message": "xxxxx",
    "QueueMessagesOfflineDevices": true,
    "ReportScriptStatus": true,
    "SendBy": "MobiControl",
    "ScriptType": "JavaScript",
    "ScriptName": "mv588eddie"
  }

Error log :Custom log (ReferenceError: "xxxxx" is not defined. (UserScript#1))


RS
Rafael Schäfer
2 years ago

That's the only way i did it also for myself and used Javascript.

The best would be to rework the script in your programm (for example: replace all " with ' inside of your script) to go around issues. And then place this into the API call. Then you are most dynamical as possible as long you don't need ' and " both in your javascript.

AW
Adam Williams
2 years ago

Hi Eddie,

Can I check which POST method are you using?

I would be using POST   ​/devices​/{deviceId}​/actions

Your payload seems ok to me.

SOTI documentation on the REST APIs stuff really is not great, and I find it often very useful to use the network tab in chrome developer options (F12 Network) to see what MC webconsole itself is calling.

So if the above still doesnt work, when calling a script from MC, it instead calls the below. But I dont understand what the below would work and and the above not.

POST /devices/actions

with payload

{
  "DeviceIds": [
    "xxxxx"
  ],
  "ActionInfo": {
    "Action": "SendScript",
    "Message": "xxxxx",
    "QueueMessagesOfflineDevices": true,
    "ReportScriptStatus": true,
    "SendBy": "MobiControl",
    "ScriptType": "xxxxx",
    "ScriptName": "xxxxx"
  },
  "Filter": null
}

AW
Adam Williams
2 years ago

Hi Eddie,

I download the attachements from SOTI for some reason. Can you screen grab them and copy them into a reply?

E
EddieN
2 years ago

apicall

sotiapi

log

mv588eddie is my saved script, it's just a simple app restart script for resting purposes. My actual javascript script is actually rather complicated which gathers device information and create an XML file in a predefined directory.

AW
Adam Williams
2 years ago

All looks fine from those screenshots too.

May I suggest adding the line below into mv588eddie script, just to see we're hitting it . 

mobicontrol.log.info('Hello world!');
E
EddieN
2 years ago

Here is the log : "Script was sent to device ('1')"  so it's not hitting it.

AW
Adam Williams
2 years ago

Hi Eddie,

I have done some testing as I could not let this one go.

"Message" is the script you want to send to the device.

"ScriptName" (appears to be) an optional and highly misleading parameter. 

e.g. 

{
    "Action": "SendScript",
    "Message": "mobicontrol.log.info('Hello world!');",
    "QueueMessagesOfflineDevices": true,
    "ReportScriptStatus": true,
    "SendBy": "MobiControl",
    "ScriptType": "JavaScript"
  }
E
EddieN
2 years ago

Hi Adam, thanks for not giving up on this. I believe this syntax works 100% as Rafael also mentioned above. I was looking for a clean way to execute my saved Javascript without adding it to "Message" using the "ScriptName" parameter

If there are not other workaround then I will have to stick to the old method. Thanks again

C
CDMOD@SOTI
2 years ago

Similar Discussions