ryanblenis / MeshCentral-ScriptTask

Scripting plugin for MeshCentral
Apache License 2.0
52 stars 14 forks source link

Adding more script languages #2

Open OutbackMatt opened 4 years ago

OutbackMatt commented 4 years ago

Love the work

Where would I start to look if I wanted to add say VBS to the languages that can be used Yes, I know that VBS is really only useful on Windows machines...

I have a large number of VBS scripts, and the concept of not loading them onto client machines, but instead running from my server is rather appealing

ryanblenis commented 4 years ago

Hi @OutbackMatt,

VBScripts are generally run by either CScript.exe or WScript.exe, depending on whether the context should be a Console (CScript) or Windows application (WScript). Given that WScript is likely going to interact with the user (echo's produce message box's), and all the scripts (currently) run as the MeshAgent user (SYSTEM by default unless you're doing something special when installing your agents) you'd likely want to run CScript.

If you're looking to get output back from the VBScript, I'd modify the BAT handler to use CSCript instead of cmd.exe. If you're looking for a "quick and dirty" way to run them, you can utilize the existing BAT functionionality and modify your VBScripts to be BAT by echoing the VBScript to a file, then calling CScript on them from the BAT file.

I don't have any VBS files on hand to test with, but assuming they aren't built with user-interaction (WScript) in mind, in theory it shouldn't be difficult to implement, but I haven't used VBScript for much in years so I don't have any test scripts to work with right now.

OutbackMatt commented 4 years ago

I use VBS to gather system information about servers, things like current event log events, hard drive FREE capacity, sizes of specific (log) files and much more.

I send (via scheduled task) these informational reports to myself via secure SMTP connections (email), and have my mail server act on the contents automatically.

I can certainly supply some samples, but it is likely simple enough for me to re-write my scripts in powershell.

ryanblenis commented 4 years ago

Feel free to share and I'll take a look. I'm currently working on procedures (logic included in script processing), variable replacement, reactions (if a script returned X, do Y), and alerting, so I've got a few features in the works, but if I can and it makes sense with the other features I'll take a look at native VBS support.

OutbackMatt commented 4 years ago

This VBS gets a backup log, reads it and includes the last three lines into the body on an email sent to an account at my mailserver for auto-analysis.

Rem
Rem sends an email on completion of Backup
Rem

Option Explicit

Dim s1, s2, s3, FSO, OBJfile, FileIn, ObjEmail, schema

filein = "C:\Documents and Settings\All Users\Application Data\MyApplication\Log\Backup.log"

Set objEmail = CreateObject("CDO.Message")
Set FSO = CreateObject("Scripting.FileSystemObject")
set OBJfile = FSO.opentextfile(filein,1,0)

schema = "http://schemas.microsoft.com/cdo/configuration/"

While Not OBJfile.atendofstream
    s3=s2
    s2=s1
    s1=OBJfile.readline
Wend

With objEmail
    .From = "server@example.com"
    .To = "outbackmatt@example.com"
    .Subject = "Backup"
    .Textbody = "Backup" & VBCRLF & VBCRLF
    .Textbody = objEmail.Textbody & "Backup has successfully been completed" & VBCRLF

    With .Configuration.Fields
        .Item (schema & "sendusing") = 2
        .Item (schema & "smtpserver") = "mail.example.com"
        .Item (schema & "smtpauthenticate") = 1
        .Item (schema & "smtpserverport") = 465
        .Item (schema & "smtpusessl") = True
        .Item (schema & "sendusername") = "server@example.com"
        .Item (schema & "sendpassword") = "Top_secret_password"
        .Update
    End With
    .Configuration.Fields.Update

    .Textbody = .Textbody & VBCRLF & VBCRLF & S3
    .Textbody = .Textbody & VBCRLF & VBCRLF & S2
    .Textbody = .Textbody & VBCRLF & VBCRLF & S1

    .Send
End With

set OBJfile = nothing
openmoto commented 4 years ago

Hi @OutbackMatt

If you haven't figured out the VBS yet, you can use powershell till your issue is figured out. Like you guys already suggested, it's probably easier to rewrite the scripts.

I tried doing what you're trying in powershell and here's what I have that worked. Since @ryanblenis has future plans of passing variables to script, some time down the road you would be able to eliminate the values from the script and pull it from mesh central per group.


$recipient = "Manager 1 <youranalysismail@domain.com>"
$mailserver = "smtp-relay.gmail.com"
$port = 25
$Subject = "Log Tail"
$logfile = "C:\Documents and Settings\All Users\Application Data\MyApplication\Log\Backup.log"
$tail = 3

$latest = get-content -Last $tail $logfile  | Out-String

Send-MailMessage -To $recipient -From $sender -SMTPServer $mailserver -Port $port -Subject $Subject -Body $latest

#For more send-mailmessage examples see below
#https://4sysops.com/archives/send-mailmessage-paramaters-and-examples/ 
MailYouLater commented 4 years ago

I would have thought that VBS scripts should be directly supported by this plugin, but since they apparently aren't, a good workaround would be to use a hybrid VBS/BAT file. This can be done several different ways, but the way I prefer is the %~f0?.wsf trick, where you save it as a .bat, and the batch script runs an embedded VBS script directly:

<!-- : Begin batch script
@echo off
cscript //nologo "%~f0?.wsf"
exit /b

----- Begin wsf script --->
<job><script language="VBScript">
  WScript.Echo "VBScript output called by batch"
</script></job>

I found this information in the "UPDATE 2014-04-27" section at the bottom of this StackOverflow answer. It should also work for JScript by changing the \<script> tag's language attribute, and you should also be able to embed multiple VBScript and/or JScript scripts into your BAT file by giving each job an id="name" and adding //job:name to the cscript calls, but I've personally only done the basic embedded VBS in a BAT like the sample above.

OutbackMatt commented 4 years ago

Interesting concept - thanks for the tips /links