Kaseya Community

Pass Kaseya variables to VBS ???

  • anyone know of a way to pass kaseya varables to vbscripts?

     

    I've thought about maybe using txt files to go back and forth, so grab a variable from within a Vbscript via text file, then pass back to vbscript with kaseya writing to a txt file that the vbscript can read into a var= x line.

     

    anyone have a better idea to achiving this?

    I'm thinking along the lines of host names, registry keys for version testing, etc....

  • You would use the “Execute Command Shell” or “Execute file” procedure option with;

    %windir%\system32\wscript.exe //nologo (path)\defragaudit.vbs "#vDefrag#"

    your VB script needs to be written though to accept input from command line, you can do this by updating your vbscript to use something like this;

    varDefrag = wscript.arguments(0)

    for every argument/Kaseya variable you pass to the VB script you will need to create a VB script variable

    var1 = wscript.arguments(0)

    var2 = wscript.arguments(1)

    var3 = wscript.arguments(2)



    [edited by: HardKnoX at 3:10 PM (GMT -7) on 10-18-2010] more input
  • I did this once. It isn't very elegant, but it worked.. actually I posted info about it in the old forum. I will cross post it. I thought i'd be doing this "replace.vbs" thing all the time, but as it turned out, I never developed any other scripts that needed it.

    forum.kaseya.com/showthread.php

    ----------------

    Basically, you're replacing known text in a vbs script with a kaseya variable.

    For example, you want to replace the text "C:\temp" in a vbs script file named backup.vbs with a variable you have defined for the agent temp directory.

    i.e.

    replace.vbs "c:\path\backup.vbs" "c:\temp" "#atemp#"

    Then execute backup.vbs with the kaseya variable written in it.

    You would download replace.vbs to the managed machine as part of the script.

    For more info on replace.vbs see:

    www.microsoft.com.nsatc.net/.../hey0208.mspx

  • more cross-posting:

    ----------

    It is possible to input your kaseya variables into vbs script files using a simple find replace tool. (replace.vbs).

    Download replace.vbs as part of your kaseya script, and use it to search a vbs script file (or any other file) for certain text, and then replace that text with your kaseya variable, i.e:

    c:\temp\replace.vbs modifyfile.vbs oldtext #newtext#

    -----------------

    Replace.vbs

    Const ForReading = 1

    Const ForWriting = 2

    strFileName = Wscript.Arguments(0)

    strOldText = Wscript.Arguments(1)

    strNewText = Wscript.Arguments(2)

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFSO.OpenTextFile(strFileName, ForReading)

    strText = objFile.ReadAll

    objFile.Close

    strNewText = Replace(strText, strOldText, strNewText)

    Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)

    objFile.WriteLine strNewText

    objFile.Close

  • MyArch-man,

    Thank you for the help ths far. I am trying to use a replace.vbs solution as you have above but am getting some errors. Maybe you can help.

    I have a vbs script that looks at a certain directory and checks the dates of the files. If any of the dates are older than X days it will delete the files. I would like to use this script to clean out the patch directory that we use at several clients but dont want to have to rework the script for each one SO I am leveraging the kaseya patch location that we configured ( #patchparams.sourcepath# ), however we need to use the replace .vbs to pass the variable to the script that cleans out the directory.

    Currently the script executes the file #agenttempdirectory#\replace.vbs "#agenttempdirectory#\patchclean.vbs" "PATCHDIRECTORY" "#patchparams.sourcepath#

    Where "PATCHDIRECTORY" are the words that are in the patchclean.vbs that need to be replaced with the patch location.

    For some reason whenever I run the script I get the following error:

    FAILED in processing THEN step 4, Execute File, with error Spawn Failed, c:\agentdirectory\replace.vbs "patchclean.vbs" "PATCHDIRECTORY" "\\VEFILE\Patches\"

    Am I doing something wrong? Any help would be greatly apprecaited.

  • from what you have written here I take it PATCHDIRECTORY is the variable you are using in the VBS script - normally when I create parameters in VBS you don't specify the parameter name you just put it in.

    so in your case it would be patchclean.vbs #patchparams.sourcepath#

    Then in your VBS you would just accept that as Argument 1

  • Im not sure I understand.

    The point of the replace.vbs is to replace the words designated in the quotes ie. "PATCHDIRECTORY" - it is not a variable.

    replace.vbs looks at patchclean.vbs for the string "PATCHDIRECTORY" and replaces it with #patchparams.sourcepath#

  • sorry confused by your original post - correct me if I am wrong

    You have a vbscript called patchclean.vbs that looks at a certain directory and then cleans out files of a certain age all very simple.

    So surely in your vb script all you do is make the directory you want to clean a variable and then assign that variable the first parameter of your script

    I am unsure why you are using replace to change a value in a script.

    So when you run your script from a command line you might run it like

    wscript.exe  patchclean.vbs c:\patchfolder

    *Code in your script*

    patchdirectory = Wscript.Arguments(0)

    So the variable patchdirectory now = c:\patchfolder

    You can then run whatever code you want against the variable patchdirectory

    You can also check on the runtime of the script if no parameter is passed then quit the script.

    In yoru kaseya script you just do as I mentioned above - patchclean.vbs #patchparams.sourcepath#

    Again sorry if I am off the mark just cannot see why your using a replace script to change the values seems like your making more work for yourself.

  • I see what you are getting at. That would be a better way to go about it. However I tried it and i am getting an error.

    Here is what I have in my VBS - let me know if something is amiss:

    Dim patchdirectory, fso, f, f1, fc

    Set patchdirectory = Wscript.Arguments(0)

    Set fso = CreateObject("Scripting.FileSystemObject")

    Set f = fso.GetFolder("patchdirectory")

    Set fc = f.Files

    For Each f1 in fc

    If DateDiff("d", f1.DateLastModified , Now) > 15 Then

    f1.Delete

    End If

    Next

    Set fso = Nothing

    Set f = Nothing

    Set fc = Nothing

    Whenever I execute it like this: wscript C:\somedirectory\patchclean.vbs #patchparams.sourcepath# I am getting a Windows Script Host error on Line 2 Character 1 Error: Object Required '[string: "mypatchdirectory"]'

    Any help would be appreciated.

    Thanks.

  • So I removed the "Set" from line 2 and no longer got that error, but now I am getting a "Path Not Found" error.

    Any ideas?

  • question if you run the vbs from a command prompt and fill in an actual path does it work... so not the kaseya variables...

  • I thought along the same lines and ran it a few ways:

    wscript C:\somedirectory\patchclean.vbs \\server\patchdirectory

    wscript C:\somedirectory\patchclean.vbs \\server\patchdirectory\

    wscript C:\somedirectory\patchclean.vbs "\\server\patchdirectory"

    wscript C:\somedirectory\patchclean.vbs "\\server\patchdirectory\"

    Got the same error when running manually.

  • Set f = fso.GetFolder("patchdirectory")

    Take it out of the " "  you make it a string when you do this, it is a variable..

    and the path you pass should just be the path no " "

  • That did it. It did exactly what I wanted it to do...when I ran it manually.

    For some reason when I schedule this through Kaseya it just stays in the pending procedures and doesnt actually execut (or fail for that matter).

    I currently have the command execute as System although I tried the script with "logged on user" also and that didnt work.

    Any Ideas?

  • Normally when it does that for me it either failed or its requesting an action (e.g.: User credentials, asks for permission to execute the script, vbscript error popup requiring user interaction, etc....).

    If the script runs okay when you run it manually, then I would suggest you check the Kaseya Agent credentials are valid and also make sure that you execute the the VB script as a user with admin rights.

    Next I would make sure that you using the correct switches to run the vb script silently (%windir%\system32\wscript.exe //nologo (path\script.vbs)) and that you put the "On Error Resume Next" line near the top of your script to skip any silly false positive errors from blocking the script to run.

    Also last thing I have found that sometimes the "Execute file" option works when the "Execute Shell Command" does not, just make sure you if you use this to execute %windir%\system32\wscript.exe and add the //nologo (path\script.vbs) to the "Enter arguments to pass to executable" bit.